ISRO Scientist/Engineer CSE 2018Programming in C › Program Output and Loops
ISRO Scientist/Engineer CSE 2018 PYQ: What is the output of tho following program? main(){ int x=2, y=5; if(x<y) return (x=x+y); else…
What is the output of tho following program?
main(){
int x=2, y=5;
if(x<y) return (x=x+y);
else printf("z1");
printf("z2");
}- A$z2$
- B$z1z2$
- CCompilation error
- DNone of theseCorrect answer
Answer
(D) None of these
Approach
The key construct here is the return statement placed inside the if branch of main. Execution begins with x = 2 and y = 5. The condition x < y reduces to 2 < 5, which is true, so the statement return(x = x+y) executes. This statement performs two actions at once: it evaluates the assignment x = x+y (assigning 7 to x, yielding the value 7) and then immediately returns from main with that value. Both printf calls -- the one in the else branch (printf("z1")) and the one after the if-else block (printf("z2")) -- are located after the point of return and are therefore never reached. Because main terminates without executing any printf, the program produces no output whatsoever. Checking the options: option A (z2) would require the if-condition to be false and main to continue past the else; option B (z1z2) would require both printfs to run, which is impossible since they are in mutually exclusive branches in the first place; option C (Compilation error) is wrong because return(x = x+y) is perfectly valid C -- the assignment expression is well-typed and the return value is compatible with int main; hence no syntax or semantic error exists. The Runtime Error/NZEC shown in the screenshot is not a text output -- it is merely the runner reporting the non-zero exit status produced by returning 7, which actually corroborates that the return was taken. Since no output is generated, none of the literal output strings listed match, making option D (None of these) the correct choice.
Reviewed by ISRO IQ Editorial Team.