98.6k views
3 votes
Suppose I have int b = new int[42].What are the highest and lowest legal array indexes for b?

User Gradyetc
by
8.5k points

1 Answer

5 votes

Final answer:

The lowest legal array index for int b = new int[42] is 0, and the highest is 41. Accessing indexes outside of this range can lead to an exception.

Step-by-step explanation:

When declaring an array such as int b = new int[42], you're creating an array that can hold 42 integer values. The indexes of an array in most programming languages, including Java, start at 0 and end at the array's length minus one. Therefore, the lowest legal array index for b would be 0, and the highest legal array index would be 41. It's crucial to remember that attempting to access an index outside of this range will result in an ArrayIndexOutOfBoundsException.

User Niteya Shah
by
8.2k points