ProgrammingHardTCS NQT · Product company OA

What is printed?

What is printed?
int a[] = {3, 5, 0, 7}, i = 0, s = 0, v;
while ((v = a[i++])) s += v;
printf("%d %d", s, i);
  1. A. 8 3
  2. B. 8 2
  3. C. 15 4
  4. D. 8 4

Correct answer: A. 8 3

The loop stops at the first 0, so the 7 is never added: s = 3 + 5 = 8. i is incremented even on the read that returns 0, so it ends at 3, one past the zero's index.

More C Programming questions

Exams that ask this topic