Recursion

AP Computer Science A· difficulty 2/5

public static int fact(int n) {
  if (n <= 1) return 1;
  return n * fact(n - 1);
}

How many recursive calls (excluding the initial) does fact(5) make?

  • A

    3

  • B

    4

    check_circle
  • C

    5

  • D

    6

Explanation

fact(5) calls fact(4), then fact(3)..fact(1), totaling 4 recursive calls.

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

Related questions