ACompressed recap
🧾
Static scoping binds a non-local name to the nearest enclosing declaration in the program text; dynamic scoping binds it to the most recently called active procedure that declares it. Trace by deciding, for each procedure, where its non-locals come from (text vs call chain), then simulate, remembering that assignments change that declaration. Nested procedures use access links (follow depth difference) or a display.
BMerged rule card
5.3 Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
- Scope: region where a declaration applies; non-local = used but not declared locally.
- Static: nearest enclosing declaration in the text (compile time).
- Dynamic: most recent active caller that declares it (run time, control links).
- Access link → record of the lexically enclosing procedure; hops = depth(user) − depth(declaration).
- Display d[i] → most recent record at depth i; save/restore d[i] on call/return.
CInterleaved GATE-style questions
Q1MCQ2 marksTrace
Output under static scoping?
int x = 5;
void A() { x = x + 1; print(x); }
void B() { int x = 10; A(); print(x); }
main() { B(); A(); print(x); }
- (A) 6, 10, 7, 7
- (B) 11, 11, 6, 6
- (C) 6, 10, 6, 6
- (D) 11, 10, 7, 7
Q2MCQ2 marksTrace
Same program under dynamic scoping:
- (A) 6, 10, 7, 7
- (B) 11, 11, 6, 6
- (C) 11, 10, 6, 6
- (D) 6, 11, 6, 6
Q3NAT2 marksTrace
Under static scoping, what is the first number printed?
int n = 5;
int f(int k) { return k * n; }
int g() { int n = 2; return f(3) + n; }
main() { int n = 10; print(g(), f(1)); }
Numerical answer: ________
Q4NAT2 marksTrace
Same program under dynamic scoping: what is the first number printed?
Numerical answer: ________
Q5MCQ1 markLinks
Access links follow:
- (A) the call chain
- (B) the lexical nesting of procedures
- (C) the heap
- (D) the order of declarations
Q6MCQ1 markDisplay
A display entry d[i] points to:
- (A) the oldest record at depth i
- (B) the most recent active record at nesting depth i
- (C) the caller’s record
- (D) the global area
Q7NAT1 markHops
A procedure at nesting depth 4 uses a variable declared at depth 2. How many access links are followed?
Numerical answer: ________
Q8MSQ2 marksFacts
Which are true?
- (A) Under both rules a local declaration hides an outer one with the same name.
- (B) Under dynamic scoping a procedure may change a caller’s local variable through a non-local name.
- (C) Static scoping needs the call chain to resolve names.
- (D) Most modern languages use static scoping.
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (A)2 marks
A always updates the global x: 6 then 7; B prints its own x 10.
- (B) is wrong: That is dynamic scoping.
- (C) is wrong: The second A makes x 7.
- (D) is wrong: A does not see B’s x statically.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q2 Answer: (B)2 marks
A called from B changes B’s x to 11 (printed twice); A from main changes the global x to 6 (printed twice). Computed: 11, 11, 6, 6.
- (A) is wrong: Static.
- (C) is wrong: B prints its own x, which A changed to 11.
- (D) is wrong: The first print is 11.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q3 Answer: 172 marks
f(3) uses the global n = 5 → 15, plus g’s n = 2 → 17.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q4 Answer: 82 marks
f(3) called from g uses n = 2 → 6, + 2 → 8.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q5 Answer: (B)1 mark
Each points to the enclosing procedure’s record.
- (A) is wrong: That is the control link.
- (C) is wrong: No.
- (D) is wrong: No.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q6 Answer: (B)1 mark
Updated on every call and return at depth i.
- (A) is wrong: Most recent, not oldest.
- (C) is wrong: That is the control link.
- (D) is wrong: Only d[0] would.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q7 Answer: 21 mark
4 − 2 = 2.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)
Q8 Answer: (A) (B) (D)2 marks
Static scoping ignores the call chain.
Re-read: Lesson 5.3 — Static vs dynamic scoping; tracing program output under each; access links and displays (brief)