Closures and Lexical Scope Essentials Quiz Quiz

Dive into key concepts of closures and lexical scope with these targeted questions designed to reinforce your understanding of variable visibility and function behaviors in programming. Explore scenarios where nested functions impact access and memory, perfect for anyone aiming to solidify knowledge of closure mechanics and lexical environments.

  1. Closure Variable Access

    Which option best describes why an inner function can access variables declared in its outer function's scope, such as in the example where function outer() declares a variable x and function inner() inside outer() references x?

    1. outer() passes variables to inner() through arguments automatically.
    2. Because of lexical scope, variable x is available to inner() when it is called.
    3. Function variables are shared globally by default.
    4. inner() copies all variables from outer() when it is defined.

    Explanation: Lexical scope means that an inner function has access to variables defined in its outer functions, as seen when inner() uses x from outer(). The option about inner() copying variables is incorrect, as no copying occurs—access is dynamic. Variables are not shared globally by default; scope rules keep them local unless specified otherwise. Variables are not implicitly passed as arguments; inner functions simply access them through scope.

  2. Closure Persistence

    When a closure is created, what happens to variables from the enclosing function if the closure references them after the outer function has finished executing?

    1. The variables are deleted immediately after the outer function returns.
    2. The variables are preserved by the closure and remain accessible.
    3. The variables only exist if declared with a special keyword like 'persistent'.
    4. The variables are made global and accessible everywhere.

    Explanation: Closures maintain references to variables in their lexical environment, so those variables are preserved and remain accessible even after the outer function finishes. The claim that variables are deleted is incorrect because closures keep them alive. Variables are not made global; their scope remains within the closure. No special keywords like 'persistent' are required for this behavior.

  3. Lexical Scope versus Dynamic Scope

    If a variable called y is defined both in the global scope and within a function, which value does a nested function within that function access when it references y?

    1. The value from the global scope by default.
    2. Both values are merged for the nested function.
    3. The latest value in the call stack is chosen by dynamic scope.
    4. The value from the nearest enclosing function due to lexical scope rules.

    Explanation: Lexical scope means that a nested function uses the variable value from the closest enclosing scope, not the global value. The global value isn't used unless no closer scope exists. Values are not merged for nested functions; variables are shadowed according to their scope. The option about choosing by dynamic scope is incorrect because most modern languages use lexical, not dynamic, scope.

  4. Practical Use of Closures

    What practical programming use is shown when a function returns another function that keeps using variables defined in the outer function, such as returning a function that increments an internal count?

    1. Creating recursive calls by default.
    2. Creating private state encapsulated within the returned function.
    3. Making variables automatically public to other code.
    4. Optimizing memory by deleting unused variables.

    Explanation: Returning a function that maintains its own internal variables is a classic use of closures for data encapsulation. This allows creation of private state, accessible only through the returned function. Variables are not made automatically public; they're actually hidden. Closures may increase, not necessarily decrease, memory use if references persist. Recursion is unrelated unless explicitly implemented.

  5. Variable Shadowing in Closures

    In the case where a variable z is declared both in the outer function and then again in a nested inner function, what happens when the inner function references z?

    1. The inner function only sees the global z value.
    2. The program throws an error due to multiple declarations.
    3. The inner function uses its own z, shadowing the outer z.
    4. Both z variables are combined for the calculation.

    Explanation: When a variable is redeclared in an inner function, it shadows the variable of the same name from the outer scope, so the inner function uses its own version. The global value is only visible if neither the inner nor the outer scope has z. Variables are not merged; each scope’s declaration is separate. No error occurs from shadowing; it is standard scope behavior.