Home / Questions / Which code fragment will throw an ArrayIndexOutOfBoundsException?
Explanatory Question

Which code fragment will throw an ArrayIndexOutOfBoundsException?

👁 40 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


a. // Discussed in previous question
b. System.out.print(args.length);
c. for (int i = 0; i < 1; i++) {
       System.out.print(args[i]);
   }
d. None of the above

  • b prints the length of the array, which is safe.

  • c may throw an exception if args[] has length 0, because accessing args[0] is out of bounds.

  • Correct answer: c