Which of the options, when inserted at //INSERT CODE HERE, will print close? (Choose all that apply.)
//INSERT CODE HERE public void close() { System.out.println("close"); }/* w w w . java 2 s.c o m*/ } class Main2 { public static void main(String... args) { try (MyClass arton = new MyClass()) {} } }
a class MyClass implements AutoCloseable{ b class MyClass implements java.lang.Closeable{ c class MyClass implements java.lang.AutoCloseable{ d class MyClass implements ImplicitCloseable{ e class MyClass implements java.io.Closeable{
a, c, e
The code will print close, if class MyClass implements the java.lang.AutoCloseable interface or any of its sub interfaces.
Options (a) and (c) are correct, because MyClass implements the interface java.lang.AutoCloseable itself.
Option (b) is incorrect.
The Closeable interface that extends the java.lang.AutoCloseable interface is defined in the java.io package.
Option (d) is incorrect.
The Java API doesn't define any interface with the name ImplicitCloseable
in the java.lang package.
Option (e) is correct because java.io.Closeable is a sub interface of java.lang.AutoCloseable.