ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
try {
    try { throw new IllegalStateException("A"); }
    finally { throw new IllegalArgumentException("B"); }
} catch (RuntimeException e) {
    System.out.print(e.getMessage() + " " + e.getSuppressed().length);
}
  1. A. A 0
  2. B. B 1
  3. C. A 1
  4. D. B 0

Correct answer: D. B 0

An exception thrown from finally replaces the one in flight; A is lost entirely. Only try-with-resources records the other exception as suppressed, so the suppressed array is empty.

More OOP with C++ and Java questions

Exams that ask this topic