CJCoding With Joseph
15free / daySign inCreate account

Question 2

Lab 3 Exceptions - SM 2025

What exception will the following source code throw? Why?

for (int i = 0; i <= myArray.length; i++) {
    System.out.println(myArray[i]);
}


Short Answer

This code will throw an ArrayIndexOutOfBoundsException because it tries to access an index equal to myArray.length, which is outside the valid range of the array.

Keep reading this lesson

You've reached the end of the free preview. Unlock the rest of this lesson plus 54 more lessons in Thompson Rivers University.

  • Unlimited coding questions
  • Unlimited access to all classroom material
  • Weekly live call for questions and guidance
  • Mock exams marked by hand, with feedback
  • 3,000 AI tutor tokens / month
  • Personal SQL database (3 MB)
  • Progress tracking

Cancel anytime.