Recursion

AP Computer Science A· difficulty 3/5

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

What is printed (and returned)?

  • A

    3

  • B

    3 2 1 0

    check_circle
  • C

    0 1 2 3

  • D

    3 2 1

Explanation

Each call prints n then recurses. n goes 3,2,1,0. Returns 0.

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

Related questions