GATE / CS ARCHIVE

SUBJECT COLLECTION

Compiler Design · Runtime Environments & Parameter Passing

42 questions, newest first

GATE CSE has asked 42 questions on Runtime Environments & Parameter Passing (Compiler Design) from 1991 to 2021, in 20 of the 46 papers; the most recent is in GATE CSE 2021 Set 1. In the last five years it carried 0 marks per 100-mark paper on average, against 0.4 since 2003. Since 2014 the questions have been 3 MCQ. 5 groups of questions here are solved the same way; “One per method” keeps the newest of each.

Browse topics
1 mark · MCQ
Report issue

Runtime Environments & Parameter Passing

Question 4, GATE 2021: S1: The sequence of procedure calls is a preorder traversal of the activation tree. S2: The sequence of procedure returns is a postorder traversal. Which is correct? A S1 true/S2 false; B S1 false/S2 true; C both true; D both false.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

A procedure activation is entered before its child calls, producing preorder. It returns only after its nested calls finish, producing postorder. Thus both statements are true, C, in the ordinary sequential, properly nested call-return model.

For example, if main calls A, A calls B and C in order, and main then calls D, the entry order is main,A,B,C,D. The completion order is B,C,A,D,main. These are exactly preorder and postorder on the ordered activation tree. Recursive calls create distinct activation nodes even when the procedure name repeats; one must not merge all calls of the same procedure into a single node.

Original paper, page 13 · Answer key

2 marks · MCQ
Report issue

Runtime Environments & Parameter Passing

Question 36, GATE 2016: Assume dynamic scoping and pass-by-reference in this pseudocode:
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);}
What is printed? A:6,2. B:6,6. C:4,2. D:4,4. The source does not explicitly declare whether m's first assignment introduces a local a; preserve that ambiguity.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Our answer: D under implicit local-a binding

The intended pseudocode treats a=1 inside m as introducing a local a, while reference parameter y remains an alias of the outer a=3. Then local a=y−a=2. Passing it by reference to n makes x and dynamically resolved a refer to that same local cell: x=x*a stores 4, and both prints show 4, D. If a=1 instead assigns the existing outer dynamically scoped a, y aliases it, so a=y−a becomes 0 and the output is 0, 0. The source lacks an explicit local declaration, so its binding convention must be supplied.

Distinguish parameter aliasing from name lookup. Under the intended local-binding interpretation, y aliases the global cell containing 3, while m's local a first contains 1 and then 2. n's formal x aliases that local cell, and dynamic lookup of its free name a finds the active m frame, also that same cell. Thus the assignment squares 2 in place. Under assignment-to-existing-binding semantics both y and a refer to the global cell before subtraction, making 1−1=0. The printed key chooses the former semantics but does not remove the source ambiguity.

Original paper, page 15 · Answer key

1 mark · MCQ
Report issue

Runtime Environments & Parameter Passing

Question 18, GATE 2014: Evaluate: (1) statically allocating every data area prevents recursion; (2) automatic garbage collection is essential for recursion; (3) dynamic allocation of activation records is essential for recursion; (4) both heap and stack are essential for recursion. Choose A 1,2 only; B 2,3 only; C 3,4 only; D 1,3 only.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Our answer: D under the general recursion storage model

Step 1: General recursive calls can leave multiple invocations of the same procedure simultaneously active. Each needs its own parameters, local variables, return address and other activation information. Step 2: One fixed static record per procedure cannot preserve all these activations; records must be allocated as calls occur. This is the intended sense in which 1 and 3 hold. Step 3: A runtime stack allocates and reclaims activation records without a heap or automatic garbage collector, refuting 2 and 4. Thus D. Bounded recursion or tail-call elimination can use fixed storage, so these claims concern the conventional general recursion model, not every special recursive program.

Original paper, page 57 · Answer key

2 marks · MCQ
Report issue

Reference parameter and multiplication evaluation order

Runtime Environments & Parameter Passing

Question 42, GATE 2013: p starts 5. The function uses reference parameter x and value parameter c: int f(int &x, int c){c=c−1;if(c==0)return1;x=x+1;return f(x, c)*x;} What does f(p, p) return? A3024, B6561, C55440, D161051. The published key awards Marks to All; source does not specify operand evaluation order.
Tap to enlarge
Your answer
Answer & solution

Official answer: Marks to All

Our answer: No uniquely specified value; 3024 (A) and 6561 (B) arise from different operand orders.

The parameter declaration int &x is C++ reference syntax, not valid C. Every recursive invocation aliases the same p through x, whereas c is a private by-value copy. The four non-base calls increment p successively to 6, 7, 8 and 9; the fifth call returns 1 without incrementing p. In return f(x, c) * x, multiplication does not require the left operand to be evaluated first. If each call reads its right-hand x before executing the recursive call, the stored factors are 6, 7, 8, 9 and the return value is 6×7×8×9=3024 (A). If each call executes the recursive call before reading its right-hand x, all four factors are the final p=9 and the result is 9^4=6561 (B). Mixed choices can give other values. Under C++ rules sequencing a function’s execution as a unit relative to otherwise unsequenced outside evaluations, this is an unspecified operand-order issue; the recursive function call must not be confused with an unsequenced x++ written directly beside a read of x. The source does not prescribe an evaluation convention or a language version, so no unique numerical option follows. The official Code A key explicitly awards Marks to All. This record is classified under compiler parameter-passing/evaluation semantics, not as a valid C program.

Make the parameter environments explicit: each call has its own c, but every x refers to the same external p. The nonbase frames correspond to c after decrement 4, 3, 2, 1. Each increments p once, so p becomes 9 at the base return. A frame can fetch its multiplication operand before recursion, preserving its current 6, 7, 8or9, or fetch after recursion, seeing9. Thus a single deterministic answer requires an evaluation-order convention absent from the prompt. The official MTA must remain an adjudication label, not be replaced by whichever value one compiler happens to print.

Original paper, page 10 · Answer key

2 marks · MCQ
Report issue

Static links in nested procedure activations

Runtime Environments & Parameter Passing

Question 36, GATE 2012: In a lexically scoped block language, main declares A1 and A2. A1 calls A2. A2 declares A21 and calls A21; A21 calls A1. Main calls A1, producing the live call stack main→A1(first)→A2→A21→A1(second). Which activation-record/access-link diagram is correct? Let M, X, Y, Z, W denote these five activations respectively. Arrows below are access (static) links, and FP denotes the frame pointer.
(A) X→M, Y→X, Z→Y, W→Z; FP=W.
(B) Y→M, Z→Y, W→Z; no access link is drawn for X; FP=W.
(C) Only M,X,Y,Z are drawn: X→M, Y→M, Z→Y; W is absent and FP=X.
(D) X→M, Y→M, Z→Y, W→M; FP=W.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

