ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int c = 0;
int f(int n) {
c++;
return n > 0 && (f(n - 1) || f(n - 2));
}
int main(void) {
int r = f(4);
printf("%d %d", r, c);
}- A. 0 9
- B. 1 15
- C. 1 9
- D. 0 15
Correct answer: D. 0 15
Any n ≤ 0 returns 0, so by induction every call returns 0, and the || never short-circuits: both f(n − 1) and f(n − 2) always run. Calls: C(n) = 1 for n ≤ 0, else 1 + C(n − 1) + C(n − 2): C(1) = 3, C(2) = 5, C(3) = 9, C(4) = 15.
More C Programming questions
- What is printed? c char buf[32], p = buf; for (int i = 1; i <= 4; i++) p += sprintf(p, "%d,", i i); p[-1] = '0'; printf("%s %td", buf, p…
- How many swaps does this selection sort perform? c int a[] = {3, 1, 2, 5, 4}, sw = 0; for (int i = 0; i < 4; i++) { int m = i; for (int j…
- How many element shifts (executions of a[j + 1] = a[j]) does this insertion sort make? c int a[] = {4, 3, 2, 1}, shifts = 0; for (int i =…
- How many passes of the outer loop run? c int a[] = {2, 3, 4, 5, 1}, passes = 0; for (int sw = 1; sw; ) { sw = 0; passes++; for (int j = 0…
- What is printed? c int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { int t =…
- What is printed? c int x = 5; x = x << 2 >> 1; printf("%d", x);