Recursion

AP Computer Science A· difficulty 3/5

public static int pow(int b, int e) {
    if (e == 0) return 1;
    return b * pow(b, e - 1);
}
System.out.println(pow(2, 5));

What is printed?

  • A

    16

  • B

    25

  • C

    32

    check_circle
  • D

    10

Explanation

2^5 = 32. Each recursive call multiplies by b until e hits 0.

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

Related questions