Which code fragment will throw an ArrayIndexOutOfBoundsException?

Long Answer
Views 38

Answer:


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

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.