1Z0-805 · Question #5
Given the code fragment: public static void processFile () throws IOException { Try (FileReader fr = new FileReader ("logfilesrc.txt"); FileWriter fw = new FileWriter ("logfilesdst.txt") ) { int i =…
The correct answer is B. The java runtime automatically closes the FileWriter Instance first and the FileReader instance next. The try-with-resources statement is a try statement that declares one or more resources. Aresource is an object that must be closed after the program is finished with it. The try- with- resources statement ensures that each resource is closed at the end of the statement. Any…
Question
Given the code fragment:
public static void processFile () throws IOException { Try (FileReader fr = new FileReader ("logfilesrc.txt"); FileWriter fw = new FileWriter ("logfilesdst.txt") ) { int i = fr.read(); } } Which statement is true?
Options
- AThe code fragment contains compilation errors.
- BThe java runtime automatically closes the FileWriter Instance first and the FileReader instance next.
- CThe java runtime automatically closes the FileReader Instance first and the FileWriter instance next.
- DThe developer needs to close the FileReader instance first and the FileWriter instance explicitly in a
- EThe Java runtime may close the FileReader and FileWriter instance in an intermediate manner.
How the community answered
(53 responses)- A2% (1)
- B77% (41)
- C4% (2)
- D13% (7)
- E4% (2)
Explanation
The try-with-resources statement is a try statement that declares one or more resources. Aresource is an object that must be closed after the program is finished with it. The try- with- resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implementjava.io.Closeable, can be used as a resource. Oracle 1Z0-805 Exam
Topics
Community Discussion
No community discussion yet for this question.