An access link points to the activation of the lexically enclosing procedure, not generally to the caller. Both A1 activations and A2 are declared directly in Main, so all three point to Main. A21 is declared in A2, so its access link points to that A2 activation. The frame pointer is at the latest (bottom) A1 activation. Option D depicts precisely these static links; caller links instead would form the dynamic call chain.

Check: The second A1 was called by A21, but A1’s declaration remains inside main. Dynamic recursion changes which activation is current, not the lexical parent. Thus its static link skips both A21 and A2, while its dynamic caller link would point to A21.

Original paper, page 9 · Answer key

1 mark · MCQ
Report issue

Runtime heap allocation

Runtime Environments & Parameter Passing

Question 14, GATE 2010: Which class of programming languages necessarily needs heap allocation in its runtime environment?
Options: (A) Languages supporting recursion; (B) Languages using dynamic scoping; (C) Languages allowing dynamic data structures; (D) Languages using global variables.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Dynamically created data structures can have sizes and lifetimes unrelated to the last-in-first-out nesting of procedure activations, so their runtime storage requires heap-style allocation in the standard implementation model. Recursion and dynamic scope can be supported with activation stacks; global variables can use static storage. C is the intended distinction. A language implementation may realize the allocator through a preallocated arena, but that is still heap-style management rather than a counterexample to the storage-lifetime distinction.

A recursive call creates a new activation record that can be popped when the call returns, so recursion alone needs a stack, not a heap. A linked node returned to a caller may have to survive its creator’s return and be freed in an arbitrary order. That non-LIFO lifetime is the key distinction tested by dynamic data structures.

Original paper, page 3

2 marks · MCQ
Report issue

Activation records, recursion and escaping closures

Runtime Environments & Parameter Passing

Question 54, GATE 2008: Which are true? I: a language without globals or nested procedures, but with recursion, can use static storage allocation. II: multilevel access links/displays are needed only for languages with nested procedures. III: recursion cannot use dynamic storage allocation. IV: nesting and recursion require heap allocation and cannot use stack activation records. V: languages allowing a function to return a function cannot use only stack allocation for activation records.

(A) II and V only
(B) I,III,IV only
(C) I,II,V only
(D) II,III,V only
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. Under the standard language-runtime assumptions, II and V are true, giving A. I is false: recursive calls can have arbitrarily many simultaneously live instances of local variables, so one statically allocated activation record per procedure is insufficient even without globals or nested procedures.
2. II is true: multilevel static access links/displays serve access to lexically enclosing procedures; without lexical nesting no multilevel environment chain is needed. III is false because dynamic stack allocation supports ordinary recursion.
3. IV is false because nested procedures and recursion alone can use stack activation records with static/dynamic links. V describes a returned nested function that can retain its defining environment after the creator returns: pure stack allocation cannot preserve that environment, so escaping environments need extended lifetime, normally heap allocation or copying.
4. The literal statement V is too broad without that escaping-closure assumption: a function may return a pointer to a global function, or a function with no captured environment, while ordinary activation records remain stack allocated. Thus A is the intended answer under the nested escaping-function interpretation, not a universal consequence of merely returning any function.

Original paper, page 11

2 marks · MCQ
Report issue

Pass-by-reference with an expression argument

Runtime Environments & Parameter Passing

Question 56, GATE 2006: A pedagogical pass-by-reference language executes:
subroutine swap(ix,iy)
 it=ix
 L1: ix=iy
 L2: iy=it
end
ia=3; ib=8; call swap(ia,ib+5); print ia,ib.
Statements: S1 an unnamed temporary initialized to 13 is allocated and its address passed for the second argument; S2 runtime error at L1; S3 runtime error at L2; S4 prints 13,8; S5 prints 13,−2. Which stated set is correct under the expression-to-temporary model?
Options:
(A) S1 and S2
(B) S1 and S4
(C) S3
(D) S1 and S5
Question 56, GATE 2006: A pedagogical pass-by-reference language executes:
subroutine swap(ix,iy)
 it=ix
 L1: ix=iy
 L2: iy=it
end
ia=3; ib=8; call swap(ia,ib+5); print ia,ib.
Statements: S1 an unnamed temporary initialized to 13 is allocated and its address passed for the second argument; S2 runtime error at L1; S3 runtime error at L2; S4 prints 13,8; S5 prints 13,−2. Which stated set is correct under the expression-to-temporary model?
Options:
(A) S1 and S2
(B) S1 and S4
(C) S3
(D) S1 and S5
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Initially ia=3 and ib=8; the expression ib+5 evaluates to 13. Under the stated expression-to-temporary reference model, the compiler passes the address of a temporary containing 13, not the address of ib. swap sets ia to 13 and the temporary to 3; ib remains 8.

2. S1 and S4 hold, so B. This is the pedagogical model specified by the options, not a guarantee that every actual FORTRAN standard/compiler permits defining an expression-associated dummy argument.

3. Teaching check: The expression ib+5 has a value but is not the storage cell ib. Passing a temporary permits swap to overwrite the argument without overwriting ib. Trace the two addressed cells separately: ia changes 3→13, temporary changes 13→3, and ib stays 8.

Original paper, page 8

2 marks · MCQ
Report issue

Static scope with call-by-need evaluation

Runtime Environments & Parameter Passing

Question 73, GATE 2003: A hypothetical language permits globals but no nested function declarations:
global int i=100, j=5;
void P(x) {
  int i=10;
  print(x+10);
  i=200;
  j=20;
  print(x);
}
main() { P(i+j); }
If the language uses static scoping and call-by-need, what values are printed?
A. 115,220  B. 25,220  C. 25,15  D. 115,105
Question 73, GATE 2003: A hypothetical language permits globals but no nested function declarations:
global int i=100, j=5;
void P(x) {
  int i=10;
  print(x+10);
  i=200;
  j=20;
  print(x);
}
main() { P(i+j); }
If the language uses static scoping and call-by-need, what values are printed?
A. 115,220  B. 25,220  C. 25,15  D. 115,105
Question 73, GATE 2003: A hypothetical language permits globals but no nested function declarations:
global int i=100, j=5;
void P(x) {
  int i=10;
  print(x+10);
  i=200;
  j=20;
  print(x);
}
main() { P(i+j); }
If the language uses static scoping and call-by-need, what values are printed?
A. 115,220  B. 25,220  C. 25,15  D. 115,105
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Under static scope the actual expression i+j is bound to the globals visible at the call in main. P's local i does not replace that lexical binding.
2. Call-by-need postpones evaluation until first use and caches its result. At print(x+10), the actual is first demanded: global i+j=100+5=105. The first print is 105+10=115.
3. P then sets its own local i to 200 and global j to 20. Neither assignment changes the cached value 105.
4. The second print reuses that value, giving 105. Therefore D. Recomputing 100+20 would be call-by-name, not call-by-need.

