GATE / CS ARCHIVE

GATE CSE 1996 / QUESTION 19(i)

Reference parameter aliasing

Programming in C · Functions & Parameter Passing · Full GATE CSE 1996 paper

GATE 1996, Q19(i) Descriptive
Report issue

Functions & Parameter Passing

Question 19(i), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by reference.
Question 19(i), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by reference.
Question 19(i), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by reference.
Tap to enlarge
Answer & solution

Answer: point 1: 5 5; point 2: 10 10; point 3: 10. (no official key was published)

A reference parameter is another name for the caller's variable, not a private copy of its value. Here the actual parameter is the global b, so a and b designate the same storage throughout test1. Initially that cell contains 3. Executing a:=5 updates the cell itself, which means both a and b now read as 5. Consequently the first output is point 1: 5 5.

Next test2 executes b:=10. It refers to the global b by the displayed lexical scope; this is the same cell already aliased by a. Therefore both reads at the second output produce 10, giving point 2: 10 10. Returning from test1 requires no copy-back for a reference parameter. The global value remains 10, and the final output is point 3: 10.

The important reasoning is storage identity: a:=5 changes b immediately, and test2's assignment to b changes what a observes immediately. The procedure nesting does not create a second b or hide the global declaration. Spaces and line breaks may depend on Pascal's output formatting; the value sequences, in order, are (5,5), (10,10), and (10).

Original paper, page 9