ProgrammingHardProduct company OA

What is printed?

What is printed?
struct N { int v, next; } t[] = {{10, 2}, {20, -1}, {30, 4}, {40, 1}, {50, 1}};
int s = 0;
for (int i = 0; i != -1; i = t[i].next) s += t[i].v;
printf("%d", s);
  1. A. 90
  2. B. 110
  3. C. 150
  4. D. 60

Correct answer: B. 110

The next fields form a linked list stored in an array: 0 → 2 → 4 → 1 → end. That visits 10 + 30 + 50 + 20 = 110. Node 3 (40) is never reached because nothing links to it; '150' sums the whole array.

More C Programming questions

Exams that ask this topic