Java OCA OCP Practice Question 2454

Question

What's the output of the following code?

class Employee implements AutoCloseable{
    String id;//  ww w.java  2 s . c o m
    Employee(String id) {
        this.id = id;
    }
    public void close() {
        System.out.println("close:"+id);
    }
}
public class Main {
    public static void main(String... args) {
        try (Employee admn1 = new Employee("1234");
            Employee admn2 = new Employee("5678");){}
    }
}
a  close:5678
   close:1234

b  close:1234
   close:5678
c   Compilation error
d   Runtime exception


a

Note

The code compiles successfully, and no runtime exceptions are thrown during its execution.

The resources initialized in a try-with-resources statement are closed in the reverse order.




PreviousNext

Related