ProgrammingHardAccenture · Capgemini technical pattern
What happens when this Java code is compiled?
What happens when this Java code is compiled?
try {
FileReader r = new FileReader("a.txt");
} catch (Exception e) {
System.out.print("E");
} catch (IOException e) {
System.out.print("IO");
}- A. Compilation error: the IOException catch block is unreachable
- B. It compiles; IO is printed if the file is missing
- C. It compiles; E is printed if the file is missing
- D. It compiles with only a warning
Correct answer: A. Compilation error: the IOException catch block is unreachable
Catch blocks are tried in order, and Exception already catches every IOException, so the second block can never run. Java treats this as an error; the more specific catch must come first.
More OOP with C++ and Java questions
- This class is meant to be immutable. What change is still needed? java public final class Team { private final List<String> members…
- What is printed? cpp struct V { int x; V(int a) : x(a) {} V operator+(const V& o) const { return V(x + o.x 2); } }; int main() { V a(1)…
- 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…
- Class P is in package a and declares protected int x. Class C is in package b and extends P. Inside an instance method of C, which of…
- What happens when this Java class is compiled? java class Util { static void f(List<String> l) { } static void f(List<Integer> l) { } }
- How many of the following statements about equals() and hashCode() in Java are correct? (i) If a.equals(b) is true, a.hashCode() must…