0
0
Javaprogramming~15 mins

Syntax for command line arguments in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Command Line Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
What is the output of this Java program with command line arguments?

Consider the following Java program. What will it print if run with arguments apple banana?

Java
public class Main {
    public static void main(String[] args) {
        System.out.println(args[0] + " and " + args[1]);
    }
}
Aapple and banana
Bargs[0] and args[1]
Capplebanana
DArrayIndexOutOfBoundsException
Attempts:
2 left
💻 code output
intermediate
2:00remaining
What happens if no command line arguments are passed?

What will this Java program print if run without any command line arguments?

Java
public class Main {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("No arguments");
        } else {
            System.out.println(args[0]);
        }
    }
}
ANo arguments
Bnull
CArrayIndexOutOfBoundsException
Dargs[0]
Attempts:
2 left
💻 code output
advanced
2:00remaining
What is the output when printing all command line arguments?

What will this Java program print if run with arguments one two three?

Java
public class Main {
    public static void main(String[] args) {
        for (String arg : args) {
            System.out.print(arg + "-");
        }
    }
}
A-eerht-owt-eno
Bone-two-three-
Cone two three
Done-two-three
Attempts:
2 left
💻 code output
advanced
2:00remaining
What error occurs when accessing args without checking length?

What error will this Java program throw if run without any command line arguments?

Java
public class Main {
    public static void main(String[] args) {
        System.out.println(args[0]);
    }
}
ACompilation error
BNullPointerException
CNo output, program ends silently
DArrayIndexOutOfBoundsException
Attempts:
2 left
🧠 conceptual
expert
3:00remaining
How to correctly parse integer command line arguments in Java?

You want to read two integer numbers from command line arguments and add them. Which code snippet correctly does this?

Aint a = Integer.valueOf(args[0]); int b = Integer.valueOf(args[1]); System.out.println(a + b);
Bint a = (int) args[0]; int b = (int) args[1]; System.out.println(a + b);
Cint a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.println(a + b);
Dint a = args[0]; int b = args[1]; System.out.println(a + b);
Attempts:
2 left