1. Home
  2. Computer Science MCQs
  3. C Programming
  4. For int a[5] = {1, 2}; what value does a[4] hold in C?

Added 30 Nov -0001

For int a[5] = {1, 2}; what value does a[4] hold in C?

  1. Garbage values from the previous stack contents
  2. 2, because the last value is repeated
  3. 1, because the initialiser wraps around
  4. 0, because the remaining elements are zeroed Correct answer

Correct answer

D. 0, because the remaining elements are zeroed

14 views

MCQSEXAM

Explanation

When an initialiser list is shorter than the array, C zero-initialises every remaining element, so a[2], a[3] and a[4] all become 0. This applies to static and automatic arrays alike, unlike an uninitialised local array whose elements hold indeterminate values. The rule is why writing int a[100] = {0}; is a reliable way to clear a whole array.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs