ProgrammingHardAccenture · Cognizant pattern
What is printed?
What is printed?
int depth = 0;
void f(int n) {
depth++;
if (n == 0) return;
f(n - 1);
}
int main() {
f(3);
printf("%d", depth);
}- A. 4
- B. 3
- C. 1
- D. 0
Correct answer: A. 4
depth is a global, incremented once at the START of every call, before checking the base case. f(3) calls f(2),f(1),f(0) — that's 4 total invocations (n=3,2,1,0), each incrementing depth once: final depth is 4.
More C Programming questions
- 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"…
- Assume a 64-bit LP64 ABI. What is printed? c int a[4] = {1, 2, 3, 4}; int p = a + 3; int q = a; printf("%ld %d %d", (long)(p - q), p, (p…
- Assume a 64-bit LP64 ABI. What is printed? c void swap(int a, int b) { int tmp = a; a = b; b = tmp; } int main(void) { int x = 1, y = 2…