Recursive Linear Search
Consider the following recursive findPosition method. What is printed as a result of executing the code segment in the main method comment?
public int findPosition(int nums[], int key, int currentIndex) {
if(currentIndex >= nums.length)
return -1;
if(nums[currentIndex] == key)
return currentIndex;
return findPosition(nums, key, currentIndex + 1);
}
// In main:
// int[] players = {3, 7, 9, 5, 9};
// System.out.println(findPosition(players, 9, 0));
A
4
B
2
C
-1
D
0
Question Leaderboard
Not enough data yet to show leaderboard.
