ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
try {
try { throw new IllegalStateException("A"); }
finally { throw new IllegalArgumentException("B"); }
} catch (RuntimeException e) {
System.out.print(e.getMessage() + " " + e.getSuppressed().length);
}- A. A 0
- B. B 1
- C. A 1
- D. B 0
Correct answer: D. B 0
An exception thrown from finally replaces the one in flight; A is lost entirely. Only try-with-resources records the other exception as suppressed, so the suppressed array is empty.
More OOP with C++ and Java questions
- 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 {…
- What is printed? java class Pt { int x; Pt(int x) { this.x = x; } public boolean equals(Object o) { return o instanceof Pt && ((Pt) o).x…
- What is printed? java interface Greeter { String name(); default String greet() { return "Hi " + name(); } } class G implements Greeter {…