Analyse the following code using The java.lang.Cloneable interface : public class Test { public static void main(String[] args) { java.util.Date x = new java.util.Date(); java.util.Date y = x.clone(); System.out.println(x = y); } }

Analyse the following code using The java.lang.Cloneable interface :
public class Test {  public static void main(String[] args) {    java.util.Date x = new java.util.Date();    java.util.Date y = x.clone();    System.out.println(x = y);  }}


A. A java.util.Date object is not cloneable.
B. x = y in System.out.println(x = y) causes a compile error because you cannot have an assignment statement inside a statement.
C. x = y in System.out.println(x = y) causes a runtime error because you cannot have an assignment statement inside a statement.
D. The program has a compile error because the return type of the clone() method is java.lang.Object.

The correct answer is D

Explanation: (A) is wrong because Date implements and Cloneable and overrides the clone() method. (B) and (C) are wrong because x = y is an assignment expression, which assigns y to x. (D) is correct. You have to cast it into Date in order to assign it to y.


Java

Learn More Multiple Choice Question :