Consider the program below in a hypothetical programming language which allows…
2012
Consider the program below in a hypothetical programming language which allows global variables and a choice of static or dynamic scoping in it:
program Main()
{
i = 10;
call f();
}
procedure f()
{
int i = 20;
call g();
}
procedure g()
{
print i;
}Let x be the value printed under static scoping and y be the value printed under dynamic scoping. Then x and y are
Answer: A. x = 10, y = 20 — ConceptScoping decides which declaration a non-local name inside a procedure body refers to. Under static (lexical) scoping the binding is fixed at compile…
- A.
x = 10, y = 20
- B.
x = 20, y = 10
- C.
x = 20, y = 20
- D.
x = 10, y = 10
Attempted by 38 students.
Show answer & explanation
Correct answer: A
Concept
Scoping decides which declaration a non-local name inside a procedure body refers to. Under static (lexical) scoping the binding is fixed at compile time by the program text: the search starts in the procedure itself and moves outward through the blocks that lexically enclose it, ending at the global scope. Under dynamic scoping the binding is decided at run time by the chain of active calls: the search starts in the current activation record and moves down the run-time stack through the caller, the caller's caller, and so on, stopping at the first activation that declares the name.
Application to this program
The language allows global variables, so the undeclared assignment i = 10 in Main writes to the global i. The declaration int i = 20 inside f creates a fresh variable that belongs to f's own activation and leaves the global i untouched. Procedure g declares no i at all, so print i is a non-local reference and the scoping rule alone decides which binding it reads.
Main executes
i = 10. Since i is not declared anywhere in Main, this assigns 10 to the global variable i.Main calls f. Entering f pushes an activation record holding f's own i, initialised to 20; the global i still holds 10.
f calls g. Entering g pushes an activation record with no i in it, so
print imust look outside g.Static scoping: g is written at the top level of the program, beside f rather than inside it, so the only scope that lexically encloses g's body is the global scope. The lookup therefore finds the global i and prints 10, no matter which procedure happened to call g. Hence x = 10.
Dynamic scoping: when
print iruns, the live call chain is Main to f to g. Walking down the stack, g's activation has no i, and the next activation down is f's, which does declare i = 20. The lookup stops there and prints 20. Hence y = 20.
The two lookups side by side
Scoping rule | Search path used from g | Binding reached | Value printed |
|---|---|---|---|
Static (lexical) | g's own body, then the enclosing program text, i.e. the global scope | global i | 10 (this is x) |
Dynamic | g's activation, then the caller f's activation on the run-time stack | f's local i | 20 (this is y) |
Cross-check against the other pairings
x = 20, y = 10 would require lexical lookup to see the caller's local i while the run-time stack walk skips past that same live local, each rule doing the other's job.
x = 20, y = 20 would require lexical lookup from g to reach f's local i, but g is not nested inside f, so f's declaration is invisible to g in the program text.
x = 10, y = 10 would require the run-time stack walk to ignore f's live activation and jump straight to the global binding, which contradicts the innermost-active-declaration rule.
So the printed values are x = 10 under static scoping and y = 20 under dynamic scoping.
Explore the full course: Mppsc Assistant Professor Computer Science Paper 2