Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an optional integer variable.
Swift
var age: Int[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ! instead of ? to declare an optional
Using : or = which are not for optional declaration
✗ Incorrect
In Swift, adding a ? after the type declares an optional variable.
2fill in blank
mediumComplete the code to declare an optional string variable named name.
Swift
var name: String[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ! which means forced unwrapping, not declaration
Using : or = which are not for optional declaration
✗ Incorrect
The ? suffix after String makes name an optional string.
3fill in blank
hardFix the error in the optional declaration of a Double variable.
Swift
var price: Double[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using : which is for type annotation but not optional
Using ! which is for forced unwrapping
✗ Incorrect
Use ? to declare price as an optional Double.
4fill in blank
hardFill both blanks to declare an optional Boolean variable and assign nil.
Swift
var isActive: Bool[1] = [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning true or false instead of nil to an optional
Using ! instead of ? for optional declaration
✗ Incorrect
Use ? to declare optional and assign nil to show no value.
5fill in blank
hardFill all three blanks to declare an optional string variable, assign nil, and then assign a value.
Swift
var city: String[1] = [2] city = [3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using ? for optional declaration
Assigning a string value directly without optional
Using wrong quotes or missing quotes around string
✗ Incorrect
Declare city as optional with ?, assign nil, then assign a string value.