Recall & Review
beginner
What is an Optional in Swift?
An Optional is a type that can hold either a value or no value (nil). It is used to handle the absence of a value safely.
Click to reveal answer
beginner
How do you declare an Optional String variable in Swift?
You add a question mark after the type, like this:
var name: String? This means name can hold a String or nil.Click to reveal answer
beginner
What does 'unwrapping' an Optional mean?
Unwrapping means accessing the actual value inside an Optional safely, checking if it is not nil before using it.
Click to reveal answer
intermediate
Explain 'optional binding' with an example.
Optional binding uses <code>if let</code> or <code>guard let</code> to check if an Optional has a value and then use it safely. Example: <code>if let safeName = name { print(safeName) }</code>Click to reveal answer
intermediate
What happens if you force unwrap a nil Optional?
Force unwrapping a nil Optional causes a runtime crash. Use
! only when you are sure the Optional has a value.Click to reveal answer
How do you declare an Optional integer in Swift?
✗ Incorrect
Optionals are declared by adding a question mark after the type, like
Int?.Which keyword is used for safe unwrapping of an Optional?
✗ Incorrect
The
if let syntax safely unwraps an Optional by checking if it contains a value.What does the exclamation mark (!) do when used with an Optional?
✗ Incorrect
The exclamation mark force unwraps an Optional, which can cause a crash if the value is nil.
What will happen if you force unwrap a nil Optional?
✗ Incorrect
Force unwrapping a nil Optional causes a runtime crash.
Which of these is NOT a way to unwrap an Optional?
✗ Incorrect
Declaring with var does not unwrap an Optional; it just declares a variable.
Explain what an Optional is in Swift and why it is useful.
Think about how you handle missing or unknown values in real life.
You got /4 concepts.
Describe two ways to unwrap an Optional safely in Swift.
These ways help you avoid crashes by checking if the value exists first.
You got /4 concepts.