5.9k views
1 vote
What are the legal indexes for the array ar, given the following declaration:

int[] ar= (2, 4, 6, 8, 10);
a. 1, 2, 3, 4, 5
b. 0,1,2,3,4
c. 2, 4, 6, 8, 10
d. 0, 2, 4, 6,8

User Addohm
by
7.9k points

1 Answer

0 votes

Final answer:

The legal indexes for the declared array are 0,1,2,3,4 because array indexing in Java starts at 0.

Step-by-step explanation:

The legal indexes for the array ar, given the declaration int[] ar = {2, 4, 6, 8, 10};, are b. 0,1,2,3,4. In Java and many other programming languages, array indexing starts at 0, not 1. Therefore, the index of the first element (2) is 0, the second element (4) is 1, and so on up to the fifth element (10) which is at index 4.

User Vikranth
by
8.8k points