ProgrammingVery hardGATE CS · TCS NQT Prime · Product Company SDE
java try { throw new ArithmeticException("x"); } catch (RuntimeException e) { System.out.print("R"); } catch (ArithmeticException e) { System.out.print("A")…
try {
throw new ArithmeticException("x");
} catch (RuntimeException e) {
System.out.print("R");
} catch (ArithmeticException e) {
System.out.print("A");
}What happens?- A. Prints "RA"
- B. Compile error: the ArithmeticException catch block is unreachable, since RuntimeException (its superclass) already catches it first
- C. Prints "R"
- D. Prints "A"
Correct answer: B. Compile error: the ArithmeticException catch block is unreachable, since RuntimeException (its superclass) already catches it first
Java requires catch clauses to be ordered from most specific to least specific; putting the broader RuntimeException before its subclass ArithmeticException makes the second clause unreachable, which javac rejects.
More OOP with C++ and Java questions
- Which OOP principle bundles data with the methods that act on it and hides the internal state behind an interface?
- In Java, which keyword makes one class inherit from another class?
- What is the return type of a C++ constructor?
- Every Java class that declares no superclass implicitly extends:
- In C++, members of a class declared without any access specifier are:
- Which statement about Java String objects is correct?