ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
// static methods of the main class:
static void f() {
    try {
        System.out.print("1");
        throw new IllegalStateException();
    } catch (IllegalArgumentException e) {
        System.out.print("2");
    } finally {
        System.out.print("3");
    }
    System.out.print("4");
}
// in main:
try { f(); }
catch (RuntimeException e) { System.out.print("5"); }
System.out.print("6");
  1. A. 1356
  2. B. 1346
  3. C. 12346
  4. D. 136

Correct answer: A. 1356

f prints 1 and throws IllegalStateException, which the IllegalArgumentException handler does not match. finally still prints 3, the exception leaves f so 4 is skipped, main's RuntimeException handler prints 5, and then 6.

More OOP with C++ and Java questions

Exams that ask this topic