Solution: Under lexical scope the delayed actual expression i+j refers to main's enclosing global bindings, initially 100 and 5. Its first demand evaluates and memoizes 105, so print(x+10) emits 115. Assigning 200 to P's local i and 20 to global j cannot change the already cached actual value. The later print(x) emits 105. Thus D, 115 followed by 105. Call by need evaluates once on first demand; it is not repeated call-by-name evaluation.

Original paper, page 11

2 marks · MCQ
Report issue

Dynamic scope with call-by-name evaluation

Runtime Environments & Parameter Passing

Question 74, GATE 2003: A hypothetical language permits globals but no nested function declarations:
global int i=100, j=5;
void P(x) {
  int i=10;
  print(x+10);
  i=200;
  j=20;
  print(x);
}
main() { P(i+j); }
If the language uses dynamic scoping and call-by-name, what values are printed?
A. 115,220  B. 25,220  C. 25,15  D. 115,105
Question 74, GATE 2003: A hypothetical language permits globals but no nested function declarations:
global int i=100, j=5;
void P(x) {
  int i=10;
  print(x+10);
  i=200;
  j=20;
  print(x);
}
main() { P(i+j); }
If the language uses dynamic scoping and call-by-name, what values are printed?
A. 115,220  B. 25,220  C. 25,15  D. 115,105
Question 74, GATE 2003: A hypothetical language permits globals but no nested function declarations:
global int i=100, j=5;
void P(x) {
  int i=10;
  print(x+10);
  i=200;
  j=20;
  print(x);
}
main() { P(i+j); }
If the language uses dynamic scoping and call-by-name, what values are printed?
A. 115,220  B. 25,220  C. 25,15  D. 115,105
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Call-by-name reevaluates the actual expression i+j whenever x is used. Under the stipulated dynamic scope, name lookup uses the nearest active bindings.
2. During the first print, P's local i=10 is nearest, while j resolves to global 5. Thus x=15 and x+10=25.
3. The assignments make the active local i=200 and global j=20. The second use reevaluates i+j as 220, rather than retaining 15.
4. The output is 25,220, B. This is specifically the question's dynamic-name interpretation, not lexical capture of main's i.

Solution: In the question's dynamic-scoping, call-by-name model, the actual expression i+j is reevaluated at each use with the active dynamic bindings. At the first print, i is P's local 10 and j is the global 5, so x+10 is 25. P then changes its local i to 200 and global j to 20. The second evaluation of x gives 220. Hence B. This uses the stipulated dynamic-name lookup rather than lexical capture of the actual expression.

Original paper, page 11

1 mark · MCQ
Report issue

Copy-restore and reference parameters

Runtime Environments & Parameter Passing

Question 1.18, GATE 2002: Compare function results under value-result and reference parameter passing. They:
(A) Never differ; (B) Differ when loops are present; (C) Differ in every case; (D) May differ when exceptions are present.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step 1: Use an exceptional exit to distinguish parameter mechanisms.

Step 2: Suppose an actual variable initially holds 0 and the function sets its formal to 1 before raising an exception. Reference passing has already changed the actual to 1. Under copy-restore with copying back only at normal return, the actual remains 0. This is a possible difference, not a claim that all calls differ or that exceptions have one universal language-independent copy-out policy.

Conclusion: With reference passing, updates immediately affect the actual argument. With value-result (copy-in/copy-out), they can remain in the local formal until normal return. If an exception exits before copy-out, the visible results can differ, D, under the conventional no-copy-out-on-exception rule. Aliasing can also distinguish these mechanisms; loops alone neither force nor exclude a difference.

Original paper, page 2

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

Reference parameters with dynamic scope

Runtime Environments & Parameter Passing

Question 2.19, GATE 2001: Program P2 has outer n=10. Procedure D declares local n=3 and calls W(n). W takes x by reference, increments x, and prints x. Under dynamic scoping, choose output 10,11,3, or none.
Tap to enlarge
Your answer
Answer & solution

Answer: D; the value printed is 4 (no official key was published)

Step 1: The outer n is set to 10, but procedure D declares its own n and sets it to 3. At the call W(n), the actual argument is that active local variable.

Step 2: Reference passing binds W’s x to D’s n. Executing x=x+1 changes it from 3 to 4; print x therefore prints 4. The outer n remains 10.

Since the listed numeric choices are 10,11,3, the answer is none of these, D. The formal binding already identifies the object, so dynamic scope does not redirect x to the outer n.

Original paper, page 6

1 mark · MCQ
Report issue

Meaning of aliasing

Runtime Environments & Parameter Passing

Question 1.16, GATE 2000: Does aliasing mean multiple names for one memory location, equal values, repeated identifiers, or repeated use of a variable?
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step 1: Aliasing occurs when two accessible expressions designate the same storage object. For example, if p=&x, then x and *p refer to the same int.

Step 2: Assigning through either expression changes what the other observes. Two separate variables can hold equal values without aliasing, and the same identifier can name different objects in different scopes.

Thus A, not merely equal contents or repeated spelling.

Original paper, page 2

2 marks · MSQ
Report issue

Dynamic scope through a caller

Runtime Environments & Parameter Passing

Question 2.13, GATE 1999: Global x is set to 0.25. show prints x. small has local x=0.125 and calls show. Main calls show,then small, using dynamic scope.
Question 2.13, GATE 1999: Global x is set to 0.25. show prints x. small has local x=0.125 and calls show. Main calls show,then small, using dynamic scope.
Tap to enlarge
Select all that apply
Answer & solution

Answer: C: 0.25,0.125 (no official key was published)

Dynamic lookup searches active calling environments. In the first show call, no nearer local x is active, so the global value 0.25 is printed. In the second call, small is the active caller and its local x=0.125 is the nearest binding, so show prints 0.125. Thus C. Under lexical scope, show’s definition outside small would instead resolve both calls to the global binding; the explicit dynamic-scoping premise is therefore essential.

Original paper, page 4

5 marks · Descriptive
Report issue

Draw activation stacks and static links

Runtime Environments & Parameter Passing

Question 16, GATE 1999: In a Pascal skeleton, program sort declares a,x and the sibling procedures readarray, exchange, qsort. qsort declares k,v and a nested function partition, which declares i,j. The active call sequence is sort → qsort(1,9) → qsort(1,3) → partition(1,3) → exchange(1,3). Draw the runtime stack with access links after each activation.
Tap to enlarge
Answer & solution

Answer: Both qsort frames and exchange link statically to sort; partition links to qsort(1,3). (no official key was published)

