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.
Which statement best describes a closure in the context of the following code: function outer() { let x = 3; return function inner() { return x; }; }?
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.
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?
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.
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?
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.
What is a common potential issue to be aware of when working with closures that capture large objects from their enclosing scope?
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.
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; }; }?
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.