Closures and Lexical Scope Essentials Quiz Quiz

Test your understanding of closures and lexical scope with these fundamental questions, designed to check your grasp of key concepts and behavior in programming.

  1. Closure Concept

    What is a closure in programming?

    1. A function that returns itself
    2. An error raised by missing brackets
    3. A function combined with its lexical environment
    4. A data structure like an array
    5. A variable declared inside a function
  2. Lexical Scope Definition

    How does lexical scope determine variable access in code?

    1. By how many times a function runs
    2. By the operating system used
    3. By the order in which functions are called
    4. By the physical location where variables are declared
    5. By user input during runtime
  3. Closure Example Behavior

    Given the structure: function outer() { let a = 3; function inner() { return a; } return inner; }, what does calling outer()() return?

    1. null
    2. SyntaxError
    3. outer
    4. undefined
    5. 3
  4. Scope Accessibility

    If a variable is declared inside a function, where can it be accessed?

    1. Only within that function and its inner functions
    2. From external files
    3. Only within global scope
    4. From anywhere in the code
    5. From unrelated functions
  5. Closure Utility

    Which of the following is a common use of closures?

    1. Declaring global constants
    2. Destroying functions
    3. Automatically importing modules
    4. Debugging errors
    5. Creating private variables
  6. Lexical Scope in Nested Functions

    In a nested function, which scope does the function have access to?

    1. All scopes in all files
    2. Only its own scope
    3. Only global scope
    4. Its own scope and ancestor scopes
    5. Random scope levels
  7. Closure and Variable Updates

    If a closure refers to a variable that changes after its creation, what value does it use when later invoked?

    1. Only the initial value
    2. A random value
    3. An error is thrown
    4. The most recent value of the variable
    5. Undefined
  8. Incorrect Term Recognition

    Which of the following is NOT related to closures or lexical scope?

    1. Inner function
    2. Enclosed scope
    3. Heap sorting
    4. Variable environment
    5. Free variable
  9. Function Return Scenario

    What happens if you return a function from within another function and call it outside?

    1. It cannot be called outside
    2. It only returns global variables
    3. It loses access to variables from the outer function
    4. It can still access variables from the outer function
    5. It causes a compile error
  10. Lexical vs. Dynamic Scope

    What is the primary difference between lexical scope and dynamic scope?

    1. Lexical scope uses global variables, dynamic does not
    2. They are the same
    3. Lexical scope depends on code structure, dynamic scope depends on call order
    4. Dynamic scope is never used in programming
    5. Lexical scope only works with integers