Menu
Topics Index
...
`

Book cloning2460


If you need explanation Read this topic

If you need Answer Take test on this topic

Book cloning2460
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 = myBook.clone();
        System.out.println("Book name " + xerox.bookName + " and price is " + xerox.price + ".");
    }
}
class Book {
    public Book(int price, String bookName) {
        this.price = price;
        this.bookName = bookName;
    }
    String author;
    int price;
    String bookName;
    @Override
    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. Some other output
D. Compilation Error
E. 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 d..clone()-type mismatch compilatn err

Posted by Maheshwari Natarajan    2014-06-13 13:12:55


answer is d...as clone method retrns the object...it needs type casting thats y it throws compilatn eror

Posted by Vijay Kumar    2014-06-13 13:30:09


Congratulations Vijay Kumar. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-06-16 03:38:07


© meritcampus 2019

All Rights Reserved.

Open In App