Questions: Scope and Binding Resolution

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

Consider this pseudocode: x = 10 is defined globally, then a function outer() defines x = 20 and calls an inner function inner() which prints x. inner() prints 20, not 10. Which principle explains this?

ADynamic scoping — x is resolved at runtime by scanning the call stack for the most recent assignment
BLexical (static) scoping — the compiler searches outward from the innermost enclosing scope; outer's x = 20 is found before the global x = 10
CFlat symbol tables — only one x can exist per program, so the most recently written value is used
DForward reference resolution — the compiler always uses the last assignment to x anywhere in the program
Question 2 Multiple Choice

A language allows two mutually recursive functions — A calls B, and B calls A — declared in the same scope, with A defined before B. What special handling does this require during scope resolution?

ANone — a single forward pass handles all name uses automatically
BEither a two-pass approach or a declare-then-define protocol, because when A is being processed, B has not yet been declared and the forward reference cannot be resolved in one pass
CDynamic scoping, because the mutual call order cannot be determined at compile time
DFlat symbol tables, so both functions share a global namespace and see each other immediately
Question 3 True / False

In a language with lexical scoping, declaring a variable with the same name as an outer-scope variable inside an inner scope causes the inner declaration to shadow the outer one during name lookup.

TTrue
FFalse
Question 4 True / False

After scope resolution is complete, later compiler phases such as type checking and code generation is expected to re-perform name lookups to ensure they reference the correct declarations.

TTrue
FFalse
Question 5 Short Answer

What is the difference between lexical (static) scoping and dynamic scoping, and why does the distinction matter for compiler design?

Think about your answer, then reveal below.