One program under two scoping rules

The program below is written in Python syntax. Assume every assignment or parameter inside a function creates a new local binding for that function, and that the module binding x = 1 never changes.


Python
x = 1

def get():
    return x

def a():
    x = 2
    return get()

def b(x):
    return a() * 10 + get()


Each call is made separately from the module level. Fill in the integer value returned by each call when the free name x in get is resolved by lexical scope, and again when a hypothetical interpreter resolves it by dynamic scope, using the most recent active binding of x on the call stack.

CallLexical scope resultDynamic scope result
get()
a()
b(3)

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Binding and Scoping Questions: