ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int f(int n) {
static int depth = 0;
depth++;
if (n <= 1) return depth;
int a = f(n - 1);
int b = f(n - 2);
return a + b;
}
int main(void) {
int r = f(4);
printf("%d", r);
}- A. 32
- B. 5
- C. 24
- D. 9
Correct answer: A. 32
depth is shared by every call and counts calls made so far, and each base case returns that count. Calls in order: f4(1), f3(2), f2(3), f1(4) → 4, f0(5) → 5, so f2 = 9; f1(6) → 6, so f3 = 15; f2(7) has f1(8) → 8 and f0(9) → 9, so 17; f4 = 15 + 17 = 32. '9' is the number of calls, and '5' the Fibonacci-style answer.
More C Programming questions
- What is printed? c char s[] = "hello world"; char p = strchr(s, 'o'), q = strrchr(s, 'o'); q = '0'; printf("%s %zu %td", s, strlen(p), q…
- What is printed? c unsigned p = 0xB5; p ^= p >> 4; p ^= p >> 2; p ^= p >> 1; printf("%u", p & 1);
- Assume 2-byte short, 4-byte int and natural alignment. What is printed? c #include <stddef.h> struct R { char a; short b; char c; int d…
- What is printed? c int sum(int (r)[3], int n) { int s = 0; for (int i = 0; i < n; i++) s += r[i][i % 3]; return s; } int main(void) { int…
- What is printed? c int even(int n); int odd(int n) { return n == 0 ? 0 : even(n - 1); } int even(int n) { return n == 0 ? 1 : odd(n - 1)…
- What is printed? c char w[] = {"pear", "fig", "apple", "kiwi"}; for (int i = 0; i < 3; i++) for (int j = 0; j < 3 - i; j++) if…