Access (static) links follow lexical nesting, whereas control (dynamic) links follow the caller. Label frames S=sort, Q1=qsort(1,9), Q2=qsort(1,3), P=partition(1,3), E=exchange(1,3). Each line below is a complete snapshot written bottom → top; the parenthesized target is the access link:

1. [S(outer/null)]
2. [S(outer/null)] [Q1(S)]
3. [S(outer/null)] [Q1(S)] [Q2(S)]
4. [S(outer/null)] [Q1(S)] [Q2(S)] [P(Q2)]
5. [S(outer/null)] [Q1(S)] [Q2(S)] [P(Q2)] [E(S)]

The final stack, top first, is:
E : static→S; dynamic→P
P : static→Q2; dynamic→Q2
Q2: static→S; dynamic→Q1
Q1: static→S; dynamic→S
S : static→outer/null; dynamic→outer/null

Recursive Q2 is not lexically declared inside Q1, so its static link must not point to Q1. Partition accesses its enclosing qsort variables such as v through Q2, then global a through Q2→S. Exchange was declared directly inside sort, so its a and x references use E→S, even though partition called it. readarray is not active and has no frame in these snapshots. Return addresses and saved registers may also occupy frames but do not change the links.

Original paper, page 8

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 · MCQ
Report issue

Constant-time access to lexical ancestors

Runtime Environments & Parameter Passing

Question 2.15, GATE 1998: An array of pointers to activation records used to speed nonlocal variable access is called stack, heap, display, or activation tree?
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

A display stores the current activation-record pointer for each lexical nesting level. A nonlocal variable’s declaration level and fixed offset then identify its address through one indexed display lookup plus the offset. This avoids following a chain of static links through multiple records. The structure is called a display, C. A stack stores records themselves, a heap supports general dynamic allocation, and an activation tree describes call relationships rather than this direct-access table.

Original paper, page 5

1 mark · MCQ
Report issue

Compiling runtime-sized arrays

Runtime Environments & Parameter Passing

Question 1.8, GATE 1997: A language permits arrays whose sizes are unknown at compile time and requires efficient memory use. Can it be compiled using static allocation, dynamic allocation, or only interpreted?
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

The compiler can generate runtime code that evaluates an array’s size, allocates that much storage and records its address and dimensions. Dynamic allocation therefore handles unknown compile-time sizes while avoiding allocation for an arbitrary worst-case bound, giving C. Interpretation is not necessary: compilation can defer particular operations until runtime. Static allocation alone cannot efficiently reserve exactly the required unknown sizes in the general case.

Original paper, page 1

1 mark · MCQ
Report issue

Why a language needs heap storage

Runtime Environments & Parameter Passing

Question 1.10, GATE 1997: Choose the feature motivating heap allocation: recursion, dynamic data structures, dynamic scope, none.
Tap to enlarge
Your answer
Answer & solution

Answer: B / listed item 2 (no official key was published)

Recursion normally creates nested activation lifetimes, which a stack handles. Dynamic scope concerns name lookup and does not itself force arbitrary object lifetimes. General dynamic structures such as linked nodes may be allocated and released in an order unrelated to calls and returns, so they need storage managed outside a strict activation stack: the heap, item 2. This is the conventional distinction. Particular bounded or region-scoped structures could use specialized pools, so “required” refers to supporting general dynamic lifetimes, not every individual dynamic object.

Original paper, page 2

2 marks · MCQ
Report issue

Lexical scope across sibling procedures

Runtime Environments & Parameter Passing

Question 4.8, GATE 1997: A declares x, y. Nested B declares x, z and contains S1; sibling C declares i and contains S2. Which variables are visible?
Question 4.8, GATE 1997: A declares x, y. Nested B declares x, z and contains S1; sibling C declares i and contains S2. Which variables are visible?
Tap to enlarge
Your answer
Answer & solution

Answer: C: S1 sees B.x, z, A.y; S2 sees A.x, A.y, C.i (no official key was published)

Lexical lookup searches the enclosing declaration chain. At S1, the local x in B shadows A’s x; B’s z and A’s y are accessible. At S2, procedure C is a sibling of B, not nested inside B, so B’s x and z are inaccessible. C sees its own i and the x, y declared in A. This is option C. The dynamically most recent caller is irrelevant in this Pascal-like lexical-scope question.

Original paper, page 4

5 marks · Descriptive
Report issue

Five activation records with nested recursion

Runtime Environments & Parameter Passing

Question 24, GATE 1997: Consider this Pascal nesting and call structure, with no calls other than those shown. Main declares X; A, nested in Main, declares Y; B, nested in A, declares Z; C, nested in B, also declares Z. Main calls A; A calls B; B calls C and, after C returns, calls A. C contains no procedure calls. At an instant there are five activation records on the runtime stack. Show its top, static and dynamic links, and local variables. Source skeleton:
program Main; var X: integer;
  procedure A; var Y: integer;
    procedure B; var Z: integer;
      procedure C; var Z: integer;
      begin {no procedure calls} end;
    begin C; A end;
  begin B end;
begin A end.
Ellipses in the source allow other non-call work; the skeleton preserves every shown declaration and call.
Question 24, GATE 1997: Consider this Pascal nesting and call structure, with no calls other than those shown. Main declares X; A, nested in Main, declares Y; B, nested in A, declares Z; C, nested in B, also declares Z. Main calls A; A calls B; B calls C and, after C returns, calls A. C contains no procedure calls. At an instant there are five activation records on the runtime stack. Show its top, static and dynamic links, and local variables. Source skeleton:
program Main; var X: integer;
  procedure A; var Y: integer;
    procedure B; var Z: integer;
      procedure C; var Z: integer;
      begin {no procedure calls} end;
    begin C; A end;
  begin B end;
begin A end.
Ellipses in the source allow other non-call work; the skeleton preserves every shown declaration and call.
Tap to enlarge
Answer & solution

Answer: Bottom to top: Main, A1, B1, A2, B2. Dynamic links follow the call chain; static links B2→A2, A2→Main, B1→A1, A1→Main. (no official key was published)

Distinguish a procedure's lexical parent from the activation that calls it. The dynamic link records the caller; the static link records the active instance of the enclosing lexical scope. Calls to A from inside B are recursive calls to a procedure declared directly inside Main, not declarations of a new A inside B.

Trace the shown calls. Main calls A1, which calls B1. B1's call to C1 creates only the fourth record, and C1 makes no further calls. C1 returns before the following call to A2, so its record has been removed. A2 is then the fourth record, and its call to B2 creates the fifth. At any matching five-record stage of this call pattern, the active chain is therefore:

