int[] a = {1, 2, 3, 4, 5};
for (int i = 1; i < a.length; i++) {
a[i] = a[i + 1];
}What happens?
- Acheck_circle
ArrayIndexOutOfBoundsException
- B
Prints 1 3 4 5 5
- C
Prints 1 2 3 4 5
- D
Compilation error
Explanation
The condition should be i < a.length - 1 to avoid accessing a[a.length]. As written, when i = 4, a[5] is out of bounds.