ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
static void m(int x) throws java.io.IOException, InterruptedException {
    if (x == 0) throw new java.io.IOException();
    else throw new InterruptedException();
}
public static void main(String[] a) {
    try {
        m(0);
    } catch (java.io.IOException | InterruptedException e) {
        System.out.print(e.getClass().getSimpleName());
    }
}
  1. A. Exception
  2. B. Compilation error: cannot combine unrelated exception types
  3. C. InterruptedException
  4. D. IOException

Correct answer: D. IOException

IOException and InterruptedException are unrelated but both checked, which multi-catch explicitly allows. m(0) throws an IOException, so the catch runs and e.getClass().getSimpleName() is "IOException".

More OOP with C++ and Java questions

Exams that ask this topic