Recall & Review
beginner
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?
✗ 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?✗ Incorrect
Swift prevents changing constants by giving a compile-time error.
Why is it safer to use
let instead of var when possible?✗ Incorrect
let makes your code safer by preventing accidental value changes.If you declare a class instance with
let, can you change its variable properties?✗ 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?
✗ 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.