0
0
Javaprogramming~20 mins

Command line execution in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Command Line Java Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Java program with command line arguments
What is the output of this Java program when run with arguments hello world?
Java
public class Main {
    public static void main(String[] args) {
        System.out.println(args.length);
        for (String arg : args) {
            System.out.print(arg + " ");
        }
    }
}
A
0
B
1
hello world 
C
2
hello
world
D
2
hello world 
Attempts:
2 left
💡 Hint
Remember that command line arguments are passed as an array of strings to main.
🧠 Conceptual
intermediate
1:30remaining
Understanding Java command line argument indexing
If a Java program is run with the command java Program one two three, what is the value of args[1] inside main?
AIndexOutOfBoundsException
B"two"
C"three"
D"one"
Attempts:
2 left
💡 Hint
Array indexing in Java starts at zero.
🔧 Debug
advanced
2:00remaining
Identify the error when running Java program with no arguments
What error occurs when running this Java program with no command line arguments?
Java
public class Test {
    public static void main(String[] args) {
        System.out.println(args[0]);
    }
}
ANoSuchElementException
BNullPointerException
CArrayIndexOutOfBoundsException
DCompilation error
Attempts:
2 left
💡 Hint
Accessing an array element that does not exist causes an exception.
📝 Syntax
advanced
1:30remaining
Correct command to compile and run Java program with arguments
Which command correctly compiles and runs a Java program named App.java with command line arguments foo bar?
Ajavac App.java && java App foo bar
Bjava App.java foo bar
Cjavac App.java foo bar && java App
Djava App foo bar && javac App.java
Attempts:
2 left
💡 Hint
Remember that javac compiles and java runs the class without the .java extension.
🚀 Application
expert
2:30remaining
Determine output of Java program processing command line arguments
What is the output of this Java program when run with arguments 3 5 2?
Java
public class SumArgs {
    public static void main(String[] args) {
        int sum = 0;
        for (String arg : args) {
            sum += Integer.parseInt(arg);
        }
        System.out.println(sum);
    }
}
A10
B352
C0
DNumberFormatException
Attempts:
2 left
💡 Hint
The program converts each argument to a number and adds them.