Recursion

AP Computer Science A· difficulty 4/5

What does this print for n=4?

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

  • A

    4321

  • B

    1234

    check_circle
  • C

    4

  • D

    0

Explanation

Recurse first (depths to base), then print on the way back up: 1, 2, 3, 4.

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

Related questions