ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
void countdown(int n) {
if (n < 0) return;
printf("%d ", n);
countdown(n - 1);
}
int main(void) {
countdown(3);
printf("done");
}- A. 0 1 2 3 done
- B. 3 2 1 done
- C. done 3 2 1 0
- D. 3 2 1 0 done
Correct answer: D. 3 2 1 0 done
countdown prints n then recurses with n-1, continuing until n becomes negative (base case). It prints 3, 2, 1, 0 as it descends, and since there is no code after the recursive call in this version (nothing runs on the way back up), only "done" is printed afterward in main, once the whole recursive chain fully returns.
More C Programming questions
- What is printed? c int f(int n) { if (n <= 0) return 0; (n)--; return 1 + f(n); } int main(void) { int x = 5; printf("%d %d", f(&x), x); }
- Assume a 64-bit LP64 ABI with natural alignment (no #pragma pack). What is printed? c struct S { char a; int b; char c; }; printf("%zu"…
- Assume a 64-bit LP64 ABI with natural alignment. What is printed? c struct S { int a; char b; int c; }; printf("%zu", sizeof(struct S));
- Assume a 64-bit LP64 ABI with #pragma pack(1) applied to this struct only. What is printed? c #pragma pack(push, 1) struct Packed { char…
- Assume a 64-bit LP64 ABI. What is printed? c struct A { char x; }; struct B { struct A a; char y; }; struct C { struct B b; char z; }…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; struct Point p1 = {1, 2}; struct Point p2 = p1; p2.x = 99…