Stack position   Activation   Local slot   Dynamic link   Static link
TOP              B2           B2.Z         A2             A2
                 A2           A2.Y         B1             Main
                 B1           B1.Z         A1             A1
                 A1           A1.Y         Main           Main
BOTTOM           Main         Main.X       null           null

Other ordinary record fields, such as a return address and saved registers, may exist but are not assigned values by the problem. A1.Y and A2.Y are distinct storage cells, as are B1.Z and B2.Z. No numeric values for X,Y,Z can be recovered from the ellipses. The completed C1 record, including its separate C1.Z slot, is absent.

The dynamic chain is B2→A2→B1→A1→Main. The different static chain from B2 is B2→A2→Main. In particular, A2's static link must not point to B1 merely because B1 called it. To create A2, B1 can follow its own static chain B1→A1→Main to find A's lexical parent. This is the central distinction the example tests.

Original paper, page 10

2 marks · MCQ
Report issue

A value parameter shadows a global

Runtime Environments & Parameter Passing

Question 2.4, GATE 1995: Pascal has global integer X=2 and procedure FIND(X:real) assigning X:=sqrt(X). After FIND(X), what does printing global X produce?
Tap to enlarge
Your answer
Answer & solution

Answer: A: 2 under the intended Pascal value-parameter program (no official key was published)

Without the keyword var, the procedure parameter is passed by value. Its local X receives a real copy of the argument and shadows the global name inside FIND. Assigning sqrt(X) changes only that local copy; it does not assign to the global integer. On return the global X is still 2, so printing it yields 2. The the paper omits some statement separators; this analysis uses the intended syntactically separated program rather than treating formatting omissions as executable Pascal.

Original paper, page 3

2 marks · MCQ
Report issue

Storage of a Pascal variant record

Runtime Environments & Parameter Passing

Question 2.11, GATE 1995: A record has one integer number and alternative fields (x,y:integer) or (p,q:real). With 4-byte integers and 8-byte reals, how much storage for 100 records?
Question 2.11, GATE 1995: A record has one integer number and alternative fields (x,y:integer) or (p,q:real). With 4-byte integers and 8-byte reals, how much storage for 100 records?
Tap to enlarge
Your answer
Answer & solution

Answer: C: 2000 bytes without padding or a stored tag (no official key was published)

Alternative fields share the same region; the record does not store both variants simultaneously. The integer pair needs 8 bytes and the real pair 16, so the variant region must reserve their maximum, 16. Add the common integer field’s 4 bytes to get 20 per record, or 2000 for 100. This assumes the abstract packed/no-padding model and no separately stored discriminant, as the intended choices do. An implementation aligning 8-byte real fields might use 24 bytes per record instead; byte sizes alone do not determine actual ABI padding.

Original paper, page 4

Descriptive
Report issue

Resolve Pascal record scopes

Runtime Environments & Parameter Passing

Question 10(a), GATE 1995: procedure T(U,V,X,Y:integer); var A:record A,B:integer end; B:record B,A:integer end; begin with A do begin A:=4; B:=V end; with B do begin A:=X; B:=Y end end. Identify the declarations bound to each A and B occurrence.
Tap to enlarge
Answer & solution

Answer: First with assigns A.A=4 and A.B=V; second assigns B.A=X and B.B=Y. (no official key was published)

There are six distinct declarations with these spellings: two outer record variables, outer A and outer B, and two integer fields inside each record. A field name belongs to its record’s field namespace; declaring field A does not replace the surrounding record variable A.

In “with A do”, the selector A is resolved before entering the with environment, so it denotes the outer record variable A. Within that body, unqualified field identifiers take precedence: the assignment A:=4 means outer-A.A:=4, while B:=V means outer-A.B:=V. V remains the procedure parameter.

That with scope ends at its end. Therefore the selector in the following “with B do” denotes the outer record B, not the field B from the preceding block. Within the second body, A:=X means outer-B.A:=X and B:=Y means outer-B.B:=Y. X and Y are parameters. The field declarations in the first record define A.A and A.B; those in the second define B.B and B.A. Parameter U is unused in the displayed the paper.

Original paper, page 6

Report issue

Historical FORTRAN storage and recursion

Runtime Environments & Parameter Passing

Question 1.1, GATE 1994: Why did the historical FORTRAN implementations described not permit recursion: static allocation, dynamic allocation, absent hardware stacks, or impossibility on some machines?
Tap to enlarge
Your answer
Answer & solution

Answer: A: static allocation in the historical implementation model (no official key was published)

A single statically allocated location for each local variable provides only one activation’s storage. A recursive call would overwrite values still needed by its caller, so that implementation cannot support general recursion without changing its storage scheme. A stack of activation records is the usual solution; a hardware stack is not essential because software can implement one. Thus A explains the historical restriction. It is not a claim about all modern Fortran: later standards and implementations support recursive procedures with appropriate activation storage.

Original paper, page 1

Report issue

Call by name versus call by reference

Runtime Environments & Parameter Passing

Question 1.20, GATE 1994: Which argument can expose different results between call-by-name and call-by-reference: constant, array address, whole array, or an array element?
Tap to enlarge
Your answer
Answer & solution

Answer: D (array element) (no official key was published)

For an actual argument a[i], call-by-reference fixes the target location at call time. Call-by-name reevaluates the argument expression each time the formal is used. For example start i=1 and call a procedure whose body sets i:=2 then assigns 9 to the formal. Reference modifies a[1], while name modifies a[2]. Thus an array element whose index can change demonstrates the difference.

Original paper, page 3

Descriptive
Report issue

Coroutines and subroutines

Runtime Environments & Parameter Passing

Question 17(a), GATE 1994: State true or false with reason: A coroutine is just another name for a subroutine.
Question 17(a), GATE 1994: State true or false with reason: A coroutine is just another name for a subroutine.
Tap to enlarge
Answer & solution

Answer: False. (no official key was published)

1. An ordinary subroutine is entered by a call and returns control to its caller; a fresh call normally begins execution at its entry point.

2. A coroutine can suspend at a yield/resume point, preserve its execution context, and later continue from that point. Control may transfer cooperatively between peer coroutines.

3. One can implement coroutines using lower-level mechanisms, but the two terms do not name the same control-flow abstraction.

Original paper, page 6

Descriptive
Report issue

Lexical scope in sibling procedures

Runtime Environments & Parameter Passing

