Java | Functions | Question 10

Predict the output of the following program.

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test implements Cloneable
{
    int a;
  
    Test cloning()
    {
        try
        {
            return (Test) super.clone();
        }
        catch(CloneNotSupportedException e)
        {
            System.out.println("CloneNotSupportedException is caught");
            return this;
        }
    }
}
  
class demo
{
  
    public static void main(String args[])
    {
        Test obj1 = new Test();
        Test obj2;
        obj1.a = 10;
        obj2 = obj1.cloning();
        obj2.a = 20;
  
        System.out.println("obj1.a = " + obj1.a);
        System.out.println("obj2.a = " + obj2.a);
    }
}

chevron_right


(A)

obj1.a = 10
obj2.a = 20

(B)

obj1.a = 20
obj2.a = 20

(C)

obj1.a = 10
obj2.a = 10


Answer: (A)

Explanation: The clone( ) method generates a duplicate copy of the object on which it is called. Only classes that implement the Cloneable interface can be cloned.

Quiz of this Question
Please comment below if you find anything wrong in the above post



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.