ProgrammingVery hardProduct-company MCQ pattern
What is printed?
What is printed?
static void f() throws Exception {
try {
throw new RuntimeException("A");
} finally {
throw new IllegalStateException("B");
}
}
public static void main(String[] a) {
try { f(); } catch (Exception e) { System.out.print(e.getMessage()); }
}- A. AB
- B. A
- C. B
- D. A then B on separate lines
Correct answer: C. B
When finally itself throws, that new exception replaces the one propagating from try; RuntimeException("A") is discarded (not suppressed, just lost) and only IllegalStateException("B") reaches main's catch.
More OOP with C++ and Java questions
- What is printed? java for (int i = 0; i < 3; i++) { try { if (i == 1) continue; System.out.print(i); } finally { System.out.print("f"); } }
- What is printed? java try { try { throw new java.io.IOException("io-fail"); } catch (java.io.IOException e) { throw new…
- What happens when this runs? java class Test { static void m() { throw new ArithmeticException("boom"); } public static void main(String[]…
- What is printed? java static void m(int x) throws java.io.IOException, InterruptedException { if (x == 0) throw new java.io.IOException()…
- What happens when this runs, assuming recurse() causes infinite recursion? java static void recurse() { recurse(); } public static void…
- What is printed? java class AEx extends Exception {} class BEx extends AEx {} // in main, wrapped in throws Exception: try { throw new…