✏️ Explanatory Question

Which of the following code fragments will throw an ArrayIndexOutOfBoundsException?

👁 35 Views
📘 Detailed Answer
🔴 Hard
💡

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.