Consider the C function given below. Assume that the array listA contains n (>…

GATE · 2014 · CS · Set 3 · Computer Science & IT

Consider the C function given below. Assume that the array listA contains n (> 0) elements, sorted in ascending order.

int ProcessArray(int *listA, int x, int n)
{
    int i, j, k;
    i = 0;
    j = n-1;
    do {
        k = (i+j)/2;
        if (x <= listA[k])
            j = k-1;
        if (listA[k] <= x)
            i = k+1;
    } while (i <= j);
    if (listA[k] == x)
        return(k);
    else
        return -1;
}

Which one of the following statements about the function ProcessArray is CORRECT?

  1. A.

    It will run into an infinite loop when x is not in listA.

  2. B.

    It is an implementation of binary search.

  3. C.

    It will always find the maximum element in listA.

  4. D.

    It will return −1 even when x is present in listA.

Attempted by 287 students.

Sign up free to check your answer

Sign up free

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…