Recursion

AP Computer Science A· difficulty 3/5

public static int bad(int n) {
    if (n == 0) return 0;
    return n + bad(n + 1);
}
bad(1);

What happens?

  • A

    StackOverflowError

    check_circle
  • B

    Returns 0

  • C

    Returns 1

  • D

    Compile-time error

Explanation

The recursive call goes AWAY from the base case (n+1 instead of n-1), causing infinite recursion and stack overflow.

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

Related questions