J.I · Java 25 practice question
A developer runs the following Java 25 program to update an element in a scores array. What happens at run time?
void main() {
int[] scores = {10, 20};
int idx = 2;
scores[idx] += 5;
System.out.println(scores[0]);
}
Check answer and explanation
Correct answer: ArrayIndexOutOfBoundsException
Explanation
Because idx equals the array length of 2, no assignment takes place and ArrayIndexOutOfBoundsException is thrown.
Common trap
Java arrays have fixed lengths and never resize automatically on out-of-bounds compound assignment. Out-of-bounds array access does not fail silently; it throws an exception immediately.
Source: JLS SE 25 §15.26.2. Compound Assignment Operators
Checked against the cited text on 2026-11-25.