Java MCQ – Home
Java Basic MCQ
➤ Java Hello World MCQ
➤ Find Java Keywords
➤ Java Identifier Quiz
➤ Java Data Types Quiz
➤ If-Else MCQ in Java-1
➤ If-Else MCQ in Java-2
Object class MCQ
➤ Object Class Quiz
➤ equals() Method Quiz
➤ Hashcode Value Quiz
➤ toString() Method Quiz
➤ Clone() Method Quiz
Multithreading MCQ
➤ Define a Thread-1
➤ Define a Thread-2
➤ Get/set ThreadName
➤ Thread State MCQ
➤ Thread Priority MCQ
➤ Yield(), join() & sleep()
➤ Synchronization MCQ
➤ Interthread Comms
➤ Deadlock, Daemon
Exception Handling
➤ Exception Handling-1
➤ Exception Handling-2
➤ Exception Handling-3
➤ Java try-catch MCQ-1
➤ Java try-catch MCQ-2
➤ Java try-catch MCQ-3
➤ Nested try-catch MCQ
➤ throw Keyword MCQ
➤ finally Block MCQ-1
➤ finally Block MCQ-2
➤ throws Keyword MCQ
Generics MCQ
➤ Java Generics Quiz-1
➤ Java Generics Quiz-2
➤ Java Generics Quiz-3
Collection Framework
➤ Collections Quiz-1
➤ Collections Quiz-2
➤ ArrayList MCQ-1
➤ ArrayList MCQ-2
➤ LinkedList MCQ
➤ Vector Stack MCQ
➤ Java Cursors MCQ
➤ Java TreeSet MCQ
➤ TreeSet & Comparator
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) ObjectQ2) clone() method of Object class is?
a) public method
b) protected method
c) private method
d) None of these
View Answer
Answer:- b) protected methodQ3) 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 18In 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 9On 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 30For 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!