What is the output of the following application?
package mypkg; //from ww w. ja v a 2s .c om import java.io.*; public class Main { public int m(byte[] data) throws Exception { try (InputStream is = new ByteArrayInputStream(data)) { is.read(new byte[2]); if(!is.markSupported()) return -1; is.mark(5); is.read();is.read(); is.skip(3); is.reset(); return is.read(); } } public static void main(String... sprockets) throws Exception { final Main p = new Main(); System.out.print(p.m(new byte[] {1,2,3,4,5,6,7})); } }
A.
The code compiles and runs without issue.
The first two values of the ByteArrayInputStream are read.
Next, the markSupported()
value is tested.
Since -1 is not one of the possible answers, we assume that ByteArrayInputStream does support marks.
Two values are read and three are skipped, but then reset()
is called, putting the stream back in the state before mark()
was called.
Everything between mark()
and reset()
can be ignored.
The last value read is 3, making Option A the correct answer.