Java OCA OCP Practice Question 2234

Question

Ee have a Reader instance that will produce the characters with the numeric values {1,2,3,4,5,6,7}.

Which of the following are possible outcomes of executing the checkLottoNumbers() method with this Reader instance? (Choose two.)

23: public String checkLottoNumbers(Reader r) throws IOException { 
24:    r.read();r.skip(1); 
25:    r.mark(5); 
26:    r.skip(1); 
27:    r.reset(); 
28:    return r.read()+"-"+r.read(new char[5]); 
29: } 
  • A. An IOException on line 25
  • B. An IOException on line 27
  • C. 'c'-4 is returned.
  • D. 'd'-3 is returned.
  • E. 3-4 is returned.
  • F. 4-3 is returned.


B, E.

Note

The method does not call the markSupported() prior to calling mark() and reset().

This is considered a poor practice.

The input variable could be a subclass of Reader that does not support this functionality.

In this situation, the method would ignore the mark() call and throw an IOException on the reset() method, making Option A incorrect and Option B correct.

On the other hand, if marking the stream is supported, then no exception would be thrown.

First, line 24 skips two values, 1 and 2.

On line 25, the mark() method is called with a value of 5, which is the number of characters that can be read and stored before the marked point is invalidated.

Next, line 26 would skip another value but is undone by the reset() on line 27.

The next value to be read would be the third value, 3.

The read(char[]) call would then read up to five values, since that is the size of the array.

Since only four are left (4, 5, 6, 7) the method would return a value of 4, corresponding to the number of characters read from the stream.

The output is 3-4, making Option E the correct answer.

Options C and D can be eliminated because read() returns an int value, not a char.




PreviousNext

Related