ProgrammingHardTCS NQT · Infosys
What is the digit sum of 2^15 = 32768? The recursion below computes it. Assume int is 32 bits. What is printed?
What is the digit sum of 2^15 = 32768? The recursion below computes it. Assume int is 32 bits. What is printed?
#include <stdio.h>
int ds(int n) {
return n == 0 ? 0 : n % 10 + ds(n / 10);
}
int main(void) {
printf("%d", ds(1 << 15));
}- A. 25
- B. 27
- C. 26
- D. 23
Correct answer: C. 26
With a 32-bit int, 1 << 15 = 32768, and ds adds n % 10 before recursing on n / 10: 8 + 6 + 7 + 2 + 3 = 26. 23 drops the leading 3, and 25 or 27 are addition slips.
More C Programming questions
- How many subsets of {1, 2, 3} (the empty set included) have a sum divisible by 3? The recursion below counts them. What is printed? c…
- How many bit positions are zero in both 0xAAAA and 0x5555 within 16 bits? Count them with this program. c #include <stdint.h> #include…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = 12; printf("%u", (x << 3) + (x << 1)); }
- How many adjacent pairs of 1 bits does 27 (binary 11011) have? The program counts the set bits of x & (x >> 1). c #include <stdio.h> int…
- What is printed? c #include <stdio.h> int main(void) { int b[] = {1, 1, 0, 1, 1, 1, 0, 1, 1, 1}, run = 0, best = 0; for (int i = 0; i <…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 3, 4, 5}; for (int i = 0; i + 1 < 5; i += 2) { int t = a[i]; a[i]…