• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

Java | Functions | Question 10

Predict the output of the following program. Java
 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);
    }
}

(A)

obj1.a = 10
obj2.a = 20

(B)

obj1.a = 20
obj2.a = 20

(C)

obj1.a = 10
obj2.a = 10

Answer

Please comment below if you find anything wrong in the above post
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated :
Share your thoughts in the comments