ProgrammingHardInfosys DSE · Wipro Elite

What is printed?

What is printed?
class R implements AutoCloseable {
    String n;
    R(String n) { this.n = n; System.out.print("o" + n); }
    public void close() { System.out.print("c" + n); }
}
// main:
try (R a = new R("1"); R b = new R("2")) { System.out.print("B"); }
finally { System.out.print("F"); }
  1. A. o1o2Bc1c2F
  2. B. o1o2Bc2c1F
  3. C. o1o2BFc2c1
  4. D. o1Bo2c2c1F

Correct answer: B. o1o2Bc2c1F

Resources close in reverse order of opening, before the finally block runs.

More OOP with C++ and Java questions

Exams that ask this topic