Recall & Review
beginner
What does the
? suffix mean when declaring a variable in Swift?It means the variable is an optional. It can hold a value or be
nil (no value).Click to reveal answer
beginner
How do you declare an optional integer variable named
age in Swift?You write:
var age: Int? This means age can hold an integer or be nil.Click to reveal answer
intermediate
What happens if you try to use an optional variable without unwrapping it?
Swift will not let you use it directly because it might be <code>nil</code>. You must unwrap it safely first.Click to reveal answer
intermediate
How can you safely check if an optional has a value before using it?
You can use
if let or guard let to unwrap the optional safely.Click to reveal answer
beginner
What is the difference between
Int and Int? in Swift?Int must always have a value. Int? can have a value or be nil.Click to reveal answer
What does
var name: String? mean in Swift?✗ Incorrect
The ? means name is optional and can be nil or hold a String.
How do you declare an optional Double variable called
price?✗ Incorrect
Use var price: Double? to declare an optional variable.
What must you do before using an optional variable's value?
✗ Incorrect
You must unwrap optionals safely to avoid errors if they are nil.
Which keyword helps safely unwrap an optional in Swift?
✗ Incorrect
if let unwraps optionals safely by checking if they have a value.
What value can an optional variable hold that a non-optional cannot?
✗ Incorrect
Optionals can hold nil, meaning no value, unlike non-optionals.
Explain what an optional variable is in Swift and why we use the
? suffix.Think about variables that might not have a value yet.
You got /4 concepts.
Describe how to safely use the value inside an optional variable.
How do you check if the optional has a value before using it?
You got /4 concepts.