ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement

Which statement about this code is correct?

Which statement about this code is correct?
try {
    new java.io.FileReader("a.txt").close();
} catch (java.io.IOException | Exception e) {
    System.out.print("caught");
}
  1. A. It compiles and prints caught only if a.txt is missing
  2. B. It fails to compile because IOException is a subclass of Exception
  3. C. It compiles, and the IOException branch is chosen first
  4. D. It fails to compile because a multi-catch parameter must be final

Correct answer: B. It fails to compile because IOException is a subclass of Exception

Alternatives in a multi-catch may not be related by subclassing, since the narrower one would be redundant, so this is a compile-time error. The parameter is implicitly final, which is allowed and needs no keyword.

More OOP with C++ and Java questions

Exams that ask this topic