Recursion

AP Computer Science A· difficulty 4/5

What does this print for n=3?

<code>void p(int n) { if (n == 0) return; p(n - 1); System.out.print(n); p(n - 1); } </code></pre>

  • A

    1213121

    check_circle
  • B

    1

  • C

    123

  • D

    321

Explanation

Recursive in-order pattern: p(2)+3+p(2) → ((p(1)+2+p(1))+3+(p(1)+2+p(1))) → 1213121.

Want 10 more like this — adaptive to your weak spots?

Related questions

AP Computer Science A · Recursion Practice Question | Acemy