ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
try {
    try { System.out.print("1"); throw new Exception("a"); }
    catch (Exception e) { System.out.print("2"); throw new RuntimeException("b"); }
    finally { System.out.print("3"); }
} catch (RuntimeException e) { System.out.print("4" + e.getMessage()); }
finally { System.out.print("5"); }
  1. A. 12345
  2. B. 1234b5
  3. C. 1234a5
  4. D. 123b45

Correct answer: B. 1234b5

The inner catch throws a new RuntimeException; the inner finally runs before it propagates (3), then the outer catch prints 4 with message b, and the outer finally prints 5.

More OOP with C++ and Java questions

Exams that ask this topic