nerdexam
Oracle

1Z0-808 · Question #7

Given the code from the Greeting.java file: public class Greeting { public static void main(String[] args) { System.out.println("Hello " + args[0]); } } Which set of commands prints Hello Duke in…

The correct answer is C. javac Greeting.java java Greeting Duke. Option C is correct because Java requires a two-step process: first compile the .java source file with javac Greeting.java, then run the compiled bytecode with java Greeting Duke - where Duke becomes args[0], producing "Hello Duke". Why the distractors fail: A - javac Greeting…

Java Basics

Question

Given the code from the Greeting.java file: public class Greeting { public static void main(String[] args) { System.out.println("Hello " + args[0]); } } Which set of commands prints Hello Duke in the console?

Options

  • Ajavac Greeting java Greeting Duke
  • Bjavac Greeting.java Duke java Greeting
  • Cjavac Greeting.java java Greeting Duke
  • Djavac Greeting.java java Greeting.class Duke

How the community answered

(57 responses)
  • A
    2% (1)
  • B
    7% (4)
  • C
    88% (50)
  • D
    4% (2)

Explanation

Option C is correct because Java requires a two-step process: first compile the .java source file with javac Greeting.java, then run the compiled bytecode with java Greeting Duke - where Duke becomes args[0], producing "Hello Duke".

Why the distractors fail:

  • A - javac Greeting is wrong; the compiler requires the full filename with the .java extension.
  • B - javac Greeting.java Duke attempts to compile Duke as a second source file, which doesn't exist; the argument Duke belongs at runtime, not compile time.
  • D - java Greeting.class Duke is wrong; the java launcher takes the class name (Greeting), not the filename - specifying .class causes a runtime error.

Memory tip: Think of it as "compile the file, run the class" - javac needs the extension (.java), while java needs the bare class name with no extension. Runtime arguments always go after the class name, never during compilation.

Topics

#command-line arguments#javac compilation#main() method#args parameter

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice