Recursion

AP Computer Science A· difficulty 4/5

public static void f(int n) {
    if (n > 3) return;
    System.out.print(n + " ");
    f(n + 1);
    System.out.print(n + " ");
}
f(1);

What is printed?

  • A

    1 2 3 3 2 1

    check_circle
  • B

    1 2 3 1 2 3

  • C

    3 2 1 1 2 3

  • D

    1 1 2 2 3 3

Explanation

First print descends 1,2,3. Then as recursion unwinds, prints 3,2,1.

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

Related questions