ProgrammingHardGATE CS · Competitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
unsigned fib_calls = 0;
int fib(int n) {
fib_calls++;
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
}
int main(void) {
fib(10);
printf("%u", fib_calls);
}- A. 177
- B. 176
- C. 89
- D. 178
Correct answer: A. 177
Each call to fib(n) makes 2 * fib(n + 1) - 1 calls in total, so for n = 10 it is 2 * 89 - 1 = 177. The value 89 is fib(10) + 1, not the call count.
More C Programming questions
- What is printed for the sorted array below? c #include <stdio.h> int main(void) { int a[] = {1, 3, 3, 3, 5, 8}; int n = 6, lo = 0, hi = n…
- What is printed? c #include <stdio.h> int find(const char hay, const char nd) { for (const char p = hay; p; p++) { const char a = p, b =…
- What is printed? Assume int is 32 bits. c #include <stdio.h> int main(void) { unsigned a = 10; int b = -3; printf("%u", a % b); }
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 40; int c = 0; while (!(x & 1u)) { x >>= 1; c++; } printf("%d", c); }
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 2, 3, 1, 4}; int c = 0; for (int i = 0; i < 5; i++) if (a[i] < a[i…
- For uint32t a = 5 and b = 3, how many of these statements are true? (i) a - b > a (ii) (a - b) + b == a (iii) ~a == UINT32MAX (iv) a / b b…