Recursive Sum Of Array Elements
What is the output of the following code?
public class ArraySum {
public static int recursiveSum(int[] arr, int index) {
if (index == arr.length) {
return 0;
}
return arr[index] + recursiveSum(arr, index + 1);
}
public static void main(String[] args) {
int[] nums = {2, 4, 6, 8};
System.out.println(recursiveSum(nums, 0));
}
}
A
18
B
22
C
20
D
16
Question Leaderboard
Not enough data yet to show leaderboard.
