ProgrammingVery hardProduct company OA

What is printed?

What is printed?
int f(int n) {
    if (n == 0) return 0;
    int r = f(n - 1);
    printf("%d ", n);
    return r;
}
int main(void) { int x = f(4); printf("| %d", x); }
  1. A. 4 3 2 1 | 0
  2. B. 1 2 3 4 | 4
  3. C. 4 3 2 1 | 4
  4. D. 1 2 3 4 | 0

Correct answer: D. 1 2 3 4 | 0

Each frame recurses fully before printing its own n, so prints happen in increasing order as the stack unwinds from the deepest call back out: 1 2 3 4. The return value r is always whatever f(0) returned, which is 0, propagated unchanged through every frame.

More C Programming questions

Exams that ask this topic