← Quiz: Java Arrays: all 10 questions
Quiz: Java Arrays · Question 7 of 10
int[] a = new int[5]; What happens at a[a.length] = 1;?
int[] a = new int[5]; What happens at a[a.length] = 1;?
- A.The array grows to 6 elements
- B.Compile error
- C.The value is stored in the last element
- D.ArrayIndexOutOfBoundsException at run time
Answer & explanation
Correct answer: ArrayIndexOutOfBoundsException at run time
Valid indexes are 0 to 4. The compiler does not check index values, so the error appears when the line runs.
Can you get the rest right without peeking?
Take the timed Quiz: Java Arrays quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →