Java OCA OCP Practice Question 2468

Question

Which option, when inserted at //INSERT CODE HERE, will enable method read() in class Main to read and output its own source file?.

class Main{/*from   w  ww  .  jav  a  2s. c  o  m*/
    public void read() throws IOException{
        File f = new File("Main.java");
        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);
        String line = null;
        //INSERT CODE HERE
            System.out.println(line);
        br.close();
        fr.close();
    }
}
  • a while ((line = br.readLine()) != null)
  • b while ((line = fr.readLine()) != null)
  • c while ((line = f.readLine()) != null)
  • d while ((line = br.readLine()) != -1)
  • e while ((line = fr.readLine()) != -1)
  • f while ((line = f.readLine()) != -1)


a

Note

Method readLine() isn't defined in classes File and FileReader.

It's defined only in class BufferedReader.

Also, readLine() returns null if the end of the stream has been reached.




PreviousNext

Related