Recursion

AP Computer Science A· difficulty 2/5

public static int sum(int[] a, int i) {
  if (i >= a.length) return 0;
  return a[i] + sum(a, i + 1);
}
// call: sum({1,2,3,4}, 0)

What does the call return?

  • A

    6

  • B

    0

  • C

    4

  • D

    10

    check_circle

Explanation

1+2+3+4 = 10.

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

Related questions