1Z0-811 Control Flow and Loops Practice Question
Exhibit
int[] data = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i <= data.length; i++) {
sum += data[i];
}
System.out.println(sum);Given the code fragment:
```java int[] data = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i <= data.length; i++) {sum += data[i];
}
System.out.println(sum); ```
What is the result?
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
✓
ArrayIndexOutOfBoundsException
The loop condition i <= data.length causes i to become 5 when data.length is 5, but valid indices are 0-4. This results in an ArrayIndexOutOfBoundsException. Option A is wrong because the exception occurs before sum reaches 15. Option C is wrong because sum is not 0. Option D is wrong because the code compiles successfully.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
15
Why it's wrong here
The loop would attempt to access index 5, causing an exception before sum becomes 15.
- ✓
ArrayIndexOutOfBoundsException
Why this is correct
When i equals 5, data[5] is out of bounds, throwing an exception.
- ✗
0
Why it's wrong here
The sum is not 0; the loop runs and adds values until the exception.
- ✗
Compilation fails
Why it's wrong here
The code compiles without errors.
Go deeper
Related to this question
About these practice questions
This 1Z0-811 question is part of Courseiva's 481-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. 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.