Java SE 8 Programmer II — Question 183

Given:
final class Folder { //line n1
//line n2
public void open () {
System.out.print("Open");
}
}
public class Test {
public static void main (String [] args) throws Exception { try (Folder f = new Folder()) { f.open();
}
}
}
Which two modifications enable the code to print Open Close? (Choose two.)

Answer options

Correct answer: A, E

Explanation

Option A is correct because implementing AutoCloseable allows the Folder class to be used in a try-with-resources statement, which automatically calls the close method. Option E is also correct as it provides a valid implementation of the close method that can throw an IOException. The other options either do not implement the necessary interface or do not provide a suitable close method.