GATE / CS ARCHIVE

GATE CSE 2024 SET 2 / QUESTION 13

Trace argument evaluation separately from function execution

Programming in C · Functions & Parameter Passing · Full GATE CSE 2024 Set 2 paper

GATE 2024 Set 2, Q13 1 mark · MCQ
Report issue

Functions & Parameter Passing

Question 13, GATE 2024: Functions g(p) and h(q) each print their integer argument with no separator and return it. Function f(x,y) calls g(x), then h(y). main calls f(g(10),h(20)). Assume argument expressions are evaluated right to left. Output choices: A 20101020; B 10202010; C 20102010; D 10201020.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Step through it (13 steps)

Approach: Separate the calls that evaluate arguments from the calls inside f.

1. Derivation. Using the stipulated right-to-left argument evaluation, h(20) prints 20, then g(10) prints 10. Their return values bind x=10, y=20 in f. Its sequential statements then print10 and 20. With no separators the output is 20101020, option A. Right-to-left evaluation is an explicit premise here, not a portable C-language guarantee.

2. Explanation and checks. The event trace is h(20): print 20, return 20; g(10): print 10, return 10; enter f with x=10,y=20; g(x): print 10; h(y): print 20. Reversing evaluation order does not swap the formal parameters: the first argument still supplies x. The explicit right-to-left assumption applies to evaluating the argument expressions, not to reversing the two statements in the body of f.

Original paper, page 10 · Answer key