GateAI
Programming in C

GATE CSE 2015 Set 3 · Q54

NAT2 marksMedium

Time

Consider the following C program.#include<stdio.h> int f1(void); int f2(void); int f3(void); int x = 10; int main( ) { int x = 1; x += f1( ) + f2( ) + f3( ) + f2( ); printf("%d", x); return 0; } int f1() { int x = 25; x++; return x;} int f2() { static int x = 50; x++; return x;} int f3() { x *= 10; return x;}The output of the program is _________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 230

Programming in C

Let us trace the variables and function calls in the program:

  1. Global Variable:x = 10 initially.

  2. In main(): A local variable x = 1 is created, which shadows the global variable inside main.

  3. The expression being evaluated is: x += f1() + f2() + f3() + f2(); Evaluating the function calls from left to right:

    • f1():

      • Has a local variable int x = 25.

      • x++ increments the local x to 26.

      • Returns 26.

    • First call to f2():

      • Has a static local variable initialized once to 50 (static int x = 50).

      • x++ increments x to 51.

      • Returns 51.

    • f3():

      • References the global variable x (which is 10).

      • x *= 10 modifies the global x to 10×10=10010 \times 10 = 100.

      • Returns 100.

    • Second call to f2():

      • The static variable x retains its previous value of 51.

      • x++ increments x to 52.

      • Returns 52.

  4. Sum of return values:

    26+51+100+52=22926 + 51 + 100 + 52 = 229
  5. Updating local x in main():

    x=x+229=1+229=230x = x + 229 = 1 + 229 = 230

Thus, the program outputs 230.

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

How GATE tests Programming in C

More from GATE CSE 2015 Set 3

Share this solution

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

GateAI solution cardGATE CSE 2015 Set 3 · Q54 · NAT

Consider the following C program. #include int f1(void); int f2(void); int f3(void); int x = 10; int main( ) { int x = 1; x += f1( ) + f2( ) + f3( ) + f2( ); printf("%d", x); return 0; } int f1() { int x = 25; x++; return x;} int f2() { static int x = 50; x++; return x;} int f3() { x *= 10; return x;} The output of the program 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.