ProgrammingHardGATE CS · Campus placements
int f(int n) { if (n == 0) return 0; return n % 2 + f(n / 2); } What is f(37)?
- A. 4
- B. 2
- C. 3
- D. 5
Correct answer: C. 3
f adds the lowest bit at each step, so it counts set bits. 37 = 100101 has three 1 bits, so f(37) = 3.
More C Programming questions
- For unsigned int i = 3; i >= 0; i--, what happens to the loop body?
- char a[] = "hi"; char b[] = "hi"; What does a == b evaluate to?
- What does printf("%d", '0' < '9') print?
- What does printf("%d", 6 % 4 2) print?
- Which expression computes ceil(a / b) for positive int a = 7 and b = 3 using integer arithmetic, written as ((a + b - 1) / b)?
- int a[3][4]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) a[i][j] = i 4 + j; printf("%d", (&a[0][0] + 7)); What is printed?