Recall & Review
beginner
What does
var mean in Swift?var is used to declare a variable that can change its value later. It means the value is mutable.
Click to reveal answer
beginner
How do you declare a variable named
score with the value 10 using var?You write: var score = 10. This means score can be changed later.
Click to reveal answer
beginner
What happens if you try to change a variable declared with
let instead of var?let creates a constant, which means its value cannot change. Trying to change it causes an error.
Click to reveal answer
beginner
Why use
var instead of let?Use var when you want to change the value later, like a score in a game. Use let when the value stays the same.
Click to reveal answer
beginner
Example: What is the output of this code?<br>
var count = 5 count = 10 print(count)
The output is 10 because count was changed from 5 to 10.
Click to reveal answer
Which keyword declares a variable that can change its value in Swift?
✗ Incorrect
var declares a mutable variable. let declares a constant.
What happens if you try to assign a new value to a
let constant?✗ Incorrect
let constants cannot be changed. Trying to do so causes an error.
Which of these is a correct way to declare a mutable variable
name with value "Anna"?✗ Incorrect
var name = "Anna" declares a variable that can be changed later.
If you want a value to never change, which keyword should you use?
✗ Incorrect
let creates a constant that cannot be changed.
What will this code print?<br>
var x = 3 x = 7 print(x)
✗ Incorrect
The variable x is changed to 7, so it prints 7.
Explain the difference between
var and let in Swift.Think about whether the value can be changed after it is set.
You got /4 concepts.
Write a short Swift code snippet that shows how to declare a variable and change its value.
Start with var, then assign a new value, then print.
You got /4 concepts.