nerdexam
Oracle

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…

Java I/O Fundamentals

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)
  • A
    2% (1)
  • B
    77% (41)
  • C
    4% (2)
  • D
    13% (7)
  • E
    4% (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

#try-with-resources#resource closing order#FileReader#FileWriter

Community Discussion

No community discussion yet for this question.

Full 1Z0-805 Practice