1Z0-829 Controlling Program Flow Practice Question
Exhibit
Refer to the exhibit.
```
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at LoopExample.main(LoopExample.java:5)
```
Source code of LoopExample.java:
```java
1: public class LoopExample {
2: public static void main(String[] args) {
3: int[] arr = {1,2,3};
4: for (int i = 0; i <= arr.length; i++) {
5: System.out.println(arr[i]);
6: }
7: }
8: }
```Which change fixes the exception?
⚠ Common exam trap
Many exam-takers confuse the loop condition i <= arr.length with i < arr.length, not realizing that the former accesses an index one past the array's last element, causing an ArrayIndexOutOfBoundsException.
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
✓
Change line 4 to: for (int i = 0; i < arr.length; i++)
It changes the loop to start at index 0 and continue while i < arr.length, which correctly iterates over all valid indices (0 to arr.length-1). The original code likely used i <= arr.length, causing an ArrayIndexOutOfBoundsException when accessing arr[i] at i = arr.length. By using i < arr.length, the loop stops before reaching the out-of-bounds 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.
- ✓
Change line 4 to: for (int i = 0; i < arr.length; i++)
Why this is correct
Corrects the off-by-one error.
- ✗
Change line 4 to: for (int i = 1; i <= arr.length; i++)
Why it's wrong here
Starts at 1, misses arr[0] and still out of bounds at i=3.
- ✗
Change line 4 to: for (int i = 0; i <= arr.length-1; i++)
Why it's wrong here
Changing the loop condition to `i <= arr.length-1` does not fix the exception because it still iterates up to the last valid index; the original exception likely arose from accessing an index beyond the array's bounds, and this alteration merely rewrites the same boundary check without altering the loop's range. It is tempting because `<=` with `arr.length-1` is a common idiom for inclusive upper bounds, and would be correct if the loop needed to access every element including the last, but the scenario requires a different iteration pattern—such as stopping one element earlier—which this change does not achieve.
- ✗
Change line 5 to: System.out.println(arr[i-1]);
Why it's wrong here
Would print wrong elements and still go out of bounds for i=3? Actually i<=3, i=3: arr[2] okay, but i=3 still accesses arr[2]? No, i=3 arr[2] works but loop still goes to i=3? Wait line 4 condition i<=arr.length, with arr.length=3, i runs 0,1,2,3. i=3: arr[3] out of bounds. Changing to arr[i-1] would cause i=0 arr[-1] exception earlier. So not valid.
Go deeper
Related to this question
About these practice questions
One of 513 original 1Z0-829 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-829 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-829 exam.