Skip to content
Official PYQ · 2016

GATE CS 2016 Set 1 Q36 — Runtime Environments

Explain with AI

What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed?                

a = 3;
void n(x) { x = x * a; print (x); }
void m(y) { a = 1 ; a = y - a;  n(a); print (a); }
void main () { m(a); }
Choose one option
View worked solution

Answer: D

To find the output, we trace the execution step-by-step under pass-by-reference and dynamic scoping:

  1. Initial State: Global variable a=3a = 3.
  2. Execution of main():
    • Calls m(a). Since parameters are passed by reference, the formal parameter y in m becomes an alias for the global variable a (value 33).
  3. Execution of m(y):
    • a = 1;: Under dynamic scoping, this assignment implicitly creates/binds a local variable a in the scope of m with an initial value of 11.
    • a = y - a;: Updates m's local a to global a−local a=3−1=2\text{global } a - \text{local } a = 3 - 1 = 2.
    • n(a);: Calls n passing m's local a by reference. Thus, the formal parameter x in n becomes an alias for m's local a.
  4. Execution of n(x):
    • x = x * a;: Under dynamic scoping, the variable a is resolved by searching up the active call stack (n →\rightarrow m). It resolves to m's local variable a (value 22).
    • Since x is a reference to m's local a, this statement evaluates to: local a=local a×local a=2×2=4\text{local } a = \text{local } a \times \text{local } a = 2 \times 2 = 4
    • print(x); prints 44.
  5. Return to m(y):
    • print(a); prints the current value of m's local a, which is now 44.

Thus, the printed output is 4,44, 4.

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

Published
Report a correction
Keep exploringMore Runtime Environments questions