GateAI
Programming in C

GATE CSE 2021 Set 1 · Q48

NAT2 marksEasy

Time

Consider the following ANSI C function:int SimpleFunction(int Y[], int n, int x) { int total = Y[0], loopIndex; for (loopIndex = 1; loopIndex <= n - 1; loopIndex++) total = x * total + Y[loopIndex]; return total; }Let ZZ be an array of 10 elements with Z[i]=1Z[i]=1, for all ii such that 0≤i≤90 \le i \le 9. The value returned by SimpleFunction(Z, 10, 2) is ________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 1023

Programming in C

The given function evaluates a polynomial using Horner's rule:

total=Y[0]⋅xn−1+Y[1]⋅xn−2+⋯+Y[n−1]\text{total} = Y[0] \cdot x^{n-1} + Y[1] \cdot x^{n-2} + \dots + Y[n-1]

Given:

  • n=10n = 10

  • x=2x = 2

  • Z[i]=1Z[i] = 1 for all 0≤i≤90 \le i \le 9

Tracing the value of total:

  • Initial: total=Z[0]=1\text{total} = Z[0] = 1

  • loopIndex = 1: total=2×1+1=3=22−1\text{total} = 2 \times 1 + 1 = 3 = 2^2 - 1

  • loopIndex = 2: total=2×3+1=7=23−1\text{total} = 2 \times 3 + 1 = 7 = 2^3 - 1

  • loopIndex = 3: total=2×7+1=15=24−1\text{total} = 2 \times 7 + 1 = 15 = 2^4 - 1

  • …\dots

  • loopIndex = 9: total=210−1=1024−1=1023\text{total} = 2^{10} - 1 = 1024 - 1 = 1023

Therefore, the value returned is 1023.

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 2021 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. 17 of them are 2-mark NAT questions like this one, and GATE CSE 2021 Set 1 has 2.

How GATE tests Programming in C

More from GATE CSE 2021 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2021 Set 1 · Q48 · NAT

Consider the following ANSI C function: int SimpleFunction(int Y[], int n, int x) { int total = Y[0], loopIndex; for (loopIndex = 1; loopIndex Let Z be an array of 10 elements with Z[i]=1, for all i such that 0 ≤ i ≤ 9. The value returned by SimpleFunction(Z, 10, 2) 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.