Java OCA OCP Practice Question 1872

Question

Assume the file prime6.txt exists and contains the first six prime numbers as bytes: 2, 3, 5, 7, 11, 13.

What is the output of the following application?

package mypkg; //from   ww w.  j  av  a  2s.  co  m
import java.io.*; 
public class Main { 
   public static void main(String[] real) throws Exception { 
      try (InputStream is = new FileInputStream("prime.txt")) { 
         is.skip(1); 
         is.read(); 
         is.skip(1); 
         is.read(); 
         is.mark(4); 
         is.skip(1); 
         is.reset(); 
         System.out.print(is.read()); 
      } 
   } 
} 
  • A. 11
  • B. 13
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


D.

Note

The code compiles, so Option C is incorrect.

The FileInputStream does not support marks, though, leading to an IOException at runtime when the reset() method is called.

Option D is the correct answer.

Be suspicious of any code samples that call the mark() or reset() method without first calling markSupported().




PreviousNext

Related