Variable lifetime, decleration, and initialization.
Swift Fundamentals Review
This worksheet focuses on Variable Scope and Lifetime in Swift
corresponding to Chapter 3 Section 1.
Key Terms: Variable Scope and Lifetime
This section defines the core vocabulary used in this chapter.
VariableShow Definition
A named storage location that holds a value of a specific type.
TypeShow Definition
The kind of data a variable can store (e.g., Int, String, Bool). Swift enforces strict typing.
AssignmentShow Definition
The process of giving a variable a value using the = operator.
ScopeShow Definition
The region of code where a variable can be accessed.
LifetimeShow Definition
The duration during program execution that a variable exists in memory.
Global VariableShow Definition
A variable declared at the top level of a file, accessible across files in the same module.
Local VariableShow Definition
A variable declared inside a function or block, existing only during execution of that block.
Instance PropertyShow Definition
A property that belongs to a specific instance of a class or struct.
Static PropertyShow Definition
A property that belongs to the type itself rather than any instance.
Scope vs LifetimeShow Definition
Scope = where it can be used in code.
Lifetime = how long it exists in memory.
Section 1: True / False — Scope and Lifetime
1. A Swift variable has a fixed type for its entire lifetime.Show Solution
True. Swift is strongly typed; once declared, a variable cannot change type.
2. A variable can freely change type during execution.Show Solution
False. Swift does not allow type changes without explicit conversion.
3. Scope determines where a variable is accessible.Show Solution
True. Scope defines visibility in code.
4. Lifetime determines how long a variable exists.Show Solution
True. Lifetime refers to creation and destruction during execution.
5. Global variables are limited to a single file.Show Solution
False. They are accessible across files in the same module.
6. Global variables exist for the entire program execution.Show Solution
True. They persist for the program lifetime.
7. Instance properties belong to a specific object.Show Solution
True. Each instance has its own stored values.
8. Static properties require an instance to access.Show Solution
False. They are accessed via the type name.
9. Local variables exist only within a function or block.Show Solution
True. Their scope is limited to that block.
10. Local variables persist after a function returns.Show Solution
False. They are destroyed when the function exits.
Section 2: Fill in the Blank
11. A variable must have a defined ______.Show Solution
type
12. Assignment makes a variable ______ a value.Show Solution
refer to
13. Top-level variables are called ______ variables.Show Solution
global
14. Variables inside functions are ______ variables.Show Solution
local
15. A static property belongs to the ______.Show Solution
type
16. Instance properties live as long as the ______.Show Solution
instance
17. Properties are accessed using ______ notation.Show Solution
dot
18. A local variable is created at its ______.Show Solution
declaration
19. A local variable ends when its ______ ends.Show Solution
scope
20. Variables inside types are called ______.Show Solution
properties
Section 3: Code Interpretation
21.
let greeting = "Hello"
class Person {
func say() {
print(greeting)
}
}
Show Solution
Hello
Global variable is accessible inside class methods.22.
class Dog {
var name = "Fido"
}
let d1 = Dog()
let d2 = Dog()
d1.name = “Buddy”
print(d1.name)
print(d2.name)
Show Solution
Buddy
Fido
Each instance has independent stored properties.23.
func test() {
let x = 10
}
print(x)
Show Solution
Error: x is out of scope outside the function.24.
class Car {
static let type = "Vehicle"
}
print(Car.type)
Show Solution
Vehicle
Static property accessed via type name.25.
func f() {
let msg = "Hi"
print(msg)
}
Show Solution
Hi
Local variable exists only during function execution.Section 4: Short Answer
26. Explain scope.Show Solution
Scope = where a variable can be accessed in code.
27. Explain lifetime.Show Solution
Lifetime = how long a variable exists during execution.
28. Advantage of global variables?Show Solution
Easy access across multiple parts of a program.
29. Disadvantage of global variables?Show Solution
Harder to track changes and debug.
30. Why use instance properties?Show Solution
They keep data tied to specific objects.
Section 5: Application / Practice
31. Define a Student class with a name property.
Show Solution
class Student {
var name = ""
}
32. Create two students with different names.
Show Solution
let s1 = Student()
s1.name = "Alice"
let s2 = Student()
s2.name = “Bob”
33. Why can't local variables be accessed outside a function?Show Solution
Because their scope is limited to the function body.
34. Add a global variable and print it inside a class.
Show Solution
let version = "1.0"
class App {
func show() {
print(version)
}
}
35. Fix this code:
class Test {
func run() {
let x = 5
}
func printX() {
print(x)
}
}
Show Solution
x is local to run(), so it cannot be used in printX().Section 6: Mixed Concept Review
36. Shared value across all instances?Show Solution
Static property.
37. Temporary variable inside function?Show Solution
Local variable.
38.
class Counter {
static var total = 0
var count = 0
}
Show Solution
total = shared across all instances
count = unique per instance39. Why limit scope?Show Solution
Reduces bugs and improves code clarity.
40. Example of global variable issue.Show Solution
Multiple parts of a program changing the same value unexpectedly.