1Z0-803 · Question #157
Given this code in a file Traveler.java: And the commands: javac Traveler.java java Traveler Java Duke What is the result?
The correct answer is A. Happy Journey! Duke. The program prints a fixed greeting followed by the second command-line argument provided during execution.
Question
Given this code in a file Traveler.java:
And the commands:
javac Traveler.java java Traveler Java Duke What is the result?
Exhibit
Options
- AHappy Journey! Duke
- BHappy Journey! Java
- CAn exception is thrown at runtime.
- DThe program fails to execute due to a runtime error.
How the community answered
(28 responses)- A89% (25)
- C4% (1)
- D7% (2)
Why each option
The program prints a fixed greeting followed by the second command-line argument provided during execution.
When the program is executed with `java Traveler Java Duke`, the string 'Java' becomes `args[0]` and 'Duke' becomes `args[1]`. The `System.out.println` statement then concatenates 'Happy Journey! ' with the value of `args[1]`, resulting in 'Happy Journey! Duke'.
This option would be correct if `args[0]` (Java) was printed, but the code accesses the second argument (`args[1]`).
No exception is thrown because sufficient arguments are provided, and the array index accessed (`1`) is within the bounds of the `args` array.
The program executes successfully as the Java Virtual Machine can load and run the compiled `Traveler` class without any fundamental errors.
Concept tested: Java command-line arguments
Source: https://docs.oracle.com/javase/tutorial/java/interpack/cliargs.html
Topics
Community Discussion
No community discussion yet for this question.
