Test your understanding of closures and lexical scope with these fundamental questions, designed to check your grasp of key concepts and behavior in programming.
Closure Concept
What is a closure in programming?
- A function that returns itself
- An error raised by missing brackets
- A function combined with its lexical environment
- A data structure like an array
- A variable declared inside a function
Lexical Scope Definition
How does lexical scope determine variable access in code?
- By how many times a function runs
- By the operating system used
- By the order in which functions are called
- By the physical location where variables are declared
- By user input during runtime
Closure Example Behavior
Given the structure: function outer() { let a = 3; function inner() { return a; } return inner; }, what does calling outer()() return?
- null
- SyntaxError
- outer
- undefined
- 3
Scope Accessibility
If a variable is declared inside a function, where can it be accessed?
- Only within that function and its inner functions
- From external files
- Only within global scope
- From anywhere in the code
- From unrelated functions
Closure Utility
Which of the following is a common use of closures?
- Declaring global constants
- Destroying functions
- Automatically importing modules
- Debugging errors
- Creating private variables
Lexical Scope in Nested Functions
In a nested function, which scope does the function have access to?
- All scopes in all files
- Only its own scope
- Only global scope
- Its own scope and ancestor scopes
- Random scope levels
Closure and Variable Updates
If a closure refers to a variable that changes after its creation, what value does it use when later invoked?
- Only the initial value
- A random value
- An error is thrown
- The most recent value of the variable
- Undefined
Incorrect Term Recognition
Which of the following is NOT related to closures or lexical scope?
- Inner function
- Enclosed scope
- Heap sorting
- Variable environment
- Free variable
Function Return Scenario
What happens if you return a function from within another function and call it outside?
- It cannot be called outside
- It only returns global variables
- It loses access to variables from the outer function
- It can still access variables from the outer function
- It causes a compile error
Lexical vs. Dynamic Scope
What is the primary difference between lexical scope and dynamic scope?
- Lexical scope uses global variables, dynamic does not
- They are the same
- Lexical scope depends on code structure, dynamic scope depends on call order
- Dynamic scope is never used in programming
- Lexical scope only works with integers