GateAI
Programming in C

GATE CSE 2020 · Q46

NAT2 marksMedium

Time

Consider the following C functions.int fun1(int n) { static int i = 0; if (n > 0) { ++i; fun1(n-1); } return(i); } int fun2(int n) { static int i = 0; if (n > 0) { i = i + fun1(n); fun2(n-1); } return(i); }The return value of fun2(5) is _________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 55

Programming in C

Both fun1 and fun2 maintain their own separate static int i, initialized once to 0.

### Behavior of fun1(n): fun1(n) increments its static variable i exactly nn times during recursive calls when invoked with argument nn. Since i is static and retains its value between function calls:

  • Before any call, fun1's static i=0i = 0.

  • Calling fun1(k) increments its static ii by kk and returns the updated total value of ii.

### Step-by-step execution of fun2(5): Let i1i_1 denote the static variable of fun1 and i2i_2 denote the static variable of fun2. Initially, i1=0i_1 = 0 and i2=0i_2 = 0.

  1. fun2(5) (n=5n=5):

    • fun1(5) is called: i1i_1 increases by 5  ⟹  i1=55 \implies i_1 = 5, returns 55.

    • i2=i2+fun1(5)=0+5=5i_2 = i_2 + \text{fun1}(5) = 0 + 5 = 5.

    • Calls fun2(4).

  2. fun2(4) (n=4n=4):

    • fun1(4) is called: i1i_1 increases by 4  ⟹  i1=5+4=94 \implies i_1 = 5 + 4 = 9, returns 99.

    • i2=5+9=14i_2 = 5 + 9 = 14.

    • Calls fun2(3).

  3. fun2(3) (n=3n=3):

    • fun1(3) is called: i1i_1 increases by 3  ⟹  i1=9+3=123 \implies i_1 = 9 + 3 = 12, returns 1212.

    • i2=14+12=26i_2 = 14 + 12 = 26.

    • Calls fun2(2).

  4. fun2(2) (n=2n=2):

    • fun1(2) is called: i1i_1 increases by 2  ⟹  i1=12+2=142 \implies i_1 = 12 + 2 = 14, returns 1414.

    • i2=26+14=40i_2 = 26 + 14 = 40.

    • Calls fun2(1).

  5. fun2(1) (n=1n=1):

    • fun1(1) is called: i1i_1 increases by 1  ⟹  i1=14+1=151 \implies i_1 = 14 + 1 = 15, returns 1515.

    • i2=40+15=55i_2 = 40 + 15 = 55.

    • Calls fun2(0).

  6. fun2(0) (n=0n=0):

    • Condition (n>0)(n > 0) is false. Returns i2=55i_2 = 55.

All pending recursive calls unwind and return i2=55i_2 = 55.

Thus, the final return value is 55.

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 Q46 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 · Q46 · NAT

Consider the following C functions. int fun1(int n) { static int i = 0; if (n > 0) { ++i; fun1(n-1); } return(i); } int fun2(int n) { static int i = 0; if (n > 0) { i = i + fun1(n); fun2(n-1); } return(i); } The return value of fun2(5) 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.