Closures and Lexical Scope Essentials Quiz Quiz

Challenge your understanding of closures and lexical scope with this targeted quiz featuring practical scenarios and key concepts. Enhance your skills in identifying closure behavior, variable access patterns, and scope-related mechanisms in programming.

  1. Basic Closure Recognition

    Which statement best describes a closure in the context of the following code: function outer() { let x = 3; return function inner() { return x; }; }?

    1. A closure is simply any nested function regardless of variable access.
    2. A closure only exists if x is declared as a global variable.
    3. A closure is the value of x itself retained after outer has finished.
    4. A closure is the inner function that remembers and accesses x from the outer function after outer has finished.

    Explanation: The correct answer is that a closure is the inner function that remembers and accesses x from the outer function after its execution. Simply being a nested function does not make it a closure unless it maintains reference to its lexical scope, which is why the second option is not correct. Having x declared as global does not produce a closure, so the third option is misleading. The last option confuses the closure with the value being enclosed, which is incorrect.

  2. Lexical Scope Behavior

    What will happen if you call a function that references a variable declared in its parent function’s scope, but the parent function has already finished executing?

    1. The variable must be declared as global for access to succeed.
    2. The function will receive undefined as the variable’s value.
    3. The variable is deleted and causes a runtime error.
    4. The function can still access the variable due to lexical scoping and closures.

    Explanation: Lexical scoping and closures enable a function to retain access to variables from its defining scope, even after the parent function is done executing. The second option incorrectly suggests deletion of the variable, which does not occur due to closure retention. Receiving undefined is incorrect unless the variable was never initialized, and requiring global declaration ignores closure mechanics.

  3. Multiple Closures and Shared State

    If a parent function creates two different inner functions, both reading and modifying a variable from the parent scope, what happens when both are invoked?

    1. Each inner function receives a unique copy of the parent’s variable.
    2. The variable is immutable and cannot be changed by the inner functions.
    3. Only the first inner function can access the variable; the second cannot.
    4. Both inner functions share access and can modify the same variable from the parent function.

    Explanation: Both inner functions close over the same instance of the variable in the parent scope, so changes by one are visible to the other. The second option wrongly assumes variable duplication, which does not happen. Variables enclosed in closures are not inherently immutable, ruling out the third option. The fourth option is incorrect because closures grant access to all referencing inner functions, not just the first.

  4. Closures and Memory Management

    What is a common potential issue to be aware of when working with closures that capture large objects from their enclosing scope?

    1. Closures automatically free all captured variables when the parent function exits.
    2. Closures cannot capture objects, only primitive values.
    3. Captured objects in closures are always cleared after each function call.
    4. Closures holding large objects can lead to unexpected memory consumption if they persist longer than needed.

    Explanation: Capturing large objects in a closure can cause unnecessary memory use if the closure outlives its usefulness, as the object won’t be released until the closure is no longer referenced. The second option is incorrect; captured variables are not freed until the closure itself is gone. The third option is misleading since clearance depends on closure lifespan, not function calls. The last option is false because closures can reference any type, not just primitives.

  5. Difference Between Closure and Scope Chain

    In the following scenario, what does the closure allow a function to do that the normal scope chain alone does not: function outer() { let msg = 'Hello'; return function display() { return msg; }; }?

    1. Expose 'msg' as a global variable.
    2. Prevent any modifications to 'msg' outside 'display()'.
    3. Access 'msg' even after 'outer' has finished executing.
    4. Duplicate the 'msg' variable on every function call.

    Explanation: Closures allow a function to access and remember variables from its outer scope even after the outer function is done running. The second option incorrectly suggests closures provide immutability, while they merely allow access. The third option is wrong because closures do not duplicate variables. The fourth option misrepresents scope, as closures do not change a variable’s visibility to global.