GATE CS 2024 Set 1 | Question 19

Programming & Data Structures › Functions · MCQ · 1 mark · Answer: C

  1. Question 19Programming & Data Structuresmcq1 mark
    Consider the C program (input is 1234 then newline):
    void fX();
    int main(){ fX(); return 0; }
    void fX(){
       char a;
       if((a=getchar()) != '\n') fX();
       if(a != '\n') putchar(a);
    }
    Which statement is CORRECT?
    • A.
      The program will not terminate
    • B.
      The program will terminate with no output
    • C.
      The program will terminate with 4321 as output
      ✓
    • D.
      The program will terminate with 1234 as output

    Answer: C

    Solution

    Answer: (C) 4321 Each call reads one character and recurses before printing. The recursion reads 1,2,3,4 then newline (stops recursing). As the recursion unwinds, characters are printed in reverse of reading order: 4,3,2,1 -> "4321".

More GATE PYQ on Functions

All GATE CS Programming & Data Structures PYQ →

Attempt GATE CS 2024 Set 1 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →