ProgrammingHardTCS NQT Prime · Infosys SP
What is printed?
What is printed?
try {
try { throw new Exception("1"); }
finally { throw new RuntimeException("2"); }
} catch (Exception e) {
System.out.print(e.getMessage() + " " + e.getSuppressed().length);
}- A. 2 0
- B. 1 0
- C. 1 1
- D. 2 1
Correct answer: A. 2 0
The exception thrown in finally replaces the pending one (it is discarded, not recorded as suppressed), and the outer catch handles the RuntimeException.
More OOP with C++ and Java questions
- What is printed? java class R implements AutoCloseable { String n; R(String n) { this.n = n; System.out.print("o" + n); } public void…
- What is printed? java class A {} class B extends A {} static void f(A a) { System.out.print("A"); } static void f(B b) {…
- What is printed? java class P { void f(Object o) { System.out.print("PO"); } } class C extends P { void f(String s) {…
- What happens? java static void f(String s) { System.out.print("S"); } static void f(Integer i) { System.out.print("I"); } // main: f(null);
- What is printed? java System.out.print('a' + 'b' + "c" + 'a' + 'b');
- What is printed? java class K { int v; K(int v) { this.v = v; } public boolean equals(K o) { return o != null && v == o.v; } public int…