Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to safely unwrap the optional using if let.
Swift
var name: String? = "Alice" if [1] unwrappedName = name { print("Hello, \(unwrappedName)!") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' instead of 'let' in the if statement.
Using 'is' which is for type checking, not unwrapping.
✗ Incorrect
The 'if let' syntax unwraps the optional safely by binding its value to a new constant.
2fill in blank
mediumComplete the code to unwrap the optional and print the value.
Swift
var age: Int? = 30 if let unwrappedAge = [1] { print("Age is \(unwrappedAge)") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the unwrapped variable name inside the if let condition.
Using a variable name that does not exist.
✗ Incorrect
You unwrap the optional variable 'age' inside the if let statement.
3fill in blank
hardFix the error in the optional binding syntax.
Swift
var city: String? = "Paris" if [1] city = city { print("City is \(city)") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' without 'let' or 'var' in the if statement.
Using 'is' which is for type checking.
✗ Incorrect
The correct syntax for optional binding uses 'if let' to unwrap the optional.
4fill in blank
hardFill both blanks to unwrap two optionals and print their values.
Swift
var firstName: String? = "John" var lastName: String? = "Doe" if [1] first = firstName, [2] last = lastName { print("Full name: \(first) \(last)") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing 'let' and 'var' in the same if statement.
Using 'is' instead of 'let' for binding.
✗ Incorrect
Both optionals are unwrapped using 'let' in the if statement.
5fill in blank
hardFill all three blanks to unwrap an optional, check a condition, and print the result.
Swift
var score: Int? = 85 if [1] unwrappedScore = score, unwrappedScore [2] 80, unwrappedScore [3] 100 { print("Great score: \(unwrappedScore)") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' instead of 'let' for unwrapping.
Using wrong comparison operators.
✗ Incorrect
Use 'let' to unwrap, then check if score is greater than 80 and less than or equal to 100.