1Z0-811 Java Basics and Syntax Practice Question
Exhibit
$ java Test
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at Test.main(Test.java:3)Refer to the exhibit. What is the most likely cause?
⚠ Common exam trap
Oracle often tests the off-by-one error where candidates mistakenly think the last valid index is the array length (e.g., 5) instead of length-1 (e.g., 4), leading them to choose option B or misidentify the array size.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
The code tries to access index 5 of a 5-element array.
Java arrays are zero-indexed, meaning a 5-element array has valid indices 0 through 4. Accessing index 5 attempts to read beyond the array bounds, causing an ArrayIndexOutOfBoundsException at runtime. This is a classic off-by-one error where the code mistakenly uses the array length as the index.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The array has 6 elements.
Why it's wrong here
Exception says length 5, not 6.
- ✗
The code tries to access index 4 of a 5-element array.
Why it's wrong here
Index 4 is valid, would not cause exception.
- ✓
The code tries to access index 5 of a 5-element array.
Why this is correct
Indices 0-4 are valid; index 5 is out of bounds.
- ✗
The code has a syntax error.
Why it's wrong here
Syntax error would prevent compilation, not produce this exception.
Go deeper
Related to this question
About these practice questions
One of 481 original 1Z0-811 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.