ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
// in main, which is declared throws Exception:
try (AutoCloseable r = null) {
    System.out.print("body");
}
System.out.print(" done");
  1. A. A NullPointerException is thrown when the resource is closed
  2. B. Compile-time error: a resource cannot be null
  3. C. body done
  4. D. " done" only; the body is skipped

Correct answer: C. body done

The try-with-resources translation only calls close() if the resource is non-null, so a null resource is simply ignored.

More OOP with C++ and Java questions

Exams that ask this topic