ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
class N {
    public Object clone() throws CloneNotSupportedException { return super.clone(); }
}
// in main:
try { new N().clone(); System.out.print("ok"); }
catch (CloneNotSupportedException e) { System.out.print("CNSE"); }
  1. A. ok
  2. B. Compile-time error: N must implement Cloneable
  3. C. CNSE
  4. D. A ClassCastException is thrown

Correct answer: C. CNSE

Overriding clone() compiles, but Object.clone checks at run time that the class implements the Cloneable marker interface and otherwise throws CloneNotSupportedException.

More OOP with C++ and Java questions

Exams that ask this topic