Java OCA OCP Practice Question 190

Question

Suppose you know that a file named aaa was created by a Java program that used a DataOutputStream.

The file contains 10 doubles, followed by a UTF string.

Which of the following code snippets read the string correctly?

Assume all code exists in an environment that legally handles IOException.

Choose all correct options.


A.     RandomAccessFile raf = new RandomAccessFile("aaa", "r"); 
       for (int i=0; i<10; i++) 
         raf.readDouble(); /*from w w  w .ja v a2 s.c  o m*/
       String s = raf.readUTF(); 

B.     RandomAccessFile raf = new RandomAccessFile("aaa", "r"); 
       raf.seek(10*8); 
       String s = raf.readUTF(); 

C.     FileReader fr = new FileReader(fr); 
       for (int i=0; i<10*8; i++) 
         fr.read(); 
       String s = fr.readUTF(); 

D.      FileInputStream fis = new FileInputStream("aaa"); 
        DataInputStream dis = new DataInputStream(fis);  
        for (int i=0; i<10; i++) 
            dis.readDouble(); 
        String s = dis.readUTF(); 

E.      FileInputStream fis = new FileInputStream("aaa"); 
        DataInputStream dis = new DataInputStream(fis); 
        dis.seek(10*8); 
        String s = dis.readUTF(); 


A, B, D.

Note

A and B correctly use a random access file.

A reads and discards the leading doubles;

B just seeks past them.

C doesn't work because readers are for files that contain only 8-bit text; they don't work on UTF files.

D and E use a file input stream.

D correctly reads and discards the leading doubles and then reads the UTF string.

E doesn't work because the DataInputStream class doesn't have a seek() method.




PreviousNext

Related