What is the command to compile a Java program named HelloWorld.java?
Use javac HelloWorld.java to compile the Java source file into bytecode.
Click to reveal answer
beginner
How do you run a compiled Java program named HelloWorld from the command line?
Use <code>java HelloWorld</code> to run the compiled Java class file.
Click to reveal answer
beginner
What does the java command expect as input after the command itself?
It expects the name of the class containing the <code>main</code> method to start execution.
Click to reveal answer
intermediate
How can you pass arguments to a Java program via the command line?
Add the arguments after the class name in the <code>java</code> command, e.g., <code>java HelloWorld arg1 arg2</code>. These are accessible in the program via the <code>String[] args</code> parameter of <code>main</code>.
Click to reveal answer
beginner
What is the difference between javac and java commands?
<code>javac</code> compiles Java source code into bytecode (.class files). <code>java</code> runs the compiled bytecode on the Java Virtual Machine.
Click to reveal answer
Which command compiles a Java source file named App.java?
Acompile App.java
Bjava App.java
Crun App.java
Djavac App.java
✗ Incorrect
The javac command compiles Java source files.
How do you execute a compiled Java class named App?
Ajava App
Bjavac App
Crun App
Dexecute App.java
✗ Incorrect
Use java App to run the compiled class.
Where do command line arguments appear in a Java program?
AIn the <code>Scanner</code> class automatically
BIn the <code>System.out</code> stream
CIn the <code>String[] args</code> parameter of <code>main</code>
DIn the <code>java.util</code> package
✗ Incorrect
Arguments passed on the command line are received in main(String[] args).
What file extension does the Java compiler produce?
A.class
B.java
C.exe
D.jar
✗ Incorrect
The compiler produces bytecode files with the .class extension.
If you run java HelloWorld but get an error, what might be a common cause?
AYou forgot to write <code>javac</code> before <code>java</code>
BThe <code>HelloWorld.class</code> file is missing or not in the current directory
CYou need to add <code>.java</code> extension when running
DJava programs cannot be run from the command line
✗ Incorrect
The java command needs the compiled .class file in the current directory or classpath.
Explain the steps to compile and run a Java program from the command line.
Think about what commands you type and in what order.
You got /4 concepts.
Describe how command line arguments are passed to and accessed in a Java program.
Focus on the main method signature and command line syntax.
You got /3 concepts.
Practice
(1/5)
1. What command do you use to compile a Java file named Example.java from the command line?
easy
A. javac Example.java
B. java Example.java
C. compile Example.java
D. run Example.java
Solution
Step 1: Understand Java compilation
Java source files (.java) must be compiled into bytecode (.class) before running.
Step 2: Use the correct command
The javac command compiles Java files, so javac Example.java compiles the file.
Final Answer:
javac Example.java -> Option A
Quick Check:
Compile Java file = javac [OK]
Hint: Use 'javac' to compile Java files before running [OK]
Common Mistakes:
Using 'java' to compile instead of run
Typing 'compile' or 'run' commands which don't exist
Forgetting to compile before running
2. Which command correctly runs a compiled Java program with the main class named App?
easy
A. java App
B. javac App
C. run App
D. java App.class
Solution
Step 1: Identify how to run Java bytecode
Compiled Java classes (.class) are run using the java command followed by the class name without extension.
Step 2: Choose the correct syntax
Running java App executes the App.class file. Adding '.class' or using 'javac' is incorrect here.
Final Answer:
java App -> Option A
Quick Check:
Run Java class = java ClassName [OK]
Hint: Run with 'java' plus class name, no file extension [OK]
Common Mistakes:
Adding '.class' extension when running
Using 'javac' to run instead of compile
Typing 'run' command which is invalid
3. Given the commands executed in order: javac HelloWorld.java java HelloWorld What will be the output if HelloWorld.java contains:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
medium
A. No output
B. HelloWorld
C. Compilation Error
D. Hello, Java!
Solution
Step 1: Compile the Java file
The javac HelloWorld.java command compiles the source code into HelloWorld.class without errors.
Step 2: Run the compiled class
The java HelloWorld command runs the main method, printing the string inside System.out.println.
Final Answer:
Hello, Java! -> Option D
Quick Check:
Print statement output = Hello, Java! [OK]
Hint: Compile first, then run to see printed output [OK]
Common Mistakes:
Trying to run without compiling first
Expecting class name printed instead of message
Confusing compile errors with runtime output
4. You try to run java MyProgram but get the error: Error: Could not find or load main class MyProgram What is the most likely cause?
medium
A. You ran java MyProgram.java instead of java MyProgram
B. You forgot to compile with javac MyProgram.java
C. The class name inside the file is different from the file name
D. You used the wrong Java version
Solution
Step 1: Understand the error meaning
This error means Java cannot find the compiled .class file for MyProgram.
Step 2: Check compilation step
If you did not run javac MyProgram.java first, the .class file does not exist, causing this error.
Final Answer:
You forgot to compile with javac MyProgram.java -> Option B
Quick Check:
Compile before run to avoid class not found [OK]
Hint: Always compile before running to avoid class not found errors [OK]
Common Mistakes:
Running with .java extension in 'java' command
Ignoring class name and file name mismatch
Blaming Java version without checking compilation
5. You have two Java files:
File1.java:
public class File1 {
public static void main(String[] args) {
System.out.println("File1 running");
}
}
File2.java:
public class File2 {
public static void main(String[] args) {
System.out.println("File2 running");
}
}
Which sequence of commands correctly compiles and runs File2 from the command line?
hard
A. java File2 javac File2.java
B. javac File2.java java File2.java
C. javac File2.java java File2
D. javac File1.java File2.java java File2.java
Solution
Step 1: Compile the correct file
To run File2, you must compile File2.java first using javac File2.java.
Step 2: Run the compiled class properly
Run the program with java File2 (without .java extension). Running before compiling or adding extensions causes errors.
Final Answer:
javac File2.java
java File2 -> Option C
Quick Check:
Compile then run target class without extension [OK]
Hint: Compile target file, then run class name without extension [OK]
Common Mistakes:
Running with .java extension
Running before compiling
Compiling unrelated files unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.