0
0
Javaprogramming~15 mins

Accessing arguments in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Argument Access 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?
Consider the following Java program that accesses command-line arguments. What will it print when run with arguments: java Main Hello World?
Java
public class Main {
    public static void main(String[] args) {
        System.out.println(args[0] + " " + args[1]);
    }
}
AHelloWorld
Bargs[0] args[1]
CHello World
DArrayIndexOutOfBoundsException
Attempts:
2 left
💻 code output
intermediate
2:00remaining
What happens if no arguments are passed?
What will be the output or result of running this Java program with no command-line arguments?
Java
public class Main {
    public static void main(String[] args) {
        System.out.println(args.length);
        System.out.println(args[0]);
    }
}
A0 followed by null
BCompilation error
C0 followed by 0
D0 followed by ArrayIndexOutOfBoundsException
Attempts:
2 left
🔧 debug
advanced
2:00remaining
Identify the error in accessing arguments
This Java program tries to print all command-line arguments but throws an error. What is the cause?
Java
public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= args.length; i++) {
            System.out.println(args[i]);
        }
    }
}
AArrayIndexOutOfBoundsException because loop index goes out of bounds
BNullPointerException because args is null
CCompilation error due to wrong loop syntax
DNo error, prints all arguments
Attempts:
2 left
📝 syntax
advanced
2:00remaining
Which option correctly accesses the last argument?
Given a Java program with command-line arguments, which code snippet correctly prints the last argument?
ASystem.out.println(args[-1]);
BSystem.out.println(args[args.length - 1]);
CSystem.out.println(args[args.length + 1]);
DSystem.out.println(args[args.length]);
Attempts:
2 left
🚀 application
expert
2:00remaining
What is the output of this program with arguments: java Main 5 10 15?
This Java program sums integer command-line arguments. What does it print when run with arguments 5 10 15?
Java
public class Main {
    public static void main(String[] args) {
        int sum = 0;
        for (String arg : args) {
            sum += Integer.parseInt(arg);
        }
        System.out.println(sum);
    }
}
A30
BNumberFormatException
CCompilation error
D51015
Attempts:
2 left