nerdexam
Oracle

1Z0-809 · Question #50

Given that course.txt is accessible and contains: Course : Java and given the code fragment: public static void main (String[ ] args) { char i; try (FileInputStream fis = new FileInputStream…

The correct answer is D. A compilation error occurs at line n1. Option D is correct because InputStreamReader.close() has a return type of void - it doesn't return a value at all - so applying the logical NOT operator (!) to a void result is a type mismatch that the compiler rejects at line n1. Options A and B are wrong because they assume…

Question

Given that course.txt is accessible and contains: Course : Java and given the code fragment: public static void main (String[ ] args) { char i; try (FileInputStream fis = new FileInputStream ("course.txt"); InputStreamReader isr = new InputStreamReader (fis);) { while (!isr.close()) { //line n1 isr.skip(2); i = isr.read(); c = (char) i; System.out.print (c); } } catch (Exception e) { e.printStackTrace(); } } What is the result?

Options

  • Aur::va
  • BueJa
  • CThe program prints nothing.
  • DA compilation error occurs at line n1.

How the community answered

(32 responses)
  • A
    6% (2)
  • B
    3% (1)
  • C
    13% (4)
  • D
    78% (25)

Explanation

Option D is correct because InputStreamReader.close() has a return type of void - it doesn't return a value at all - so applying the logical NOT operator (!) to a void result is a type mismatch that the compiler rejects at line n1. Options A and B are wrong because they assume the code reaches runtime and produces output by tracing the skip(2)/read() pattern, but a program that fails to compile never runs. Option C is wrong for the same reason - "prints nothing" implies successful execution with no output, whereas a compilation error means execution never starts. Memory tip: Treat close() like println() - both return void, and you'd never write if (!System.out.println(...)), so similarly never use close() as a loop condition; use a separate boolean flag or restructure with read() as the condition instead.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice