GateAI
Programming in C

GATE CSE 2014 Set 3 · Q42

MCQ2 marksMedium

Time

Consider the C function given below. Assume that the array listA contains n (>0> 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?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) It is an implementation of binary search.

Programming in C

Let us trace the do-while loop in ProcessArray:

  1. At each iteration, k=⌊(i+j)/2⌋k = \lfloor(i+j)/2\rfloor.

  2. If x<listA[k]x < \text{listA}[k]:

    • if (x <= listA[k]) is true   ⟹  j=k−1\implies j = k - 1.

    • if (listA[k] <= x) is false   ⟹  i\implies i remains unchanged.

    • The search space shrinks to the left half [i,k−1][i, k-1].

  3. If x>listA[k]x > \text{listA}[k]:

    • if (x <= listA[k]) is false   ⟹  j\implies j remains unchanged.

    • if (listA[k] <= x) is true   ⟹  i=k+1\implies i = k + 1.

    • The search space shrinks to the right half [k+1,j][k+1, j].

  4. If x==listA[k]x == \text{listA}[k]:

    • Both conditions x <= listA[k] and listA[k] <= x are true.

    • jj becomes k−1k - 1 and ii becomes k+1k + 1.

    • Since i=k+1>k−1=ji = k+1 > k-1 = j, the loop condition (i≤j)(i \le j) becomes false immediately and the loop terminates.

    • Outside the loop, kk still holds the index where listA[k]==x\text{listA}[k] == x, so return(k) correctly returns the index kk.

  5. If xx is not in listA, the search interval [i,j][i, j] eventually becomes empty (i>ji > j), loop terminates, and since listA[k]≠x\text{listA}[k] \neq x, it returns −1-1.

Therefore, the function is a valid implementation of binary search (Option B).

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Programming in C previous year question is Q42 of the GATE CSE 2014 Set 3 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 19 of them are 2-mark MCQs like this one, and GATE CSE 2014 Set 3 has 3.

How GATE tests Programming in C

More from GATE CSE 2014 Set 3

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2014 Set 3 · Q42 · MCQ

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 Which one of the following statements about the function ProcessArray is CORRECT?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.