GateAI
Recursion

GATE CSE 2024 Set 1 · Q19

MCQ1 markEasy

Time

Consider the following C program:#include <stdio.h> void fX(); int main(){ fX(); return 0; } void fX(){ char a; if((a=getchar()) != ' ') fX(); if(a != ' ') putchar(a); }Assume that the input to the program from the command line is 1234 followed by a newline character. Which one of the following statements is CORRECT?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) The program will terminate with 4321 as output

Recursion

Let's trace the recursive execution of the function fX() with input `"1234 "`:

  1. First Call (fX()):

    • a reads '1'. Since `'1' != '

', it makes a recursive call to fX()`.

  1. Second Call (fX()):

    • a reads '2'. Since `'2' != '

', it makes a recursive call to fX()`.

  1. Third Call (fX()):

    • a reads '3'. Since `'3' != '

', it makes a recursive call to fX()`.

  1. Fourth Call (fX()):

    • a reads '4'. Since `'4' != '

', it makes a recursive call to fX()`.

  1. Fifth Call (fX()):

    • a reads `'

'. The condition (a = getchar()) != ''` evaluates to false, so no further recursive call is made.- The next condition `if (a != '')` is also false, so nothing is printed and this call returns to the fourth call.

  1. Unwinding the Stack:

    • In the fourth call, a = '4'. putchar('4') prints 4.

    • In the third call, a = '3'. putchar('3') prints 3.

    • In the second call, a = '2'. putchar('2') prints 2.

    • In the first call, a = '1'. putchar('1') prints 1.

After unwinding the recursion, control returns to main(), which returns 0 and terminates.

Thus, the printed output is 4321, making (C) the correct option.

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 Q19 of the GATE CSE 2024 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. 2 of them are 1-mark MCQs like this one, and it is the only one in GATE CSE 2024 Set 1.

How GATE tests Recursion

More from GATE CSE 2024 Set 1

Share this solution

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

GateAI solution cardGATE CSE 2024 Set 1 · Q19 · MCQ

Consider the following C program: #include void fX(); int main(){ fX(); return 0; } void fX(){ char a; if((a=getchar()) != ' ') fX(); if(a != ' ') putchar(a); } Assume that the input to the program from the command line is 1234 followed by a newline character. Which one of the following statements is CORRECT?

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.