GateAI
Programming in C

GATE CSE 2016 Set 2 · Q35

MCQ2 marksMedium

Time

The following function computes XYX^Y for positive integers X and Y.int exp(int X, int Y) { int res = 1, a = X, b = Y; while ( b != 0 ){ if ( b%2 == 0) { a = a*a; b = b/2; } else { res = res*a; b = b-1; } } return res; }Which one of the following conditions is TRUE before every iteration of the loop?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) XY = res * ab

Programming in C

We need to find the loop invariant that holds true before every iteration of the while loop.

Initialization: Before entering the loop:

res=1,a=X,b=Yres = 1, \quad a = X, \quad b = Y

Checking option (C):

res⋅ab=1⋅XY=XYres \cdot a^b = 1 \cdot X^Y = X^Y

Thus, XY=res⋅abX^Y = res \cdot a^b holds initially.

Maintenance: Assume XY=res⋅abX^Y = res \cdot a^b holds at the start of an iteration.

  • Case 1 (bb is even): The algorithm updates anew=a2a_{\text{new}} = a^2, bnew=b/2b_{\text{new}} = b/2, and resnew=resres_{\text{new}} = res.

    resnew⋅(anew)bnew=res⋅(a2)b/2=res⋅ab=XYres_{\text{new}} \cdot (a_{\text{new}})^{b_{\text{new}}} = res \cdot (a^2)^{b/2} = res \cdot a^b = X^Y
  • Case 2 (bb is odd): The algorithm updates resnew=res⋅ares_{\text{new}} = res \cdot a, bnew=b−1b_{\text{new}} = b - 1, and anew=aa_{\text{new}} = a.

    resnew⋅(anew)bnew=(res⋅a)⋅ab−1=res⋅ab=XYres_{\text{new}} \cdot (a_{\text{new}})^{b_{\text{new}}} = (res \cdot a) \cdot a^{b-1} = res \cdot a^b = X^Y

In both cases, the relation XY=res⋅abX^Y = res \cdot a^b is preserved for the next iteration.

Hence, (C) is the correct loop invariant.

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 Q35 of the GATE CSE 2016 Set 2 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 2016 Set 2 has 3.

How GATE tests Programming in C

More from GATE CSE 2016 Set 2

Share this solution

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

GateAI solution cardGATE CSE 2016 Set 2 · Q35 · MCQ

The following function computes XY for positive integers X and Y. int exp(int X, int Y) { int res = 1, a = X, b = Y; while ( b != 0 ){ if ( b%2 == 0) { a = a*a; b = b/2; } else { res = res*a; b = b-1; } } return res; } Which one of the following conditions is TRUE before every iteration of the loop?

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.