Recursive Method Error Analysis
Which of the following statements best describes the error in the faultySearch method and its consequence?
public class RecursionError {
public static int faultySearch(int[] arr, int index, int key) {
if(index >= arr.length) return -1;
if(arr[index] == key) return index;
return faultySearch(arr, index, key);
}
public static void main(String[] args) {
int[] array = {1, 2, 3};
System.out.println(faultySearch(array, 0, 2));
}
}
A
The method incorrectly compares the key, but it will still eventually find the element.
B
The base case is missing, which would cause a compilation error.
C
The method fails to increment the index in the recursive call, causing infinite recursion and eventually a StackOverflowError.
D
The key comparison is reversed, leading to incorrect results.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE