0
0
Swiftprogramming~10 mins

Optional declaration with ? suffix in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an optional integer variable.

Swift
var age: Int[1]
Drag options to blanks, or click blank then click option'
A!
B?
C:
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using ! instead of ? to declare an optional
Using : or = which are not for optional declaration
2fill in blank
medium

Complete 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'
A!
B:
C=
D?
Attempts:
3 left
💡 Hint
Common Mistakes
Using ! which means forced unwrapping, not declaration
Using : or = which are not for optional declaration
3fill in blank
hard

Fix 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'
A:
B!
C?
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using : which is for type annotation but not optional
Using ! which is for forced unwrapping
4fill in blank
hard

Fill 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'
A?
Btrue
Cnil
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning true or false instead of nil to an optional
Using ! instead of ? for optional declaration
5fill in blank
hard

Fill 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'
A?
Bnil
C"Paris"
D"London"
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