GateAI
Runtime Environments

GATE CSE 2016 Set 1 · Q36

MCQ2 marksMedium

Time

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);}

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: D) 4, 4

Runtime Environments

Let us trace the program step-by-step under pass-by-reference and dynamic scoping:

  1. Global initialization sets a=3a = 3.

  2. In main(), m(a) is called. Since aa is not locally declared in main(), it refers to global aa.

  3. In m(y), the parameter yy is passed by reference, so yy binds directly to global aa (yy is an alias for global aa).

  4. a = 1; sets a=1a = 1 in the local scope of m. Notice that global aa (and thus yy) is still 33.

  5. a = y - a; computes a=3−1=2a = 3 - 1 = 2. So local variable aa of m becomes 22.

  6. n(a); is called with local aa (=2= 2). Parameter xx of n is passed by reference, so xx binds to m's local aa.

  7. In n(x):

    • Under dynamic scoping, references to unbound variable aa resolve to the most recent active caller, which is m, where a=2a = 2.

    • x = x * a; computes x=2×2=4x = 2 \times 2 = 4. Since xx is an alias for m's local aa, m's aa becomes 44.

    • print(x); prints 4.

  8. Execution returns to m(y):

    • print(a); prints the current value of m's local aa, which is 4.

Thus, the output is 4, 4, which corresponds to option (D).

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Runtime Environments previous year question is Q36 of the GATE CSE 2016 Set 1 paper. Find more GATE CSE PYQs on Runtime Environments in Compiler Design, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Runtime Environments PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Runtime Environments in GATE CSE

Runtime Environments has 7 GATE CSE PYQs across 7 papers from 2012 to 2023. 3 of them are 2-mark MCQs like this one, and it is the only one in GATE CSE 2016 Set 1.

How GATE tests Runtime Environments

More from GATE CSE 2016 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2016 Set 1 · Q36 · MCQ

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);}

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.