GateAI
Programming in C

GATE CSE 2020 · Q48

NAT2 marksMedium

Time

Consider the following C functions.int tob(int b, int* arr){ int i; for(i=0; b>0; i++){ if(b%2) arr[i]=1; else arr[i]=0; b = b/2; } return(i); } int pp(int a,int b) { int arr[20]; int i, tot = 1, ex, len; ex = a; len = tob(b,arr); for(i=0; i<len; i++){ if(arr[i]==1) tot = tot * ex; ex = ex * ex; } return(tot); }The value returned by pp(3,4) is _______.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 81

Programming in C

The function pp(a, b) computes aba^b using modular/binary exponentiation.

  1. tob(4, arr) converts b=4b = 4 into its binary representation in reverse order (least significant bit first) and stores it in arr:

    • i=0i=0: b=4  ⟹  4%2=0  ⟹  arr[0]=0,  b=2b=4 \implies 4\%2 = 0 \implies arr[0] = 0,\; b=2

    • i=1i=1: b=2  ⟹  2%2=0  ⟹  arr[1]=0,  b=1b=2 \implies 2\%2 = 0 \implies arr[1] = 0,\; b=1

    • i=2i=2: b=1  ⟹  1%2=1  ⟹  arr[2]=1,  b=0b=1 \implies 1\%2 = 1 \implies arr[2] = 1,\; b=0

    • Loop terminates; tob returns len = 3 with arr = [0, 0, 1].

  2. In pp(3, 4):

    • Initial values: ex = 3, tot = 1

    • i=0i = 0: arr[0] == 1 is false   ⟹  tot=1,  ex=3×3=9\implies tot = 1,\; ex = 3 \times 3 = 9

    • i=1i = 1: arr[1] == 1 is false   ⟹  tot=1,  ex=9×9=81\implies tot = 1,\; ex = 9 \times 9 = 81

    • i=2i = 2: arr[2] == 1 is true   ⟹  tot=1×81=81,  ex=81×81=6561\implies tot = 1 \times 81 = 81,\; ex = 81 \times 81 = 6561

  3. The function returns tot = 81.

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 Q48 of the GATE CSE 2020 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. 17 of them are 2-mark NAT questions like this one, and GATE CSE 2020 has 3.

How GATE tests Programming in C

More from GATE CSE 2020

Share this solution

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

GateAI solution cardGATE CSE 2020 · Q48 · NAT

Consider the following C functions. int tob(int b, int* arr){ int i; for(i=0; b>0; i++){ if(b%2) arr[i]=1; else arr[i]=0; b = b/2; } return(i); } int pp(int a,int b) { int arr[20]; int i, tot = 1, ex, len; ex = a; len = tob(b,arr); for(i=0; i The value returned by pp(3,4) 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.