Difficulty: Easy
Correct Answer: Runtime Exception
Explanation:
Introduction / Context:
This question tests Java command-line argument indexing and what happens when you access an array element that does not exist. The code prints the value of args[3]
after assigning several variables from the args
array. Understanding zero-based indexing and the number of arguments passed is crucial.
Given Data / Assumptions:
Concept / Approach:
Java arrays are zero-indexed. If the length is 3, valid indices are 0, 1, and 2. Any attempt to access args[3]
when only three elements are present will throw ArrayIndexOutOfBoundsException
at runtime. This occurs before the print statement executes, so no normal output line is printed.
Step-by-Step Solution:
Verification / Alternative check:
If the invocation had four tokens after the class name (e.g., java Test a b c d), then args[3]
would be "d"
and the print would succeed. The provided invocation has only three arguments.
Why Other Options Are Wrong:
args[3]
to exist; it does not.
Common Pitfalls:
Forgetting zero-based indexing and assuming the first argument is args[1]
; or assuming Java silently ignores out-of-range accesses (it does not).
Final Answer:
Runtime Exception
Discussion & Comments