What will be shown in console after compiling this code:

public class Test {
    public static void main(String[] args) throws Exception {
        Test test = new Test();
        Test testCloned = (Test) test.clone();
        Test test2 = test;
        boolean res = test.toString().equals(test2.toString());
        boolean resClone = testCloned.toString().equals(test.toString());
java.lang.CloneNotSupportedException will be thrown, because Test class does not implement Cloneable interface. You need to implement this interface to call clone() method.
A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Note that this interface does not contain the clone method, It is just a markup interface.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Sign Up Now
or Subscribe for future quizzes