Questions: Scope, Shadowing, and Variable Lifetime

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

A global variable count = 10 exists. Inside a function, you write count = 0 (declaring a new local variable with the same name). After the function runs, what is the value of the global count?

A0 — the function modified the global variable
B10 — the function created a shadow variable; the global was unaffected
CUndefined — the global was destroyed when the function ran
D0 — inner scope variables always update the outer scope version
Question 2 Multiple Choice

Consider a variable x declared inside an if-block in a language with block scope. After the if-block ends, which statement is correct?

Ax is still accessible but its value is undefined
Bx no longer exists — both its scope and lifetime have ended
Cx still exists in memory but cannot be accessed by name
Dx is accessible only from the enclosing function, not globally
Question 3 True / False

A variable's scope and its lifetime are generally identical — they both start and end at the same points in program execution.

TTrue
FFalse
Question 4 True / False

In languages with lexical (static) scoping, an inner scope can read variables from all enclosing outer scopes unless shadowed.

TTrue
FFalse
Question 5 Short Answer

Why is it important to distinguish between a variable's scope and its lifetime, and give a concrete example where they differ?

Think about your answer, then reveal below.