GATE / CS ARCHIVE

GATE CSE 1997 / QUESTION 4.8

Lexical scope across sibling procedures

Compiler Design · Runtime Environments & Parameter Passing · Full GATE CSE 1997 paper

GATE 1997, Q4.8 2 marks · MCQ
Report issue

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