public static int sumAll(int[] a) {
int total = 0;
for (int i = 0; i <= a.length; i++) {
total += a[i];
}
return total;
}What error occurs when this method runs?
- A
NullPointerException on the first iteration
- Bcheck_circle
ArrayIndexOutOfBoundsException on the last iteration
- C
There is no error
- D
The method returns one less than the correct sum
Explanation
Valid indices are 0 through a.length-1. Using <= a.length attempts to access a[a.length], which throws ArrayIndexOutOfBoundsException.