GateAI
Programming in C

GATE CSE 2015 Set 1 · Q33

MCQ2 marksEasy

Time

Consider the following pseudo code, where xx and yy are positive integers.begin q := 0 r := x while r >= y do begin r := r - y q := q + 1 end endThe post condition that needs to be satisfied after the program terminates is

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) {x = qy + r r < y}

Programming in C

The algorithm implements integer division of xx by yy using repeated subtraction, where qq represents the quotient and rr represents the remainder.

  1. Loop Invariant: At the start, q=0q = 0 and r=xr = x, so the relation:

    x=qy+rx = qy + r

    holds initially (x=0⋅y+xx = 0 \cdot y + x).

    In each iteration:

    rnew=r−y,qnew=q+1r_{\text{new}} = r - y, \quad q_{\text{new}} = q + 1
    qnewy+rnew=(q+1)y+(r−y)=qy+r=xq_{\text{new}} y + r_{\text{new}} = (q + 1)y + (r - y) = qy + r = x

    Thus, the invariant x=qy+rx = qy + r is preserved across all iterations.

  2. Loop Termination Condition: The loop terminates when the condition r≥yr \ge y is false, which means:

    r<yr < y
  3. Post-condition: Combining the loop invariant with the negation of the loop guard yields:

    {x=qy+r∧r<y}\{x = qy + r \wedge r < y\}

Hence, option (B) is correct.

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 Q33 of the GATE CSE 2015 Set 1 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 2015 Set 1 has 4.

How GATE tests Programming in C

More from GATE CSE 2015 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2015 Set 1 · Q33 · MCQ

Consider the following pseudo code, where x and y are positive integers. begin q := 0 r := x while r >= y do begin r := r - y q := q + 1 end end The post condition that needs to be satisfied after the program terminates is

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.