1Z0-805 · Question #57
Given: private static void copyContents() { try ( InputStream fis = new FileInputStream("report1.txt"); OutputStream fos = new FileOutputStream("consolidate.txt"); ) { byte[] buf = new byte[8192]…
The correct answer is A. Compilation fails due to an error at line 28. The auto-closable resource fis may not be assigned. Note: The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with- resources statement ensures that…
Question
Given:
private static void copyContents() { try ( InputStream fis = new FileInputStream("report1.txt"); OutputStream fos = new FileOutputStream("consolidate.txt"); ) { byte[] buf = new byte[8192]; int i; while ((i = fis.read(buf)) != -1) { fos.write(buf, 0, i); } fis.close(); fis = new FileInputStream("report2.txt"); while ((i = fis.read(buf)) != -1) { fos.write(buf, 0, i); } } What is the result?
Options
- ACompilation fails due to an error at line 28
- BCompilation fails due to error at line 15 and 16
- CThe contents of report1.txt are copied to consolidate.txt. The contents of report2.txt are appended to
- DThe contents of report1.txt are copied to consolidate.txt. The contents of report2.txt are appended to
How the community answered
(40 responses)- A70% (28)
- B8% (3)
- C18% (7)
- D5% (2)
Explanation
The auto-closable resource fis may not be assigned. Note: The try-with-resources statement is a try statement that declares one or more resources. A resource 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 implement java.io.Closeable, can be used as a resource.
Topics
Community Discussion
No community discussion yet for this question.