Bird
Raised Fist0
Javaprogramming~10 mins

Command line execution in Java - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the first command line argument.

Java
public class Main {
    public static void main(String[] args) {
        System.out.println([1]);
    }
}
Drag options to blanks, or click blank then click option'
Aargs.first()
Bargs(0)
Cargs.get(0)
Dargs[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets to access array elements.
Trying to call methods like get() on the array.
2fill in blank
medium

Complete the code to check if any command line arguments were passed.

Java
public class Main {
    public static void main(String[] args) {
        if ([1] > 0) {
            System.out.println("Arguments received.");
        } else {
            System.out.println("No arguments.");
        }
    }
}
Drag options to blanks, or click blank then click option'
Aargs.length
Bargs.count()
Cargs.size()
Dargs.length()
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() with parentheses which causes a compile error.
Using methods like size() or count() which do not exist for arrays.
3fill in blank
hard

Fix the error in the code to print all command line arguments separated by spaces.

Java
public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < [1]; i++) {
            System.out.print(args[i] + " ");
        }
    }
}
Drag options to blanks, or click blank then click option'
Aargs.count()
Bargs.size()
Cargs.length
Dargs.length()
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() with parentheses causing errors.
Using size() or count() which are not valid for arrays.
4fill in blank
hard

Fill both blanks to print the last command line argument safely.

Java
public class Main {
    public static void main(String[] args) {
        if (args.[1] > 0) {
            System.out.println(args[[2]]);
        } else {
            System.out.println("No arguments.");
        }
    }
}
Drag options to blanks, or click blank then click option'
Alength
Blength()
Csize
Dcount
Elength - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() with parentheses.
Using size or count which are invalid for arrays.
5fill in blank
hard

Fill all three blanks to print all arguments in uppercase separated by commas.

Java
public class Main {
    public static void main(String[] args) {
        for (int [1] = 0; [2] < args.[3]; [1]++) {
            System.out.print(args[[1]].toUpperCase());
            if ([1] < args.length - 1) {
                System.out.print(", ");
            }
        }
    }
}
Drag options to blanks, or click blank then click option'
Ai
Bj
Clength
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size instead of length for arrays.
Using different variable names inconsistently in the loop.

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

  1. Step 1: Understand Java compilation

    Java source files (.java) must be compiled into bytecode (.class) before running.
  2. Step 2: Use the correct command

    The javac command compiles Java files, so javac Example.java compiles the file.
  3. Final Answer:

    javac Example.java -> Option A
  4. 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

  1. 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.
  2. Step 2: Choose the correct syntax

    Running java App executes the App.class file. Adding '.class' or using 'javac' is incorrect here.
  3. Final Answer:

    java App -> Option A
  4. 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

  1. Step 1: Compile the Java file

    The javac HelloWorld.java command compiles the source code into HelloWorld.class without errors.
  2. Step 2: Run the compiled class

    The java HelloWorld command runs the main method, printing the string inside System.out.println.
  3. Final Answer:

    Hello, Java! -> Option D
  4. 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

  1. Step 1: Understand the error meaning

    This error means Java cannot find the compiled .class file for MyProgram.
  2. Step 2: Check compilation step

    If you did not run javac MyProgram.java first, the .class file does not exist, causing this error.
  3. Final Answer:

    You forgot to compile with javac MyProgram.java -> Option B
  4. 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

  1. Step 1: Compile the correct file

    To run File2, you must compile File2.java first using javac File2.java.
  2. Step 2: Run the compiled class properly

    Run the program with java File2 (without .java extension). Running before compiling or adding extensions causes errors.
  3. Final Answer:

    javac File2.java java File2 -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard