ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
int f(int n) {
if (n <= 0) return 0;
return n % 2 ? f(n - 1) + n : f(n / 2) * 2;
}
int main(void) {
printf("%d %d", f(10), f(7));
}- A. 20 17
- B. 18 15
- C. 18 17
- D. 10 13
Correct answer: C. 18 17
Trace: f(1) = f(0) + 1 = 1, f(2) = f(1)·2 = 2, f(4) = f(2)·2 = 4, f(5) = f(4) + 5 = 9, f(10) = f(5)·2 = 18. For 7: f(3) = f(2) + 3 = 5, f(6) = f(3)·2 = 10, f(7) = f(6) + 7 = 17.
More C Programming questions
- What is printed? c unsigned char x = 0x1D, r = 0; for (int i = 0; i < 8; i++) { r = (r << 1) | (x & 1); x >>= 1; } printf("%#x", r);
- What is printed? c int a = 2, b = 3, c = 4; printf("%d %d %d", a + b c % 5 - -a, a < b < c, c > b > a);
- What is printed? c char w[] = {"red", "green", "blue"}; char p = w + 1; printf("%c %s %c %zu", p, (p + 1) + 2, ((p - 1) + 1), strlen(p[1])…
- What is printed? c struct { unsigned a : 3; unsigned b : 5; } f = {0, 0}; f.a = 9; f.b = 33; printf("%u %u ", f.a, f.b); f.a += 7; f.b =…
- What is printed? c / 32-bit unsigned int / unsigned np2(unsigned v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >>…
- What is printed? c / 8-bit unsigned char, 32-bit int / unsigned char c = 0x80; printf("%d %d %d %d", c << 1, (unsigned char)(c << 1), ~c…