Exams › GATE › Technical
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?
- The program will not terminate
- The program will terminate with no output
- The program will terminate with 4321 as output
- The program will terminate with 1234 as output
Correct answer: The program will terminate with 4321 as output
Solution
The program reads characters from input recursively until it encounters a newline, storing each character in reverse order due to the nature of the recursive calls. When the newline is reached, the recursion unwinds, and the characters are printed in reverse, resulting in '4321' as the output.
Related GATE Technical questions
⚔️ Practice GATE Technical free + battle 1v1 →