GateAI
Recursion

GATE CSE 2017 Set 1 · Q35

MCQ2 marksMedium

Time

Consider the following two functions.void fun1(int n) { if(n == 0) return; printf("%d", n); fun2(n - 2); printf("%d", n); } void fun2(int n) { if(n == 0) return; printf("%d", n); fun1(++n); printf("%d", n); }The output printed when fun1(5) is called is

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: A) 53423122233445

Recursion

Let us trace the execution of fun1(5) step by step:

  1. fun1(5):

    • Prints 5

    • Calls fun2(5 - 2) = fun2(3)

  2. fun2(3):

    • Prints 3

    • ++n increments local n from 33 to 44, then calls fun1(4)

  3. fun1(4):

    • Prints 4

    • Calls fun2(4 - 2) = fun2(2)

  4. fun2(2):

    • Prints 2

    • ++n increments local n from 22 to 33, then calls fun1(3)

  5. fun1(3):

    • Prints 3

    • Calls fun2(3 - 2) = fun2(1)

  6. fun2(1):

    • Prints 1

    • ++n increments local n from 11 to 22, then calls fun1(2)

  7. fun1(2):

    • Prints 2

    • Calls fun2(2 - 2) = fun2(0), which returns immediately because of the base condition n == 0

    • Prints n (which is 2)

    • Returns to fun2(1)

  8. Back in fun2(1):

    • Prints n (which was incremented to 2)

    • Returns to fun1(3)

  9. Back in fun1(3):

    • Prints n (which is 3)

    • Returns to fun2(2)

  10. Back in fun2(2):

    • Prints n (which was incremented to 3)

    • Returns to fun1(4)

  11. Back in fun1(4):

    • Prints n (which is 4)

    • Returns to fun2(3)

  12. Back in fun2(3):

    • Prints n (which was incremented to 4)

    • Returns to fun1(5)

  13. Back in fun1(5):

    • Prints n (which is 5)

    • Returns

Concatenating all printed numbers in chronological order gives: 5 3 4 2 3 1 2 2 2 3 3 4 4 5→\rightarrow53423122233445.

Thus, option (A) is correct.

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 Recursion previous year question is Q35 of the GATE CSE 2017 Set 1 paper. Find more GATE CSE PYQs on Recursion 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 Recursion 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.

Recursion in GATE CSE

Recursion has 19 GATE CSE PYQs across 15 papers from 2011 to 2026. 10 of them are 2-mark MCQs like this one, and GATE CSE 2017 Set 1 has 2.

How GATE tests Recursion

More from GATE CSE 2017 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2017 Set 1 · Q35 · MCQ

Consider the following two functions. void fun1(int n) { if(n == 0) return; printf("%d", n); fun2(n - 2); printf("%d", n); } void fun2(int n) { if(n == 0) return; printf("%d", n); fun1(++n); printf("%d", n); } The output printed when fun1(5) is called 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.