nerdexam
Oracle

1Z0-809 · Question #233

Given that version.txt is accessible and contains: 1234567890 and given the code fragment: ``java try (FileInputStream fis = new FileInputStream("version.txt"); InputStreamReader isr = new…

The correct answer is B. 122. BufferedReader.markSupported() returns true, so the if block executes. The first read() consumes '1' and moves the cursor to position 2 ('2'). mark(2) saves that position. The second read() consumes '2', then reset() rewinds to the saved mark - back to '2', not the start of the…

Question

Given that version.txt is accessible and contains: 1234567890 and given the code fragment:
try (FileInputStream fis = new FileInputStream("version.txt");
 InputStreamReader isr = new InputStreamReader(fis);
 BufferedReader br = new BufferedReader(isr)) {
 if (br.markSupported()) {
 System.out.print(char) br.read());
 br.mark(2);
 System.out.print(char) br.read());
 br.reset();
 System.out.print(char) br.read());
 }
} catch (Exception e) {
 e.printStackTrace();
}
What is the result?

Options

  • A121
  • B122
  • C125
  • DThe program prints nothing.

How the community answered

(21 responses)
  • A
    5% (1)
  • B
    81% (17)
  • C
    5% (1)
  • D
    10% (2)

Explanation

BufferedReader.markSupported() returns true, so the if block executes. The first read() consumes '1' and moves the cursor to position 2 ('2'). mark(2) saves that position. The second read() consumes '2', then reset() rewinds to the saved mark - back to '2', not the start of the file. The third read() therefore reads '2' again, producing output 122, making B correct.

A (121) is wrong because reset() restores the position set by mark() - which was placed after reading '1', not at the file's beginning, so '1' is never re-read.

C (125) is wrong because no combination of these three reads can reach '5'; the cursor never advances past '2'.

D is wrong because InputStreamReader does not support mark/reset, but wrapping it in a BufferedReader adds that support - markSupported() returns true, so the block runs.

Memory tip: Think of mark() as dropping a bookmark at your current position; reset() flips back to that bookmark, not the cover of the book. The wrapping hierarchy matters: BufferedReader is the one that grants mark support.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice