Courseiva
Arrays and MethodshardMultiple ChoiceObjective-mapped

1Z0-811 Arrays and Methods Practice Question

Exhibit

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
    at ArrayExample.main(ArrayExample.java:6)

Refer to the exhibit. The code at line 6 of ArrayExample.java is: int[] arr = {10, 20, 30, 40, 50}; int sum = 0; for (int i = 0; i <= arr.length; i++) sum += arr[i]; Which change fixes the exception?

⚠ Common exam trap

The most common mistake in array iteration is using `<=` instead of `<`. Since arrays are zero-indexed, the last valid index is `arr.length - 1`. Using `<=` with array length causes an `ArrayIndexOutOfBoundsException` because it tries to access `arr[arr.length]`.

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 loop condition to i < arr.length

The exception occurs because the loop condition `i <= arr.length` causes the loop to iterate one index beyond the array's last valid index (arr.length - 1). When `i` equals `arr.length` (5), `arr[5]` is accessed, which throws an ArrayIndexOutOfBoundsException. Changing the condition to `i < arr.length` ensures the loop runs only for indices 0 through 4, fixing the exception. Option D, changing `arr.length` to `arr.length - 1` in the condition, also technically fixes the exception, but it is not the standard or recommended fix; the intended correct answer is to use the `<` operator.

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 array declaration to int[] arr = new int[6];

    Why it's wrong here

    Changing the array declaration to int[] arr = new int[6]; does not fix the exception because the loop still iterates up to index 5, causing an ArrayIndexOutOfBoundsException. The array initializer still has 5 elements, so arr[5] is out of bounds.

  • Change loop initialization to i = 1

    Why it's wrong here

    Changing the loop initialization to i = 1 avoids the exception for index 0, but still causes an exception when i reaches arr.length (5). The loop condition is still i <= arr.length, so it will attempt arr[5] and throw an ArrayIndexOutOfBoundsException.

  • Change loop condition to i < arr.length

    Why this is correct

    Changing the loop condition to i < arr.length ensures that the loop runs only for indices 0 through arr.length-1 (0-4), which are all valid. This correctly fixes the off-by-one error and prevents the ArrayIndexOutOfBoundsException.

  • Change arr.length to arr.length - 1

    Why this is correct

    Changing arr.length to arr.length - 1 in the condition results in i <= arr.length - 1, which does technically fix the exception for this specific array, but it is not the recommended fix because it still uses the <= operator and can be confusing. The standard approach is to use i < arr.length. Additionally, if the array length were zero, this condition would not work properly. Therefore, option D is considered incorrect in this exam.

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 →

How Courseiva writes practice questions · Editorial policy

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.