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…
Question
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)- A2% (1)
- B7% (4)
- C88% (50)
- D4% (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 Greetingis wrong; the compiler requires the full filename with the.javaextension. - B -
javac Greeting.java Dukeattempts to compileDukeas a second source file, which doesn't exist; the argumentDukebelongs at runtime, not compile time. - D -
java Greeting.class Dukeis wrong; thejavalauncher takes the class name (Greeting), not the filename - specifying.classcauses 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
Community Discussion
No community discussion yet for this question.