GATE / CS ARCHIVE

GATE CSE 2006 / QUESTION 56

Pass-by-reference with an expression argument

Compiler Design · Runtime Environments & Parameter Passing · Full GATE CSE 2006 paper

GATE 2006, Q56 2 marks · MCQ
Report issue

Runtime Environments & Parameter Passing

Question 56, GATE 2006: A pedagogical pass-by-reference language executes:
subroutine swap(ix,iy)
 it=ix
 L1: ix=iy
 L2: iy=it
end
ia=3; ib=8; call swap(ia,ib+5); print ia,ib.
Statements: S1 an unnamed temporary initialized to 13 is allocated and its address passed for the second argument; S2 runtime error at L1; S3 runtime error at L2; S4 prints 13,8; S5 prints 13,−2. Which stated set is correct under the expression-to-temporary model?
Options:
(A) S1 and S2
(B) S1 and S4
(C) S3
(D) S1 and S5
Question 56, GATE 2006: A pedagogical pass-by-reference language executes:
subroutine swap(ix,iy)
 it=ix
 L1: ix=iy
 L2: iy=it
end
ia=3; ib=8; call swap(ia,ib+5); print ia,ib.
Statements: S1 an unnamed temporary initialized to 13 is allocated and its address passed for the second argument; S2 runtime error at L1; S3 runtime error at L2; S4 prints 13,8; S5 prints 13,−2. Which stated set is correct under the expression-to-temporary model?
Options:
(A) S1 and S2
(B) S1 and S4
(C) S3
(D) S1 and S5
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Initially ia=3 and ib=8; the expression ib+5 evaluates to 13. Under the stated expression-to-temporary reference model, the compiler passes the address of a temporary containing 13, not the address of ib. swap sets ia to 13 and the temporary to 3; ib remains 8.

2. S1 and S4 hold, so B. This is the pedagogical model specified by the options, not a guarantee that every actual FORTRAN standard/compiler permits defining an expression-associated dummy argument.

3. Teaching check: The expression ib+5 has a value but is not the storage cell ib. Passing a temporary permits swap to overwrite the argument without overwriting ib. Trace the two addressed cells separately: ia changes 3→13, temporary changes 13→3, and ib stays 8.

Original paper, page 8