Menu
Topics Index
...
`

Book cloning


If you need explanation Read this topic

If you need Answer Take test on this topic

Book cloning
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;
        }
    }
}

A. Book name Sherlock Holmes and price is 170.
B. Don't do xerox buy it
Book name Sherlock Holmes and price is 170.
C. Compilation Error
D. Runtime Error
Topic: clone() Method And cloneable Interface In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is A
Because Clone() function is used to create copy of object having exactly similar values and state....
Here,"xerox" object is copy of "myBook"...so it will have price as "170" and bookName as "sherlock Holmes".

Posted by Mânïshå Mùlchåndânï    2014-07-14 12:29:29


A

Posted by Arul Palanisamy    2014-07-14 13:28:24


Congratulations Mânïshå Mùlchåndânï. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-07-15 11:10:32


© meritcampus 2019

All Rights Reserved.

Open In App