Choosing how to earn college credit? CLEP vs AP guide
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Data Collections
Start Practice TestPractice Test
About Exam
medium

Recursive Binary Search Trace

Mark
< Prev
Next >

Consider the following recursive binary search method:

public static boolean contains(int[] data, int value, int low, int high) {
    if (low > high) {
        return false;
    }
    
    int mid = (low + high) / 2;
    
    if (data[mid] == value) {
        return true;
    } else if (data[mid] > value) {
        return contains(data, value, low, mid - 1);
    } else {
        return contains(data, value, mid + 1, high);
    }
}

Suppose an array data is defined as {5, 12, 23, 34, 45, 56, 67, 78, 89, 90}. If the method is invoked by the call contains(data, 67, 0, 9), what is the sequence of values assigned to the mid variable across all recursive calls?

A

4, 6, 7, 5

B

4, 8, 5, 6

C

5, 7, 6, 4

D

4, 7, 5, 6

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.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

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