64.2k views
0 votes
Suppose the following array is declared:

int[ ] grades = {88, 92, 95, 83};

What is the value of grades[grades.length-1]?

An ArrayIndexOutOfBoundsException occurs
88
92
82
83

User Barrrdi
by
6.1k points

1 Answer

4 votes
83.

grades.length will return the number of elements in the array, starting from 1, unlike when you actually use the array, where the index starts from 0.

This means grades.length will return 4, as there are 4 elements in the array. We are subtracting 1 from this number, meaning we are finding the value of grades[3], which would equal 83, as an array index starts from 0, where

88 = grades[0]
92 = grades[1]
82 = grades[2]
and 83 = grades[3]

User TallGuy
by
5.9k points