GATE Computer ScienceHardGATE CSE

void p(int n) { if (n <= 0) return; printf("%d ", n); p(n - 2); p(n - 3); printf("%d ", n); } What does p(6) print?

  1. A. 6 4 2 2 4 3 3 6
  2. B. 6 4 2 2 1 1 4 3 1 1 3 6
  3. C. 6 4 2 2 4 1 1 3 3 6
  4. D. 6 4 2 2 1 1 4 3 3 6

Correct answer: B. 6 4 2 2 1 1 4 3 1 1 3 6

p(6) prints 6, calls p(4) and p(3); expanding the calls gives: 6 4 2 2 1 1 4 3 1 1 3 6.

More Programming and Data Structures (GATE) questions

Exams that ask this topic