Clone() Method Quiz

Clone() Method Quiz | Cloning of objects means creating an exact duplicate copy with the current object state. In Java, to perform cloning clone() method is given in java.lang.Object class. Also see:- Object Class Quiz Set-1, Java Hashcode Value Quiz, Java Equals() Method Quiz

Q1) What is the return type of clone() method in the Object class?

a) Class
b) Object
c) int
d) String

View Answer Answer:- b) Object

Q2) clone() method of Object class is?

a) public method
b) protected method
c) private method
d) None of these

View Answer Answer:- b) protected method

Q3) Which of the following statement is not applicable for the clone() method of Object class?

a) To execute the clone() method on an object the class must implement java.lang.Cloneable interface.
b) It is a protected method.
c) It throws CloneNotSupportException.
d) It is a static method.

View Answer Answer:- d) It is a static method.

All methods of java.lang.Object class are instance/non-static methods.

Q4) Which of the following statement is true about the Cloneable interface?

a) It doesn’t contain any method.
b) It contains only one method.
c) It contains multiple methods.
d) None of these

View Answer Answer:- a) It doesn’t contain any method.

Cloneable is a marker interface, it doesn’t contain any method.

Q5) Find the output of the below Java program?

class Student implements Cloneable {
  int id;
  Student(int id) {
    this.id = id;
  }

  public static void main(String[] args) 
        throws CloneNotSupportedException {
    Student s1 = new Student(9);
    Student s2 = s1;

    s1.id = 18;
    System.out.println(s1.id+" "+s2.id);
  }
}

a) 9 9
b) 18 9
c) 9 18
d) 18 18

View Answer Answer:- d) 18 18

In this program, reference of s1 variable is assigned to the variable s2. Therefore after modifying s1 variable data changes will also reflect to the s2.

Q6) Find the output of the below Java program?

class Student implements Cloneable {
  int id;
  Student(int id) {
    this.id = id;
  }

  public static void main(String[] args) 
        throws CloneNotSupportedException {
    Student s1 = new Student(9);
    Student s2 = (Student) s1.clone();

    s1.id = 18;
    System.out.println(s1.id+" "+s2.id);
  }
}

a) 9 9
b) 18 9
c) 9 18
d) 18 18

View Answer Answer:- b) 18 9

On using clone() method of Object class:- If the main object contains primitive variables then exactly duplicate copies will be created in the cloned object. Therefore modification performed in s1 will not reflect in s2.

Q7) Find the output of the below Java program?

class P {
  int x = 10;
}

class Q extends P {
  int y = 20;
}

class R extends Q implements Cloneable {
  int z = 30;

  public static void main(String[] args) 
              throws CloneNotSupportedException{
     R r1 = new R();
     R r2 = (R) r1.clone();
     r1.x = 100;
     r1.y = 200;
     r1.z = 300;
     System.out.println(r2.x+" "+r2.y+" "+r2.z);
  }
}

a) 100 200 300
b) 10 20 30
c) 10 20 300
d) 100 200 30

View Answer Answer:- b) 10 20 30

For IS-A relation Java performs deep cloning. The clone() method of the Object class clones the object inheritance graphs starts from the root superclass (java.lang.Object class) to the current cloning subclass.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *