Menu
Question Index
...

What will be the output of the following program?

public class Cloning {
    public static void main(String[] args) {
        Book myBook = new Book(170, "Sherlock Holmes");
        Book xerox = (Book) myBook.clone();
        System.out.println("Book name " + xerox.bookName + " and price is " + xerox.price + ".");
    }
}
class Book implements Cloneable {
    public Book(int price, String bookName) {
        this.price = price;
        this.bookName = bookName;
    }
    String author;
    int price;
    String bookName;
    protected Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            System.out.println("Don't do xerox buy it");
            return this;
        }
    }
}


Book name Sherlock Holmes and price is 170.
Don't do xerox buy it
Book name Sherlock Holmes and price is 170.
Compilation Error
Runtime Error

Doubts

Problems

Topic: clone() Method And cloneable Interface In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App