Exam 1z0-809 All QuestionsBrowse all questions from this exam
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.)

    Correct Answer: A, E

    To enable the code to print 'Open' and then 'Close', two modifications are necessary. First, the Folder class needs to implement the AutoCloseable interface so that it can be used in the try-with-resources statement. This is achieved by replacing line n1 with 'class Folder implements AutoCloseable'. Second, a close method needs to be defined in the class. Inserting a public void close() method that throws IOException and prints 'Close' at line n2 will fulfill this requirement. These changes ensure that the try-with-resources statement correctly handles the resources by calling the close method automatically.

Discussion
Ajit0110Options: AE

Option A and E are correct. Tested.Here we need to implement AutoCloseable interface while using try-with resources. When we implement Autocloaseable interface, we must all its methods i.e. open and close methods.

mevlt

Just close() there is no such method as open() in AutoCloseable

WilsonKKerllOptions: AE

Answer is A, E.

shivkumarxOptions: AE

AE is right AD would be right if the visibility was not reduced to package private

asdfjhfgjuaDCVOptions: AD

AD is the correct answer final class Folder implements AutoCloseable { public void open() { System.out.print("Open"); } public final void close() { System.out.print("Close"); } } public class Test { public static void main(String[] args) throws Exception { try (Folder f = new Folder()) { f.open(); } } }

shivkumarx

Except D does not use public accessibility, and hence will not compile

steefaandOptions: AE

A and E are correct.

duydnOptions: AE

A & E are correct

r1muka5Options: AE

Answers are A and E.