ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
try { throw null; }
catch (NullPointerException e) { System.out.print("NPE"); }
catch (Exception e) { System.out.print("E"); }
  1. A. NPE
  2. B. E
  3. C. Compile-time error: cannot throw null
  4. D. Nothing; throw null is ignored

Correct answer: A. NPE

throw null compiles (null has the null type, assignable to Throwable), and throwing a null reference raises a NullPointerException, which the first catch handles.

More OOP with C++ and Java questions

Exams that ask this topic