Challenge - 5 Problems
Command Line Java Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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 + " "); } } }
Attempts:
2 left
💡 Hint
Remember that command line arguments are passed as an array of strings to main.
✗ Incorrect
The program prints the number of arguments, which is 2, then prints each argument separated by a space.
🧠 Conceptual
intermediate1: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?Attempts:
2 left
💡 Hint
Array indexing in Java starts at zero.
✗ Incorrect
args[0] is "one", args[1] is "two", args[2] is "three".
🔧 Debug
advanced2: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]); } }
Attempts:
2 left
💡 Hint
Accessing an array element that does not exist causes an exception.
✗ Incorrect
args is an empty array when no arguments are passed, so args[0] access throws ArrayIndexOutOfBoundsException.
📝 Syntax
advanced1: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?Attempts:
2 left
💡 Hint
Remember that
javac compiles and java runs the class without the .java extension.✗ Incorrect
Option A compiles first, then runs the class with arguments. Others misuse commands or order.
🚀 Application
expert2: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); } }
Attempts:
2 left
💡 Hint
The program converts each argument to a number and adds them.
✗ Incorrect
3 + 5 + 2 equals 10, so the program prints 10.