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]);- A. 1 2 3
- B. 2 2 3
- C. 3 2 3
- 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
- What is printed? c int a[] = {2, 1, 3, 2, 4, 1}, best = 0, s = 0, lo = 0; for (int hi = 0; hi < 6; hi++) { s += a[hi]; while (s > 6) s -=…
- What is wrong with this code? c char p = malloc(10); p++; free(p);
- What is printed? c int x = 5; #define x (x + 1) printf("%d", x);
- What is printed? c int c = 0; for (int i = 1; i <= 16; i = 2) for (int j = 0; j < i; j++) c++; printf("%d", c);
- What is printed? c int i, c = 0; for (i = 0; i < 10; i += i + 1) c++; printf("%d %d", c, i);
- What is printed? c int a = 2, b = 4; printf("%d %d %d", a & b, a && b, a | b);