Courseiva
Arrays and MethodsmediumMultiple ChoiceObjective-mapped

1Z0-811 Arrays and Methods Practice Question

Consider the following code:

int[] arr = new int[5];

for (int i = 0; i <= arr.length; i++) {

arr[i] = i;

}

System.out.println(arr[0]);

What is the result?

⚠ Common exam trap

The trap here is that candidates often overlook the off-by-one error in the loop condition `i <= arr.length` and assume the loop runs correctly, forgetting that array indices start at 0 and end at `length - 1`.

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

An ArrayIndexOutOfBoundsException is thrown.

The loop condition `i <= arr.length` causes `i` to iterate from 0 to 5 inclusive. Since `arr` has indices 0 through 4, accessing `arr[5]` throws an `ArrayIndexOutOfBoundsException`. The exception occurs before `arr[0]` can be printed.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • 0

    Why it's wrong here

    The exception occurs before printing, so 0 is not printed.

  • 5

    Why it's wrong here

    5 is never assigned because the exception occurs when i is 5.

  • 4

    Why it's wrong here

    4 would be printed if the loop ran successfully up to index 4, but it fails at index 5.

  • An ArrayIndexOutOfBoundsException is thrown.

    Why this is correct

    The loop runs i from 0 to 5 inclusive; when i equals 5, arr[5] is out of bounds, causing the exception.

About these practice questions

Courseiva writes every 1Z0-811 question from scratch — 481 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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.