Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 10: Recursion
Start Practice TestPractice Test
About Exam
easy Solved by 3 students

Recursive Method Error Analysis

Mark
< Prev
Next >

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 method fails to increment the index in the recursive call, causing infinite recursion and eventually a StackOverflowError.

C

The base case is missing, which would cause a compilation error.

D

The key comparison is reversed, leading to incorrect results.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|