Question 22(i), GATE 1994: main declares global integer r. Procedure two, declared directly inside main, executes write(r). Sibling procedure one declares its own local r, assigns it 5, then calls two. main assigns its r=2 and executes two; one; two. What values are printed under static scoping?
Question 22(i), GATE 1994: main declares global integer r. Procedure two, declared directly inside main, executes write(r). Sibling procedure one declares its own local r, assigns it 5, then calls two. main assigns its r=2 and executes two; one; two. What values are printed under static scoping?
Question 22(i), GATE 1994: main declares global integer r. Procedure two, declared directly inside main, executes write(r). Sibling procedure one declares its own local r, assigns it 5, then calls two. main assigns its r=2 and executes two; one; two. What values are printed under static scoping?
Question 22(i), GATE 1994: main declares global integer r. Procedure two, declared directly inside main, executes write(r). Sibling procedure one declares its own local r, assigns it 5, then calls two. main assigns its r=2 and executes two; one; two. What values are printed under static scoping?
Tap to enlarge
Answer & solution

Answer: 2, 2, 2 (without separators, 222). (no official key was published)

Static, or lexical, scoping resolves a name by the nesting of declarations in the program text, not by whichever procedure happened to call the current procedure. Procedure two has no local declaration of r. Its immediately enclosing lexical scope is main, so every reference to r in two denotes main's r. Procedure one's local r is in a sibling procedure's scope and is not visible inside two.

After main assigns r=2, its first call to two prints 2. Next main calls one, which creates a separate local r and sets that local variable to 5. The call from one to two still resolves two's r to main's unchanged variable and therefore prints 2. Returning from one removes its local activation without changing main's r. The final direct call to two again prints 2.

Thus the sequence of values is 2,2,2. The pseudocode uses write rather than writeln and requests no separators, so a literal output stream would commonly appear as 222. Formatting conventions do not change the three values.

Original paper, page 6

Descriptive
Report issue

Dynamic scope follows the call chain

Runtime Environments & Parameter Passing

Question 22(ii), GATE 1994: main declares global integer r. Procedure two, declared directly inside main, executes write(r). Sibling procedure one declares its own local r, assigns it 5, then calls two. main assigns its r=2 and executes two; one; two. What values are printed under dynamic scoping?
Question 22(ii), GATE 1994: main declares global integer r. Procedure two, declared directly inside main, executes write(r). Sibling procedure one declares its own local r, assigns it 5, then calls two. main assigns its r=2 and executes two; one; two. What values are printed under dynamic scoping?
Question 22(ii), GATE 1994: main declares global integer r. Procedure two, declared directly inside main, executes write(r). Sibling procedure one declares its own local r, assigns it 5, then calls two. main assigns its r=2 and executes two; one; two. What values are printed under dynamic scoping?
Question 22(ii), GATE 1994: main declares global integer r. Procedure two, declared directly inside main, executes write(r). Sibling procedure one declares its own local r, assigns it 5, then calls two. main assigns its r=2 and executes two; one; two. What values are printed under dynamic scoping?
Tap to enlarge
Answer & solution

Answer: 2, 5, 2 (without separators, 252). (no official key was published)

Under dynamic scoping, resolve an unbound local name by searching active callers, nearest first. Procedure two itself has no r. In the first call main→two, the closest active declaration is main's r, already set to 2, so two prints 2.

The second relevant call chain is main→one→two. Procedure one has an active local r initialized to 5. The search from two finds that declaration before reaching main, so this invocation prints 5. Procedure two merely reads the variable; it does not copy the value into main's r or assign to either declaration.

After one returns, its local r is no longer in the active call chain. The last main→two call therefore finds main's original r=2 and prints 2. The resulting sequence is 2,5,2, or 252 if ordinary write operations introduce no spaces. The key distinction is lifetime plus dynamic caller order: the same body of two can access different storage on different calls under dynamic scoping, whereas static scoping would always bind its r to main's lexical declaration. This result concerns the hypothetical dynamic-scoping interpretation requested, not the default rules of Pascal.

Original paper, page 6

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

2 marks · MSQ
Report issue

A value parameter does not modify its actual expression

Runtime Environments & Parameter Passing

Question 7.2, GATE 1993: Global n=0. Q(x:integer;var y:integer) executes x:=y+1. What is printed after Q(n*1,n)?
Question 7.2, GATE 1993: Global n=0. Q(x:integer;var y:integer) executes x:=y+1. What is printed after Q(n*1,n)?
Tap to enlarge
Select all that apply
Answer & solution

Answer: B: 0 (no official key was published)

The actual n*1 is evaluated to zero and copied into value parameter x. Parameter y is a reference to n. The assignment computes y+1=1 but writes only the local copy x; it never assigns to y. Thus n remains zero and the caller prints zero. Calling with an expression as the first actual is valid precisely because that parameter has value semantics; a reference formal would require a suitable variable location instead.

Original paper, page 6

2 marks · MSQ
Report issue

Scope and shadowing of a global name

Runtime Environments & Parameter Passing

Question 7.3, GATE 1993: Global m is declared in PARAM. P declares another local m; Q does not. Where is the global name m directly visible?
Question 7.3, GATE 1993: Global m is declared in PARAM. P declares another local m; Q does not. Where is the global name m directly visible?
Tap to enlarge
Select all that apply
Answer & solution

Answer: C: PARAM and Q (no official key was published)

Lexical lookup in P finds its local m before reaching the enclosing declaration, so unqualified m there denotes the local variable. In Q, which has no local m, lookup reaches the global declaration in PARAM. The same global name is visible in the main block itself. Hence PARAM and Q are the regions of direct visibility, C. The global object can still be accessed indirectly through P’s reference parameters; object accessibility is distinct from whether its identifier is shadowed.

Original paper, page 6

Descriptive
Report issue

Passing reference and value parameters

Runtime Environments & Parameter Passing

Question 26(a), GATE 1993: Procedure P(var x:integer; y:integer) is called as P(a,b). What is pushed on the argument stack for a and b?
Question 26(a), GATE 1993: Procedure P(var x:integer; y:integer) is called as P(a,b). What is pushed on the argument stack for a and b?
Tap to enlarge
Answer & solution

Answer: Address of a for x; current integer value of b for y. (no official key was published)

The var qualifier gives x reference semantics: P must be able to access and modify the caller's actual variable a. Therefore pass a's storage address, not merely its current contents. The unqualified integer y is a value parameter, so evaluate b and push a copy of its current integer value.

After entry, assigning to x changes a because x denotes a through the supplied reference. Assigning to y changes only the callee's copy, leaving b unchanged. Passing the address of b for y would instead implement reference semantics; passing only a's value for x would lose the required alias to the caller's variable.

The question concerns argument payloads, not a complete activation-record layout. Return addresses, saved links and registers may also occupy the stack, but their presence does not change these two payloads. Argument order and stack growth direction depend on the calling convention and are not specified, so no particular push order or numeric offsets should be invented.

Original paper, page 14

Descriptive
Report issue

Addressing formal parameters

Runtime Environments & Parameter Passing

