ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
try {
try {
throw new RuntimeException("inner");
} finally {
System.out.print("inner-finally ");
}
} catch (RuntimeException e) {
System.out.print("outer-catch:" + e.getMessage());
}- A. outer-catch:inner inner-finally
- B. inner-finally outer-catch:inner
- C. inner-finally
- D. outer-catch:inner
Correct answer: B. inner-finally outer-catch:inner
The inner try's finally block always runs during unwinding, even though an exception is propagating out of the inner try; it runs BEFORE the exception reaches the outer catch. Output: inner-finally first, then outer-catch:inner.
More OOP with C++ and Java questions
- What is printed? java class A { } class B extends A { } class Test { public static void main(String[] args) { A a = new B(); try { B b =…
- What is printed? java List<String> strings = new ArrayList<>(); List<Integer> ints = new ArrayList<>()…
- What is printed? java Integer a = 127; Integer b = 127; Integer c = 200; Integer d = 200; System.out.println((a == b) + " " + (c == d));
- What is printed? java class Base { public void show() { System.out.print("Base "); } } class Derived extends Base { protected void show()…
- What is printed? java class Base { void greet(Object o) { System.out.print("Object "); } } class Derived extends Base { void greet(String…
- What is printed? java class Base { static void show() { System.out.print("Base.static "); } } class Derived extends Base { static void…