Recursion

AP Computer Science A· difficulty 3/5

public static int power(int b, int e) {
    if (e == 1) return 1;
    return b * power(b, e - 1);
}

What is wrong?

  • A

    Nothing is wrong

  • B

    The recursive call uses the wrong operator

  • C

    There is no recursive step

  • D

    The base case should return b when e == 1, or 1 when e == 0

    check_circle

Explanation

Returning 1 when e == 1 makes power(b,1) = 1 instead of b. The base case should be if (e == 0) return 1; or return b when e == 1.

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

Related questions