What is the type of the parameter used to access command-line arguments in a Java main method?
The parameter is an array of Strings, declared as String[] args. It holds all command-line arguments passed to the program.
Click to reveal answer
beginner
How do you access the first command-line argument in a Java program?
You access it using args[0], where args is the array of arguments passed to the main method.
Click to reveal answer
intermediate
What happens if you try to access an argument index that does not exist in args?
You get an ArrayIndexOutOfBoundsException because you are trying to access an index outside the array bounds.
Click to reveal answer
beginner
How can you check how many command-line arguments were passed to a Java program?
You can check the length of the args array using args.length.
Click to reveal answer
intermediate
Why is the main method declared as public static void main(String[] args) in Java?
Because the Java runtime calls this method without creating an object (hence static), it must be public to be accessible, and it receives command-line arguments as a String array.
Click to reveal answer
What is the correct way to access the third command-line argument in Java?
Aargs[3]
Bargs[2]
Cargs.get(3)
Dargs(2)
✗ Incorrect
Array indexing in Java starts at 0, so the third argument is at index 2.
What type is the parameter that holds command-line arguments in the main method?
AString
Bint[]
CList<String>
DString[]
✗ Incorrect
Command-line arguments are passed as an array of Strings: String[].
If no command-line arguments are passed, what is the length of args in main(String[] args)?
A0
B1
Cnull
DUndefined
✗ Incorrect
If no arguments are passed, args is an empty array with length 0.
What exception is thrown if you access args[5] but only 3 arguments were passed?
AArrayIndexOutOfBoundsException
BNullPointerException
CIllegalArgumentException
DClassCastException
✗ Incorrect
Accessing an invalid array index throws ArrayIndexOutOfBoundsException.
Why must the main method be static to access arguments?
ABecause static methods cannot take parameters
BBecause static methods can access instance variables
CBecause it is called by the JVM without creating an object
DBecause arguments are static variables
✗ Incorrect
The JVM calls main without creating an instance, so main must be static.
Explain how to access and use command-line arguments in a Java program.
Think about the main method signature and how to get each argument.
You got /4 concepts.
What errors might occur when accessing command-line arguments and how can you avoid them?
Consider what happens if you ask for an argument that was not passed.
You got /3 concepts.
Practice
(1/5)
1. What is the correct way to access the first command-line argument in a Java program inside the main method?
easy
A. args.first()
B. args[0]
C. args(0)
D. args.get(0)
Solution
Step 1: Understand the type of args
The args parameter in main is an array of strings.
Step 2: Access array elements in Java
Array elements are accessed using square brackets with the index inside, starting at 0.
Final Answer:
args[0] -> Option B
Quick Check:
Array access uses square brackets [OK]
Hint: Use square brackets to access array elements in Java [OK]
Common Mistakes:
Using parentheses instead of square brackets
Trying to call methods like first() on array
Using get() which is for lists, not arrays
2. Which of the following is the correct signature of the main method to access command-line arguments in Java?
easy
A. public void main(String[] args)
B. public static int main(String[] args)
C. static public void main(String args)
D. public static void main(String args[])
Solution
Step 1: Recall the standard main method signature
The main method must be public, static, return void, and accept a String array parameter.
Step 2: Check each option
public static void main(String args[]) matches the correct signature. public void main(String[] args) is missing static. static public void main(String args) has wrong parameter type. public static int main(String[] args) returns int which is invalid.
Final Answer:
public static void main(String args[]) -> Option D
Quick Check:
Main method must be public static void with String array [OK]
Hint: Main method must be public static void with String array [OK]
Common Mistakes:
Omitting static keyword
Wrong return type
Using single String instead of String array
3. What will be the output of the following Java program if run with arguments java Program Hello World?
public class Program {
public static void main(String[] args) {
System.out.println(args.length);
System.out.println(args[1]);
}
}
medium
A. 3
World
B. 2
Hello
C. 2
World
D. 3
Hello
Solution
Step 1: Count the command-line arguments
The program is run with two arguments: "Hello" and "World", so args.length is 2.
Step 2: Access the second argument
args[1] is the second argument, which is "World".
Final Answer:
2
World -> Option C
Quick Check:
args.length=2, args[1]=World [OK]
Hint: args.length counts arguments; args[1] is second argument [OK]
Common Mistakes:
Counting program name as argument
Using args[0] as second argument
Confusing args.length with number of arguments plus one
4. Identify the error in the following Java program that tries to print the first command-line argument:
public class Test {
public static void main(String[] args) {
System.out.println(args(0));
}
}
medium
A. Using parentheses instead of square brackets to access array element
B. Missing static keyword in main method
C. Incorrect class name
D. Missing return statement in main
Solution
Step 1: Check how array elements are accessed
In Java, array elements are accessed using square brackets, not parentheses.
Step 2: Identify the error in the code
The code uses args(0) which is invalid syntax and causes a compile error.
Final Answer:
Using parentheses instead of square brackets to access array element -> Option A
Quick Check:
Array access uses square brackets [OK]
Hint: Use square brackets [] for arrays, not parentheses () [OK]
Common Mistakes:
Using parentheses for array access
Confusing method call syntax with array access
Ignoring compile errors due to wrong syntax
5. You want to write a Java program that prints all command-line arguments separated by commas. Which code snippet correctly does this inside main?