Java OCA OCP Practice Question 1884

Question

How many compilation errors does the following class contain?

package mypkg; /*w w  w . j  a va2s . c  om*/
import java.io.*; 
public class Main { 
   public void m(File f) { 
      try (BufferedReader r = new BufferedReader(FileReader(f))) { 
         final String music = null; 
         try { 
            while((music = r.readLine()) != null) 
               System.out.println(music); 
         } catch (IOException e) {} 
      } catch (FileNotFoundException e) { 
         throw new RuntimeException(e); 
      } finally {} 
   }
} 
  • A. None
  • B. One
  • C. Two
  • D. Three


D.

Note

The first compilation error is that the FileReader constructor call is missing the new keyword.

The second compilation error is that the music variable is marked final, but then modified in the while loop.

The third compilation problem is that the m() method fails to declare or handle an IOException.

Even though the IOException thrown by readLine() is caught, the one thrown by the implicit call to close() via the try-with- resources block is not caught.

Due to these three compilation errors, Option D is the correct answer.




PreviousNext

Related