ProgrammingHardProduct company OA

What is printed?

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

Correct answer: B. 2 2 3

i / 2 is i shifted right by one bit and i % 2 is the bit dropped, so a[i] counts the 1 bits of i using earlier answers. 5 = 101 → 2, 6 = 110 → 2, 7 = 111 → 3.

More C Programming questions

Exams that ask this topic