ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int f(int n) {
if (n <= 1) return 1;
return n * f(n - 1);
}
int main() { printf("%d", f(5)); }- A. 120
- B. 24
- C. 15
- D. Infinite recursion
Correct answer: A. 120
This is a standard factorial recursion: f(5)=5*f(4)=5*4*f(3)=...=5*4*3*2*1=120. Each call's multiplication is deferred until the recursive call returns, so the multiplications happen in reverse (unwinding) order but the final product is the same regardless.
More C Programming questions
- What is printed? c int f(int n) { if (n == 0) return 0; int x = f(n - 1); printf("%d ", n); return x + n; } int main() { printf("= %d"…
- What is printed? c int depth = 0; void f(int n) { depth++; if (n == 0) return; f(n - 1); } int main() { f(3); printf("%d", depth); }
- What is printed? c void f(int n) { if (n <= 0) return; printf("%d ", n); if (n % 2 == 0) f(n / 2); else f(n - 1); } int main() { f(6); }
- Assume a 64-bit LP64 ABI. What is printed? c int x = 5, y = 10, z = 15; int arr[3] = {&x, &y, &z}; int p = arr; printf("%d %d %d", arr[1]…
- Assume a 64-bit LP64 ABI. Which declaration makes p an array of 5 pointers to int, and which makes q a pointer to an array of 5 ints?
- Assume a 64-bit LP64 ABI. What is printed? c int mat[2][3] = {{1, 2, 3}, {4, 5, 6}}; int (rowPtr)[3] = mat; printf("%d %d %d %zu"…