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.
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?
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.
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?
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.
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?
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.
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?
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.
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?
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.