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");
}
  1. A. 0 1 2 3 done
  2. B. 3 2 1 done
  3. C. done 3 2 1 0
  4. 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

Exams that ask this topic