Recursion

AP Computer Science A· difficulty 3/5

public static int mystery(int n) {
    if (n <= 1) return 1;
    return n * mystery(n - 2);
}
// Call: System.out.println(mystery(7));

What is printed?

  • A

    1

  • B

    105

    check_circle
  • C

    21

  • D

    5040

Explanation

mystery(7) = 7 * mystery(5) = 7 * 5 * mystery(3) = 7 * 5 * 3 * mystery(1) = 7 * 5 * 3 * 1 = 105.

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

Related questions