GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Trace aliases passed by reference

3 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Map reference formals to actual storage and execute updates respecting aliases. Excludes invalid expression actuals and evaluation-order-dependent recursion.

2 marks · MCQ
Report issue

Aliasing under call by reference

Runtime Environments & Parameter Passing

Question 2.17, GATE 2001: Caller variables begin x=10,y=3 and call func1(y,x,x) by reference, then print x,y. The callee’s formals are x,y,z and execute y=y+4; z=x+y+z. Choose 10,3; 31,3; 27,7; or none.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step 1: Rename the caller variables X=10,Y=3 to avoid confusing them with formal names. The call func1(Y,X,X) makes formal x alias Y, while both y and z alias the same caller variable X.

Step 2: y=y+4 changes X from 10 to 14. Then z=x+y+z reads 3+14+14=31 and stores 31 in X. Y is never assigned and remains 3.

The caller prints 31 and 3, B. Treating y and z as separate copies would miss the aliasing effect; this is reference-passing pseudocode, not ordinary C scalar call-by-value.

Original paper, page 6

2 marks · MCQ
Report issue

Two reference-parameter function calls

Runtime Environments & Parameter Passing

Question 2.13, GATE 1998: In Pascal-like code, f (var x) increments x and returns it. Starting x=5, result=f (x)*f (x). Options 5, 25, 36, 42.
Tap to enlarge
Your answer
Answer & solution

Answer: D: 42 under sequential call evaluation (no official key was published)

The var parameter aliases the caller’s x, so the first completed call changes 5 to 6 and returns 6. The second then changes 6 to 7 and returns 7. Multiplication gives 6·7=42. Choosing the opposite operand first merely swaps these two returned values, leaving the product unchanged. Thus D in the ordinary sequential procedural evaluation model. This is not pass-by-value, which would give 6·6=36. The explanation does not import C’s unsequenced-expression rules into the displayed Pascal-like language.

Original paper, page 5

2 marks · MSQ
Report issue

Aliased reference parameters with a shadowed local

Runtime Environments & Parameter Passing

Question 7.1, GATE 1993: Global m=0. P(var x,y:integer) has its own local m, sets that local m:=1, then x:=y+1. What is printed after P(m,m)?
Question 7.1, GATE 1993: Global m=0. P(var x,y:integer) has its own local m, sets that local m:=1, then x:=y+1. What is printed after P(m,m)?
Tap to enlarge
Select all that apply
Answer & solution

Answer: D: 1 because x and y both reference the global m (no official key was published)

Both formal reference parameters bind to the caller’s global m. The procedure’s separate local variable named m shadows the global name only for direct uses of that identifier in P; setting it to one does not change the caller’s m. Then x:=y+1 reads global m=0 through y and stores one through x. Consequently the global m printed by the caller is one. The result is not caused by the local m assignment, so the option giving that incorrect reason is not selected.

Original paper, page 6