QUESTION 51
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.)
A. Replace
line n1
with:
class Folder implements AutoCloseable {
B. Replace
line n1
with:
class Folder extends Closeable {
C. Replace
line n1
with:
class Folder extends Exception {
D. At line n2, insert:
final void close () {
System.out.print(“Close”);
}
E. At line n2, insert:
public void close () throws IOException {
System.out.print(“Close”);
}
Correct Answer:
AE
Section: (none)
Explanation
Explanation/Reference:
Partthree涉及到的AutoCloseable,Closeable和Flushable接口:
QUESTION 52
You want to create a singleton class by using the Singleton design pattern.
Which two statements enforce the singleton nature of the design? (Choose two.)
A. Make the class
static
.
B. Make the constructor
private
.
C. Override
equals
() and
hashCode
() methods of the java.lang.Object class.
D. Use a
static
reference to point to the single instance.
E. Implement the
Serializable
interface.
Correct Answer:
BD
Section: (none)
Explanation
Explanation/Reference:
QUESTION 53