GateAI
Programming in C

GATE CSE 2015 Set 1 · Q11

NAT1 markEasy

Time

The output of the following C program is_________.void f1(int a, int b) { int c; c=a; a=b; b=c; } void f2(int *a, int *b) { int c; c=*a; *a=*b; *b=c; } int main(){ int a=4, b=5, c=6; f1(a,b); f2(&b, &c); printf("%d",c-a-b); }

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: -5

Programming in C

Let us trace the execution of the program step by step:

  1. In main(), local variables are initialized:

    • a = 4

    • b = 5

    • c = 6

  2. The function f1(a, b) is called with parameters passed by value. Any modifications made to a and b inside f1 are local to f1 and do not affect the variables in main(). Thus, after f1(a, b) returns, the values in main() remain:

    • a = 4, b = 5, c = 6

  3. The function f2(&b, &c) is called with the addresses of b and c (pass-by-reference using pointers):

    • Inside f2, the values pointed to by a (which is &b) and b (which is &c) are swapped.

    • Hence, in main(), the value of b becomes 66 and the value of c becomes 55.

  4. Now, the printf statement computes c - a - b:

    c−a−b=5−4−6=−5\text{c} - \text{a} - \text{b} = 5 - 4 - 6 = -5

Therefore, the output of the program is -5.

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 Q11 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. 16 of them are 1-mark NAT questions 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 · Q11 · NAT

The output of the following C program is … void f1(int a, int b) { int c; c=a; a=b; b=c; } void f2(int *a, int *b) { int c; c=*a; *a=*b; *b=c; } int main(){ int a=4, b=5, c=6; f1(a,b); f2(&b, &c); printf("%d",c-a-b); }

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.