0
0
Javaprogramming~15 mins

Accessing arguments in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
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.
touch_appClick 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.
touch_appClick 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.
touch_appClick 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.
touch_appClick 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.
touch_appClick 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)
What type is the parameter that holds command-line arguments in the main method?
AString
Bint[]
CList<String>
DString[]
If no command-line arguments are passed, what is the length of args in main(String[] args)?
A0
B1
Cnull
DUndefined
What exception is thrown if you access args[5] but only 3 arguments were passed?
AArrayIndexOutOfBoundsException
BNullPointerException
CIllegalArgumentException
DClassCastException
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
Explain how to access and use command-line arguments in a Java program.
What errors might occur when accessing command-line arguments and how can you avoid them?