Recursion

AP Computer Science A· difficulty 4/5

public static void p(int n) {
    if (n == 0) return;
    System.out.print("A" + n + " ");
    p(n - 1);
    System.out.print("B" + n + " ");
}
p(2);

What is printed?

  • A

    A2 A1 B1 B2

    check_circle
  • B

    A1 A2 B2 B1

  • C

    A2 B2 A1 B1

  • D

    B1 B2 A1 A2

Explanation

A2 prints, then call p(1) prints A1, then base case returns, then B1 prints, then B2.

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

Related questions