1 Answers
📖 Quick Study Guide: Variable Scope Fundamentals
- 🌍 Global Scope: Variables declared outside any function or block, accessible throughout the entire program.
- 🏠 Local (Function) Scope: Variables declared inside a function, accessible only within that function. Each function call creates a new scope.
- 🧱 Block Scope: Variables declared within a block (e.g., `if`, `for`, `while` statements, using `let` or `const` in JavaScript), accessible only within that block.
- 🎭 Variable Shadowing: When a variable in an inner scope has the same name as a variable in an outer scope. The inner variable "shadows" the outer one within its scope.
- 🔎 Lexical (Static) Scope: Most common. Scope is determined at the time of code writing (compile time), based on where the variable is defined, not where it's called.
- 🌀 Dynamic Scope: Less common. Scope is determined at runtime, based on the call stack. (e.g., some shell scripts).
- ♻️ Garbage Collection: Variables typically become eligible for garbage collection once they go out of scope and are no longer referenced.
💡 Practice Quiz: Test Your Scope Knowledge
What is the primary characteristic of a variable defined in global scope?
A) It can only be accessed within the function it was declared in.
B) It is accessible from anywhere within the program.
C) It is destroyed immediately after its declaration.
D) It can only be modified by functions declared in the same file.In most modern programming languages (like Python, JavaScript, Java), what type of scope is primarily used to determine variable accessibility?
A) Dynamic Scope
B) Global Scope
C) Lexical (Static) Scope
D) Runtime ScopeConsider the following pseudocode:
```
x = 10 // Global
function foo() {
y = 20 // Local to foo
function bar() {
z = 30 // Local to bar
print(x + y + z)
}
bar()
}
foo()
```
Assuming `bar` can access `y` and `x` due to closure/lexical scope, what value would be printed?A) 10
B) 20
C) 30
D) 60What concept describes a scenario where a variable declared inside a function has the same name as a global variable, and the function uses its local version?
A) Variable Hoisting
B) Scope Chaining
C) Variable Shadowing
D) Global OverrideWhich of the following statements about 'block scope' is generally TRUE?
A) Variables with block scope are accessible throughout the entire file.
B) Variables with block scope are only accessible within the specific code block (e.g., `if`, `for`) where they are defined.
C) Block scope is the same as global scope.
D) Block scope is only relevant in object-oriented programming.In JavaScript, which keyword(s) typically create variables with block scope?
A) `var`
B) `function`
C) `let` and `const`
D) `this`When does a variable typically become eligible for garbage collection in a language with automatic memory management?
A) When the program ends.
B) When it is explicitly deleted by the programmer.
C) When it goes out of scope and is no longer referenced.
D) Immediately after its value is assigned.
Click to see Answers
1. B (Global variables are accessible throughout the entire program.)
2. C (Most modern languages use lexical, or static, scope.)
3. D (x=10, y=20, z=30. 10+20+30=60. This demonstrates lexical scoping where inner functions can access variables from their outer, enclosing scopes.)
4. C (Variable shadowing occurs when an inner scope variable hides an outer scope variable with the same name.)
5. B (Block-scoped variables are confined to the block they are declared in.)
6. C (`let` and `const` introduce block scope in JavaScript, unlike `var` which is function-scoped or global.)
7. C (Variables are typically garbage collected when they are no longer reachable or referenced, often after going out of scope.)
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