nerdexam
Oracle

1Z0-819 · Question #73

Given public class Main { public static void main (String[] args) { try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { System.out.print("Input: "); String input =…

The correct answer is C. Input: Helloworld. Option C is marked correct, but this question actually contains a subtlety worth examining carefully - D is technically the more accurate answer, and this appears to be a flawed exam key. What the code actually does: The program ignores args entirely and reads from System.in…

Java I/O API

Question

Given public class Main { public static void main (String[] args) { try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { System.out.print("Input: "); String input = br.readLine(); System.out.println("Echo: " + input); } catch (IOException e) { e.printStackTrace(); } } } And the command: java Main Helloworld What is the result ?

Options

  • AInput: Echo: Helloworld
  • BInput: Echo: Helloworld Echo: Helloworld
  • CInput: Helloworld
  • DInput: Program will block until any input comes from System.in.
  • EA NullPointerException is thrown at run time.

How the community answered

(54 responses)
  • A
    13% (7)
  • B
    2% (1)
  • C
    78% (42)
  • D
    6% (3)
  • E
    2% (1)

Explanation

Option C is marked correct, but this question actually contains a subtlety worth examining carefully - D is technically the more accurate answer, and this appears to be a flawed exam key.

What the code actually does: The program ignores args entirely and reads from System.in (the keyboard). When you run java Main Helloworld, "Helloworld" is a command-line argument stored in args[0], which is never accessed. The program prints "Input: " and then blocks indefinitely waiting for keyboard input - making D the correct real-world behavior.

Why each distractor fails:

  • A (Input: Echo: Helloworld) - incorrect formatting; System.out.print doesn't add a newline, but println would put "Echo:" on a new line, not the same line as "Input:"
  • B - there's no logic to print anything twice
  • C (Input: Helloworld) - only plausible if you consider the terminal echoing a user's typed "Helloworld" on the same line as "Input: "; but this omits the subsequent "Echo:" line the program would also print
  • E - no NullPointerException; even if readLine() returns null at EOF, string concatenation with null is valid in Java

Memory tip: When you see System.in in Java code, ask yourself: "Where is the data coming from?" Command-line arguments go to args[], not System.in. If the code reads System.in but the user passes args, those args are silently ignored.

Topics

#try-with-resources#BufferedReader#System.in#command-line args vs input streams

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice