What is the difference between let and var in Swift?
let declares a constant, which means its value cannot change after being set. var declares a variable, which means its value can change over time.
Click to reveal answer
beginner
Why should you prefer let over var when possible?
Using let helps prevent accidental changes to data, making your code safer and easier to understand.
Click to reveal answer
beginner
What happens if you try to change a value declared with let?
Swift will give a compile-time error because constants cannot be changed once set.
Click to reveal answer
intermediate
How does using let improve code readability?
It signals to readers that the value will not change, so they can trust it stays the same throughout the code.
Click to reveal answer
intermediate
Can you change properties of a class instance declared with <code>let</code>?
Yes, if the properties are variables (var) inside the class, you can change them even if the instance is declared with let. But you cannot assign a new instance to that constant.
Click to reveal answer
What keyword do you use in Swift to declare a value that cannot change?
Alet
Bvar
Cconst
Dstatic
✗ Incorrect
let declares a constant in Swift, which means the value cannot be changed.
What happens if you try to assign a new value to a let constant?
AThe program runs normally
BThe program crashes at runtime
CThe value changes silently
DA compile-time error occurs
✗ Incorrect
Swift prevents changing constants by giving a compile-time error.
Why is it safer to use let instead of var when possible?
let makes your code safer by preventing accidental value changes.
If you declare a class instance with let, can you change its variable properties?
ANo, nothing can change
BOnly if the class is a struct
CYes, variable properties can change
DOnly if you use <code>var</code> inside the class
✗ Incorrect
The instance reference is constant, but its variable properties can still change.
Which keyword should you use to declare a variable that can change?
Avar
Bconst
Clet
Dfinal
✗ Incorrect
var declares a variable whose value can change.
Explain why distinguishing between let and var is important in Swift programming.
Think about how changing or not changing values affects your program.
You got /6 concepts.
Describe a situation where using let instead of var can help avoid errors.
Consider a value like a fixed tax rate or a username that should not change.
You got /4 concepts.
Practice
(1/5)
1. What is the main difference between let and var in Swift?
easy
A. let declares a variable, var declares a constant.
B. let declares a constant, var declares a variable.
C. let is used for functions, var is used for classes.
D. let and var are interchangeable.
Solution
Step 1: Understand let usage
let is used to declare constants, meaning their value cannot change after being set.
Step 2: Understand var usage
var is used to declare variables, which can be changed after initialization.
Final Answer:
let declares a constant, var declares a variable. -> Option B
Quick Check:
let = constant, var = variable [OK]
Hint: Remember: let = fixed, var = changeable [OK]
Common Mistakes:
Thinking let can be changed after assignment
Confusing let and var roles
Assuming both behave the same
2. Which of the following is the correct way to declare a variable that can change in Swift?
easy
A. var score = 10
B. const score = 10
C. let score = 10
D. variable score = 10
Solution
Step 1: Identify Swift syntax for variable declaration
In Swift, var is used to declare variables that can change.
Step 2: Check each option
let score = 10 declares a constant. const score = 10 and variable score = 10 use invalid keywords. var score = 10 is correct.
Final Answer:
var score = 10 -> Option A
Quick Check:
Use var for changeable values [OK]
Hint: Use var for changeable values, let for fixed ones [OK]
Common Mistakes:
Using let instead of var for variables
Using non-Swift keywords like const or variable
Syntax errors from wrong keywords
3. What will be the output of this Swift code?
var count = 5
count = 10
print(count)
medium
A. 5
B. Error: Cannot assign to 'count' because it is a let constant
C. 10
D. nil
Solution
Step 1: Analyze variable declaration and assignment
count is declared with var, so it can be changed. Initially, it is 5, then reassigned to 10.
Step 2: Understand print output
The print statement outputs the current value of count, which is 10 after reassignment.
Final Answer:
10 -> Option C
Quick Check:
var allows reassignment, so output is 10 [OK]
Hint: var allows change, so print latest value [OK]
Common Mistakes:
Thinking count stays 5 after reassignment
Confusing var with let causing error
Expecting nil output
4. Identify the error in this Swift code:
let name = "Alice"
name = "Bob"
medium
A. Cannot assign to 'name' because it is a constant
B. Missing semicolon after first line
C. Variable 'name' not declared
D. No error, code runs fine
Solution
Step 1: Check declaration of name
name is declared with let, making it a constant.
Step 2: Check reassignment attempt
The code tries to assign a new value to name, which is not allowed for constants.
Final Answer:
Cannot assign to 'name' because it is a constant -> Option A
Quick Check:
let constants cannot be reassigned [OK]
Hint: let means no change allowed, reassignment causes error [OK]
Common Mistakes:
Assuming let variables can be reassigned
Thinking semicolon is required in Swift
Ignoring error messages
5. You want to keep track of a user's score that changes during the game, but also store the user's ID which never changes. Which declarations are correct?
hard
A. var userID = 12345
let score = 0
B. let userID = 12345
let score = 0
C. var userID = 12345
var score = 0
D. let userID = 12345
var score = 0
Solution
Step 1: Identify which values change
User ID never changes, so it should be a constant declared with let. Score changes, so it should be a variable declared with var.
Step 2: Match declarations to requirements
let userID = 12345
var score = 0 correctly declares userID with let and score with var.
Final Answer:
let userID = 12345
var score = 0 -> Option D
Quick Check:
Constants for fixed data, variables for changing data [OK]
Hint: Use let for fixed ID, var for changing score [OK]
Common Mistakes:
Using var for fixed values causing accidental changes
Using let for changing values causing errors
Mixing up which data changes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.