Question 26(b), GATE 1993: Procedure P(var x:integer; y:integer) receives the address of actual a and value of actual b on its stack. How does generated addressing for x differ from y?
Question 26(b), GATE 1993: Procedure P(var x:integer; y:integer) receives the address of actual a and value of actual b on its stack. How does generated addressing for x differ from y?
Question 26(b), GATE 1993: Procedure P(var x:integer; y:integer) receives the address of actual a and value of actual b on its stack. How does generated addressing for x differ from y?
Tap to enlarge
Answer & solution

Answer: x uses an indirect load/store through its pointer slot; y uses its own value slot directly. (no official key was published)

With a frame pointer FP and symbolic offsets ox,oy, the slot at FP+ox contains an address, whereas the slot at FP+oy contains an integer. To read x, first load ptr:=memory[FP+ox], then load memory[ptr]. To assign x:=v, store v into memory[ptr]. These operations reach the caller's a.

To read y, load memory[FP+oy] directly. To assign y:=v, store v into that same local argument slot. This changes the callee's copied value, not b. Thus reference-parameter access has one extra level of indirection relative to a direct stack-slot value parameter, even if a machine's addressing modes combine the micro-operations in one instruction.

The offsets remain symbolic because the source specifies neither stack layout nor saved control fields. If the compiler caches a parameter in a register, it must preserve the same observable aliasing behavior; such optimization does not change the semantic distinction being asked about.

Original paper, page 14

Descriptive
Report issue

Correct copy-in copy-out implementation

Runtime Environments & Parameter Passing

Question 10(a), GATE 1992: For P(A[i]), where P has an integer formal parameter, a proposed implementation creates local z, sets z:=A[i], runs P’s body using z, then writes A[i]:=z. Identify the parameter-passing mechanism, judge correctness and give a small correction.
Tap to enlarge
Answer & solution

Answer: Call by value-result (copy-in/copy-out); capture the actual argument’s location at call time and copy back to that same location. (no official key was published)

The separate local variable receives the initial value and is copied back on return, so the intended mechanism is call by value-result, also called copy-in/copy-out. It is not call by reference, which uses the actual storage throughout execution, nor call by value, which never copies a final value back. Call by name re-evaluates an actual-argument expression in its caller environment on each use rather than using one private copied value.

The proposed copy-back re-evaluates A[i]. If the caller’s index changes during execution, it can select a different element from the one used at entry. For example initially i=2 and A[2]=10. Suppose the body changes the caller-visible index to 3 through a global reference and leaves the formal’s local value as 99. Re-evaluating A[i] on exit writes A[3]=99 instead of copying back into A[2]. An implementation must preserve the original actual location under the usual location-fixed value-result convention.

Small correction

p:=address_of(A[i]);   {evaluate actual location in caller environment once}
z:=value_at(p);       {copy in}
execute P body with every occurrence of its formal bound to z;
value_at(p):=z;       {copy out to saved location}
If A is known not to change identity or base address, saving k:=i and using A[k] for both transfers is sufficient; saving the address is the general correction. Normal return is assumed, and with one parameter there is no multiple-copy-back ordering issue.

There is also a naming issue in the printed wording: P’s formal is named i, while the actual A[i] contains the caller’s i. They are different bindings. The body must substitute z for the formal, not literally search for the caller expression A[i] in the body. Merely assigning the formal does not itself change the caller’s separate index; the failure requires that the caller-visible index or array location can otherwise change. If that cannot happen, the original sequence happens to produce the right result.

Original paper, page 7

Descriptive
Report issue

Activation records and lexical display

Runtime Environments & Parameter Passing

Question 10(b), GATE 1992: For the following lexically scoped Pascal-like skeleton, draw activation records and the display just after the procedures invoked at labels x and y begin. Distinguish the two A declarations.
program Test;
  procedure A;                 {outer A}
    procedure B;
      procedure A;             {inner A}
      begin ... end;
    begin
      y: A;
    end;
  begin
    B;
  end;
begin
  x: A;
end.
Tap to enlarge
Answer & solution

Answer: At x: Test→A_outer with display [Test,A_outer]. At y: Test→A_outer→B→A_inner with display [Test,A_outer,B,A_inner]. (no official key was published)

Assign lexical levels Test=0, A_outer=1, B=2 and A_inner=3. The declarations, not the dynamic call order, determine these levels. The call x:A in Test resolves to A_outer. The call y:A in B resolves to the nearest enclosing declaration of A, namely B’s own nested A_inner; it does not recursively invoke A_outer.

Immediately after x’s callee starts, before A_outer executes B, the stack and display are:
Stack bottom→top:
[Test frame: caller/runtime link]
[A_outer frame: dynamic link→Test; static link→Test;
return continuation→after x:A; saved previous display[1]]
Display: D[0]→Test, D[1]→A_outer.
D[2] and D[3] have no active corresponding frames and are undefined/unused. Do not invent B or A_inner frames at this first instant.

A_outer then calls B, whose body calls A_inner at y. Immediately after y’s callee starts:

Stack bottom→top:
[Test]
[A_outer: DL→Test, SL→Test, return→after x:A, saved old D[1]]
[B: DL→A_outer, SL→A_outer, return→after B call, saved old D[2]]
[A_inner: DL→B, SL→B, return→after y:A, saved old D[3]]
Display diagram:
D[0] ──> Test
D[1] ──> A_outer
D[2] ──> B
D[3] ──> A_inner

The dynamic link names the calling activation, while the static link names the activation of the lexical parent. They happen to coincide throughout this simple nested call chain; their meanings remain different. A display-based implementation can replace static-chain access entirely, so the static links shown are explanatory rather than required additional storage. On entry at lexical level ℓ, save old D[ℓ] in the frame and set D[ℓ] to that frame; on return restore the saved value. Return addresses, saved registers and local-storage slots are conventional frame fields, but no particular byte sizes or actual locals are specified. The omitted body of A_inner has not yet executed at the requested second snapshot, so no additional calls or frames may be assumed.

Original paper, page 7

Descriptive
Report issue

Call by value with an expression argument

Runtime Environments & Parameter Passing

Question 9(a.i), GATE 1991: All variables are integers. Procedure P(a,b,c) executes a:=2; c:=a+b. Main executes x:=1; y:=5; z:=100; P(x,x*y,z); write(x,z). Determine the output for call by value.
Question 9(a.i), GATE 1991: All variables are integers. Procedure P(a,b,c) executes a:=2; c:=a+b. Main executes x:=1; y:=5; z:=100; P(x,x*y,z); write(x,z). Determine the output for call by value.
Tap to enlarge
Answer & solution

Answer: x=1, z=100. (no official key was published)

With call by value, evaluate the actual arguments in the caller before entering P: x yields 1, x*y yields 5, and z yields 100. Allocate independent local formals with values (a,b,c)=(1,5,100).

