nerdexam
Oracle

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.

Java Basics

Question

Given this code in a file Traveler.java:

And the commands:

javac Traveler.java java Traveler Java Duke What is the result?

Exhibit

1Z0-803 question #157 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)
  • A
    89% (25)
  • C
    4% (1)
  • D
    7% (2)

Why each option

The program prints a fixed greeting followed by the second command-line argument provided during execution.

AHappy Journey! DukeCorrect

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'.

BHappy Journey! Java

This option would be correct if `args[0]` (Java) was printed, but the code accesses the second argument (`args[1]`).

CAn exception is thrown at runtime.

No exception is thrown because sufficient arguments are provided, and the array index accessed (`1`) is within the bounds of the `args` array.

DThe program fails to execute due to a runtime error.

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

#main method#command line arguments#String[] args

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice