ProgrammingModerateProduct-company placement · Java
What happens when this code is compiled?
What happens when this code is compiled?
try { new java.io.FileReader("x.txt"); }
catch (Exception e) { System.out.print("E"); }
catch (java.io.IOException e) { System.out.print("IO"); }- A. Compile-time error: the IOException catch is unreachable
- B. It compiles and prints E if the file is missing
- C. It compiles and prints IO if the file is missing
- D. It compiles and prints EIO if the file is missing
Correct answer: A. Compile-time error: the IOException catch is unreachable
Catch clauses are tried top to bottom, so a subclass catch placed after its superclass can never run; Java makes that a compile-time error. The more specific catch must come first.
More OOP with C++ and Java questions
- What is printed? java // static method of the main class: static int f() { try { throw new RuntimeException("t"); } finally { return 7; }…
- What is printed? java try { try { throw new IllegalStateException("A"); } finally { throw new IllegalArgumentException("B"); } } catch…
- What is printed? java class K { static int v = 10 / Integer.parseInt("0"); } // in main: try { System.out.print(K.v); } catch…
- What is printed? java // static method of the main class: static void down() { down(); } // in main: try { down(); } catch (Exception e) {…
- What happens when this class is compiled? java class A { A() { this(1); } A(int x) { this(); } }
- What is printed? java class A { void f() { System.out.print("Af "); g(); } void g() { System.out.print("Ag "); } } class B extends A {…