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…
Question
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)- A13% (7)
- B2% (1)
- C78% (42)
- D6% (3)
- E2% (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.printdoesn't add a newline, butprintlnwould 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 ifreadLine()returnsnullat 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
Community Discussion
No community discussion yet for this question.