?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Binary Search in Array of Integers (Mystery Method)

APCSA-6WKNB4

Consider the following method.

    public static int mystery(int[] arr, int key)
    {
        int q = 0;
        int low = 0;
        int high = arr.length - 1;

        while (low <= high)
        {
            int mid = (low + high)/2;
            q++
            if (key > arr[mid])
                low = mid + 1;
            else if (key < arr[mid])
                high = mid -1;
            else
                return q;
        } 
        return q;
    }

Suppose x is of type int[] containing the numbers 0 through 49 inclusive.

Which of the following is returned as a result of the call below?

mystery(x, 20) 
A

2

B

3

C

4

D

5

E

6