Recursion

AP Computer Science A· difficulty 4/5

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

What is printed?

  • A

    5

  • B

    13

  • C

    8

    check_circle
  • D

    21

Explanation

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

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

Related questions