CJCoding With Joseph
15free / daySign inCreate account

Thompson Rivers University

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.

Members only

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
  • 3,000 AI tutor tokens / month
  • Personal SQL database (3 MB)
  • Progress tracking

Cancel any time.