Skip to content
Official PYQ · 1988

GATE CS 1988 Q2xv — Runtime Environments

Explain with AI

What is printed by following program, assuming call-by reference method of passing parameters for all variables in the parameter list of procedure P?

program     Main(inout, output);
var     a, b:integer;
        procedure P(x, y, z:integer);
        begin
            y:=y+1
            z:=x+x
        end P;
begin
    a:=2; b:=3;
    p(a+b, a, a);
    Write(a)
end.
View worked solution

Answer: 10

To find the output of the program under the call-by-reference parameter passing method, we trace the execution step-by-step:

1. Initial State

Before calling procedure P, the variables in the main program are initialized as:

  • a = 2
  • b = 3

2. Parameter Binding in Call-by-Reference

The procedure call is p(a+b, a, a) with formal parameters (x, y, z):

  • For expression a+b: Since an expression does not have an l-value (memory address), the compiler evaluates the expression at the time of the call, stores the result in a temporary location, and passes the reference of this temporary location to x.
    temp=a+b=2+3=5\text{temp} = a + b = 2 + 3 = 5 Thus, x references temp (value is 55).
  • For variable a: Both y and z receive the reference (address) of the variable a. This creates an alias, meaning any modification to y or z directly affects a.

3. Execution of Procedure P

  • Statement 1: y := y + 1

    Since y references a, this is equivalent to:

    a:=a+1  ⟹  a:=2+1=3a := a + 1 \implies a := 2 + 1 = 3
  • Statement 2: z := x + x

    Since z references a and x references temp (which holds 55):

    a:=temp+temp  ⟹  a:=5+5=10a := \text{temp} + \text{temp} \implies a := 5 + 5 = 10

4. Output

After returning from the procedure, the main program executes Write(a). The value of a is now 10.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Runtime Environments questions