What is the last valid index of an array declared as int a[10] in C++?
Options
- A. 10
- B. 9
- C. 8
- D. More than one of the above
- E. None of the above
Quiz Practice:
B. 9
Answer: (B) 9
An array declared as int a[10] contains 10 elements.
Since C++ uses zero-based indexing, the indexes are:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Therefore, the last valid index is 9.
A useful formula is:
Last index = number of elements − 1