Java OCA OCP Practice Question 2261

Question

Consider the following code segment:

FileInputStream findings = new FileInputStream("log.txt");
DataInputStream dataStream = new DataInputStream(findings);
BufferedReader br = new BufferedReader(new InputStreamReader(dataStream));
String line;
while ((line = br.readLine()) != null) {
        System.out.println(line);
 }
br.close();

Which tWO options are true regarding this code segment?.

A.   br.close() statement will close only the BufferedReader object, 
     and findings and *dataStream* will remain unclosed.
     /*from  w  ww.  j  a v  a2 s .  com*/
B.   the br.close() statement will close the BufferedReader object and 
     the underlying stream objects referred by findings and dataStream.
     
C.   the readLine() method invoked in the statement br.readLine() can 
     throw an IOException; if this exception is thrown, br.close() will 
     not be called, resulting in a resource leak.
     
d.   the readLine() method invoked in the statement br.readLine() can 
     throw an IOException; however, there will not be any resource leaks 
     since Garbage Collector collects all resources.
     
e.   In this code segment, no exceptions can be thrown calling br.close(), 
     so there is no possibility of resource leaks.


Options B and C.

Note

the br.close() statement will close the BufferedReader object and the underlying stream objects referred to by findings and dataStream.

the readLine() method invoked in the statement br.readLine() can throw an IOException;

if this exception is thrown, br.close() will not be called, resulting in a resource leak.

note that Garbage Collector will only collect unreferenced memory resources;

it is the programmer's responsibility to ensure that all other resources such as stream objects are released.




PreviousNext

Related