ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int count = 0;
void f(int n) {
count++;
if (n <= 1) return;
f(n - 1);
f(n - 2);
}
int main(void) { f(4); printf("%d", count); }- A. 8
- B. 9
- C. 7
- D. 15
Correct answer: B. 9
Counting all invocations of f(4): f(4)->f(3),f(2); f(3)->f(2),f(1); f(2)->f(1),f(0); each of these base calls (n<=1) also counts. Total invocations: f(4),f(3),f(2),f(1),f(0),f(2),f(1),f(0),f(1) = 9 calls.
More C Programming questions
- What is printed? c int f(int n, int d) { if (d > n) return 0; if (n % d == 0) return d + f(n, d + 1); return f(n, d + 1); } int main(void)…
- What is printed? Assume no stack overflow for this depth. c int f(int n) { if (n == 0) return 0; if (n % 3 == 0) return 1 + f(n - 3)…
- What is printed? c int f(int n) { if (n == 0) return 0; int r = f(n - 1); printf("%d ", n); return r; } int main(void) { int x = f(4)…
- What is printed? c union U { char c; int i; double d; }; printf("%zu", sizeof(union U));
- Assume a 64-bit LP64 ABI. What is printed? c struct Inner { char c; int i; }; struct Outer { int x; struct Inner in; double d; }…
- On a typical x86-64 GCC/Clang target, what is printed? c struct F { unsigned a : 20; unsigned b : 20; }; printf("%zu", sizeof(struct F));