ProgrammingVery hardGATE CS · TCS NQT Prime · Product Company SDE
java class R implements AutoCloseable { public void close() { System.out.print("close"); } } try (R r = new R()) { System.out.print("body"); throw new…
class R implements AutoCloseable {
public void close() { System.out.print("close"); }
}
try (R r = new R()) {
System.out.print("body");
throw new RuntimeException();
} catch (RuntimeException e) {
System.out.print("catch");
}What is printed?- A. bodyclosecatch
- B. bodycatchclose
- C. bodyclose
- D. closebodycatch
Correct answer: A. bodyclosecatch
A try-with-resources block closes its resources as part of unwinding the try block, which happens before any catch clause runs; so body prints, then the thrown exception triggers close(), and only then is it caught.
More OOP with C++ and Java questions
- Which statement about C++ vtables is correct for a class with multiple virtual functions?
- Class A's constructor declares throws IOException. Class B extends A. What must every constructor of B do about this checked exception?
- What access modifier and finality does a field declared directly inside a Java interface implicitly have?
- java Function<Integer,Integer> sq = x -> x x; Function<Integer,Integer> addOne = x -> x + 1; Function<Integer,Integer> combo =…
- What does the 'Rule of Five' in modern C++ say?
- java Function<Integer,Integer> sq = x -> x x; Function<Integer,Integer> addOne = x -> x + 1; Function<Integer,Integer> combo =…