Recursion

AP Computer Science A· difficulty 3/5

public static int fib(int n) {
    if (n <= 1) return n;
    return fib(n - 1) + fib(n - 2);
}
System.out.println(fib(5));

What is printed?

  • A

    13

  • B

    5

    check_circle
  • C

    8

  • D

    3

Explanation

fib sequence: 0,1,1,2,3,5,8. fib(5) = 5.

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

Related questions