Variables | Coding With Matt`

Variables

Contents

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.

Variable
Show DefinitionA named storage location that holds a value of a specific type.
Type
Show DefinitionThe kind of data a variable can store (e.g., Int, String, Bool). Swift enforces strict typing.
Assignment
Show DefinitionThe process of giving a variable a value using the = operator.
Scope
Show DefinitionThe region of code where a variable can be accessed.
Lifetime
Show DefinitionThe duration during program execution that a variable exists in memory.
Global Variable
Show DefinitionA variable declared at the top level of a file, accessible across files in the same module.
Local Variable
Show DefinitionA variable declared inside a function or block, existing only during execution of that block.
Instance Property
Show DefinitionA property that belongs to a specific instance of a class or struct.
Static Property
Show DefinitionA property that belongs to the type itself rather than any instance.
Scope vs Lifetime
Show DefinitionScope = 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 SolutionTrue. Swift is strongly typed; once declared, a variable cannot change type.
2. A variable can freely change type during execution.
Show SolutionFalse. Swift does not allow type changes without explicit conversion.
3. Scope determines where a variable is accessible.
Show SolutionTrue. Scope defines visibility in code.
4. Lifetime determines how long a variable exists.
Show SolutionTrue. Lifetime refers to creation and destruction during execution.
5. Global variables are limited to a single file.
Show SolutionFalse. They are accessible across files in the same module.
6. Global variables exist for the entire program execution.
Show SolutionTrue. They persist for the program lifetime.
7. Instance properties belong to a specific object.
Show SolutionTrue. Each instance has its own stored values.
8. Static properties require an instance to access.
Show SolutionFalse. They are accessed via the type name.
9. Local variables exist only within a function or block.
Show SolutionTrue. Their scope is limited to that block.
10. Local variables persist after a function returns.
Show SolutionFalse. They are destroyed when the function exits.

Section 2: Fill in the Blank

11. A variable must have a defined ______.
Show Solutiontype
12. Assignment makes a variable ______ a value.
Show Solutionrefer to
13. Top-level variables are called ______ variables.
Show Solutionglobal
14. Variables inside functions are ______ variables.
Show Solutionlocal
15. A static property belongs to the ______.
Show Solutiontype
16. Instance properties live as long as the ______.
Show Solutioninstance
17. Properties are accessed using ______ notation.
Show Solutiondot
18. A local variable is created at its ______.
Show Solutiondeclaration
19. A local variable ends when its ______ ends.
Show Solutionscope
20. Variables inside types are called ______.
Show Solutionproperties

Section 3: Code Interpretation

21.
let greeting = "Hello"

class Person { func say() { print(greeting) } }

Show SolutionHello
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 SolutionBuddy
Fido
Each instance has independent stored properties.
23.
func test() {
    let x = 10
}
print(x)
Show SolutionError: x is out of scope outside the function.
24.
class Car {
    static let type = "Vehicle"
}
print(Car.type)
Show SolutionVehicle
Static property accessed via type name.
25.
func f() {
    let msg = "Hi"
    print(msg)
}
Show SolutionHi
Local variable exists only during function execution.

Section 4: Short Answer

26. Explain scope.
Show SolutionScope = where a variable can be accessed in code.
27. Explain lifetime.
Show SolutionLifetime = how long a variable exists during execution.
28. Advantage of global variables?
Show SolutionEasy access across multiple parts of a program.
29. Disadvantage of global variables?
Show SolutionHarder to track changes and debug.
30. Why use instance properties?
Show SolutionThey 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 SolutionBecause 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 Solutionx is local to run(), so it cannot be used in printX().

Section 6: Mixed Concept Review

36. Shared value across all instances?
Show SolutionStatic property.
37. Temporary variable inside function?
Show SolutionLocal variable.
38.
class Counter {
    static var total = 0
    var count = 0
}
Show Solutiontotal = shared across all instances
count = unique per instance
39. Why limit scope?
Show SolutionReduces bugs and improves code clarity.
40. Example of global variable issue.
Show SolutionMultiple parts of a program changing the same value unexpectedly.