The assignment a:=2 changes only the local a. Next c:=a+b computes 2+5=7 and places it in the local c. Neither assignment targets the caller’s variables, so after return x remains 1, y remains 5, and z remains 100. The main program prints x=1,z=100.

The intermediate c=7 is not the value printed by the program: the print is outside P and names z. Ordinary call by value has no copy-out stage. Adding copy-out would change the parameter mechanism to value-result, which is a different question. Evaluating the expression x*y once at the call is crucial for distinguishing this case from call by name.

Original paper, page 6

Report issue

Reference parameters and expression actuals

Runtime Environments & Parameter Passing

Question 9(a.ii), GATE 1991: All variables are integers. Procedure P(a,b,c) executes a:=2; c:=a+b. Main executes x:=1; y:=5; z:=100; P(x,x*y,z); write(x,z). Determine the output for call by reference, stating any validity assumption.
Question 9(a.ii), GATE 1991: All variables are integers. Procedure P(a,b,c) executes a:=2; c:=a+b. Main executes x:=1; y:=5; z:=100; P(x,x*y,z); write(x,z). Determine the output for call by reference, stating any validity assumption.
Tap to enlarge
Answer & solution

Answer: Strict reference-only actuals: invalid call, no defined output. If expression actuals are materialized in a temporary: x=2,z=7. (no official key was published)

Pure call by reference binds each formal to an assignable storage location. The actuals x and z have such locations, but x*y is an arithmetic expression and has no variable location to which a conventional reference formal can bind. Thus in a language requiring variable actuals for reference parameters, this call is rejected and there is no output. This is an important qualification to the otherwise familiar textbook calculation.

If the intended pseudo-language permits an expression actual by evaluating it into a temporary, evaluate x*y=1·5=5 at call entry and bind b to that temporary. Bind a directly to x and c directly to z. Executing a:=2 sets x to 2. The temporary b remains 5; it is not a live expression. Then c:=a+b sets z=2+5=7. On return the program prints x=2,z=7.

The temporary convention is sufficient here because P never assigns to b. It is nevertheless an additional semantic rule, not inherent in reference passing. In particular, re-evaluating x*y after changing x would give 10 and would implement call by name rather than this temporary-backed reference convention.

Original paper, page 6

Report issue

Call by name re-evaluates the expression

Runtime Environments & Parameter Passing

Question 9(a.iii), GATE 1991: All variables are integers. Procedure P(a,b,c) executes a:=2; c:=a+b. Main executes x:=1; y:=5; z:=100; P(x,x*y,z); write(x,z). Determine the output for call by name.
Question 9(a.iii), GATE 1991: All variables are integers. Procedure P(a,b,c) executes a:=2; c:=a+b. Main executes x:=1; y:=5; z:=100; P(x,x*y,z); write(x,z). Determine the output for call by name.
Tap to enlarge
Answer & solution

Answer: x=2,z=12. (no official key was published)

Call by name delays evaluation of an actual argument until its formal is used, in the caller’s environment. Think of a as a getter/setter for x, b as a thunk returning x*y, and c as a getter/setter for z. The caller’s x and y, not unrelated locals inside P, are the names captured by b’s thunk.

The first statement a:=2 follows the location associated with x and changes x from 1 to 2. The next statement c:=a+b first obtains a=2 and evaluates b’s expression using the current caller state: x*y=2·5=10. It therefore writes 12 through c into z. After return, main prints x=2,z=12.

A textual substitution illustrates this particular collision-free example: x:=2; z:=x+x*y. General call-by-name implementations must preserve environments and avoid accidental name capture, so literal substitution is only an explanatory model. The difference from temporary-backed reference passing is that b is not frozen at its original value 5. The nonassignability of x*y does not prevent this example’s by-name use because b is only read, never assigned.

Original paper, page 6

Descriptive
Report issue

Lexical scope selects declaration context

Runtime Environments & Parameter Passing

Question 9(b.i), GATE 1991: Global integers a,b; procedure P sets a:=5;b:=10. Procedure Q declares its own local integers a,b and calls P. Main sets global a:=1;b:=2, calls Q, then prints global a,b. Give output under static scope.
Question 9(b.i), GATE 1991: Global integers a,b; procedure P sets a:=5;b:=10. Procedure Q declares its own local integers a,b and calls P. Main sets global a:=1;b:=2, calls Q, then prints global a,b. Give output under static scope.
Tap to enlarge
Answer & solution

Answer: a=5,b=10. (no official key was published)

Under static, or lexical, scope, a nonlocal name is resolved by the source program’s nesting structure. P is declared at global level, alongside Q, rather than inside Q. The closest enclosing declarations for the a and b used in P are therefore the global variables.

Main first assigns those global variables (1,2). Q then creates a fresh activation containing its own local a,b. Although Q calls P, Q’s locals are not lexically enclosing declarations of P. P’s assignments update the globals to (5,10), leaving Q’s locals untouched. Q returns and its activation disappears. Main then prints the global values a=5,b=10.

A useful implementation view is that P’s static link identifies the global environment, independent of which procedure called it. The existence of same-spelled variables in the caller does not change this link. Their uninitialized values are irrelevant because P never reads or writes them under static scoping.

Original paper, page 6

Report issue

Dynamic scope selects the active caller

Runtime Environments & Parameter Passing

Question 9(b.ii), GATE 1991: Global integers a,b; procedure P sets a:=5;b:=10. Procedure Q declares its own local integers a,b and calls P. Main sets global a:=1;b:=2, calls Q, then prints global a,b. Give output under dynamic scope.
Question 9(b.ii), GATE 1991: Global integers a,b; procedure P sets a:=5;b:=10. Procedure Q declares its own local integers a,b and calls P. Main sets global a:=1;b:=2, calls Q, then prints global a,b. Give output under dynamic scope.
Tap to enlarge
Answer & solution

Answer: a=1,b=2. (no official key was published)

Dynamic scope resolves a free name by searching the active call chain from the current activation outward. During P, the chain is P→Q→main/global. P has no local a or b; the nearest active bindings are Q’s local a,b. Hence P’s assignments set Q’s variables to (5,10), rather than modifying the globals.

The globals were initialized to (1,2) by main and are still (1,2) when Q returns. Q’s locals disappear with its activation, and the final print in main uses the globals. The output is a=1,b=2.

No undefined read is caused by Q’s initially uninitialized locals: P overwrites them before they could be read, and the final print occurs after they are out of scope. Dynamic scoping must not be confused with dynamic memory allocation or call-by-reference parameter passing. It is a rule for selecting which binding an identifier denotes; the assignment operation itself is unchanged.

Original paper, page 6