🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Recursion and Stack Frames: all 10 questions

Quiz: Recursion and Stack Frames · Question 1 of 10

What does this print? (code)

What does this print?

#include <stdio.h>
void f(int n) {
    if (n == 0) return;
    f(n - 1);
    printf("%d ", n);
}
int main(void) {
    f(4);
    return 0;
}
  1. A.4 3 2 1
  2. B.1 2 3 4
  3. C.4 3 2 1 0
  4. D.0 1 2 3 4
Answer & explanation

Correct answer: 1 2 3 4

The printf comes after the recursive call, so it runs while unwinding: the deepest call (n = 1) prints first.

Can you get the rest right without peeking?

Take the timed Quiz: Recursion and Stack Frames quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →