ProgrammingHardInfosys · Wipro
What is printed?
What is printed?
#include <stdio.h>
int calls = 0;
int f(int n) {
calls++;
if (n < 2) return n;
return f(n - 1) + f(n - 2);
}
int main(void) {
int r = f(6);
printf("%d %d", r, calls);
}- A. 8 15
- B. 13 25
- C. 8 25
- D. 8 24
Correct answer: C. 8 25
f(6) returns fib(6) = 8. Every call costs 1 + calls(n-1) + calls(n-2) with calls(0) = calls(1) = 1, giving 25 calls in total; counting only the internal nodes or using fib(7) gives the traps.
More C Programming questions
- A struct R has int16t score and uint8t id. After qsort with cmp below, what is printed? c #include <stdint.h> #include <stdio.h> #include…
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint16t x = (uint16t)~0x00F0u; int c = 0; for (; x; x &= x - 1)…
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint8t v = 0x81; v = (uint8t)((v >> 1) | (v << 7))…
- How many times does the loop body run? c #include <stdio.h> int main(void) { unsigned mask = 0x2D, n = 0; for (unsigned s = mask; s; s =…
- Assume unsigned int is 32 bits. What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint32t a = 70000, b = 70000…
- What is printed? c #include <stddef.h> #include <stdio.h> int main(void) { sizet n = 3; int k = -2; printf("%zu %d", (sizet)(k + n), (n +…