0
0
Swiftprogramming~10 mins

Implicitly unwrapped optionals 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 implicitly unwrapped optional integer variable.

Swift
var number: Int[1]
Drag options to blanks, or click blank then click option'
A!
B?
C:
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using a question mark instead of an exclamation mark.
Forgetting to add any symbol after the type.
2fill in blank
medium

Complete the code to assign a value to an implicitly unwrapped optional string.

Swift
var name: String! = [1]
Drag options to blanks, or click blank then click option'
Anil
B"Alice"
C123
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a number or boolean instead of a string.
Assigning nil when a value is expected.
3fill in blank
hard

Fix the error in the code by completing the declaration of an implicitly unwrapped optional boolean.

Swift
var isActive: Bool[1] = true
Drag options to blanks, or click blank then click option'
A!
B?
C=
D:
Attempts:
3 left
💡 Hint
Common Mistakes
Using a question mark instead of an exclamation mark.
Using assignment or colon symbols incorrectly.
4fill in blank
hard

Fill both blanks to unwrap and print the implicitly unwrapped optional safely.

Swift
if let unwrapped = optionalString[1] {
    print(unwrapped[2])
}
Drag options to blanks, or click blank then click option'
A?
B!
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using exclamation mark inside the if-let block when not needed.
Using question mark or other symbols unnecessarily in the if-let condition.
5fill in blank
hard

Fill all three blanks to declare, assign, and print an implicitly unwrapped optional integer.

Swift
var score[1]: Int
score = [2]
print(score[3])
Drag options to blanks, or click blank then click option'
A!
B42
D?
Attempts:
3 left
💡 Hint
Common Mistakes
Using question mark instead of exclamation mark for declaration or unwrapping.
Forgetting to unwrap the variable when printing.