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");
}- A. It compiles and prints caught only if a.txt is missing
- B. It fails to compile because IOException is a subclass of Exception
- C. It compiles, and the IOException branch is chosen first
- 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
- What is printed? java class A { void m(A a) { System.out.print("AA"); } } class B extends A { void m(A a) { System.out.print("BA"); } void…
- Which lines fail to compile? java List<Integer> ints = new ArrayList<>(); List<Number> a = ints; // line 1 List<? extends Number> b =…
- What happens? java boolean flag = true; Integer none = null; Integer r1 = flag ? null : 0; System.out.print(r1); Integer r2 = flag ? none…
- What is printed? java System.out.print(Math.abs(Integer.MINVALUE) + " "); System.out.print(Integer.MAXVALUE + 1 == Integer.MINVALUE);
- What is printed? java System.out.print((-7 / 2 == -3) + " " + (-7 % 2 == 1) + " " + (Math.floorMod(-7, 2) == 1) + " " + (7 % -2 == 1));
- Which line fails to compile? java byte b = 10; b += 5; // line 1 b++; // line 2 b = b + 1; // line 3 b = 10 + 20; // line 4