Home / Questions / Which of the following code fragments will throw an ArrayIndexOutOfBoundsException?
Explanatory Question

Which of the following code fragments will throw an ArrayIndexOutOfBoundsException?

👁 32 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation


for (int i = 0; i <= args.length; i++) {
    System.out.print(i);
}

Explanation:
The loop runs while i <= args.length, so when i equals args.length, it goes beyond the valid array index, causing an ArrayIndexOutOfBoundsException.