0
0
Swiftprogramming~20 mins

Implicitly unwrapped optionals in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift IUO Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of accessing an implicitly unwrapped optional
What is the output of this Swift code?
Swift
var name: String! = "Alice"
print(name.count)
ARuntime error
BOptional(5)
Cnil
D5
Attempts:
2 left
💡 Hint
Implicitly unwrapped optionals can be used like normal values without unwrapping.
Predict Output
intermediate
2:00remaining
Behavior when implicitly unwrapped optional is nil
What happens when you run this Swift code?
Swift
var number: Int! = nil
print(number + 1)
A1
BRuntime error: unexpectedly found nil while unwrapping an Optional value
COptional(1)
D0
Attempts:
2 left
💡 Hint
Implicitly unwrapped optionals behave like normal values but crash if nil.
🔧 Debug
advanced
3:00remaining
Identify the cause of runtime crash with implicitly unwrapped optional
Why does this Swift code crash at runtime?
Swift
class Person {
    var pet: String!
    func printPet() {
        print(pet.uppercased())
    }
}

let p = Person()
p.printPet()
AThe 'pet' property is nil and is force-unwrapped causing a crash
BThe 'uppercased()' method is not available on String
CThe 'printPet' method is not called correctly
DThe 'Person' class must initialize 'pet' in the initializer
Attempts:
2 left
💡 Hint
Implicitly unwrapped optionals crash if nil when accessed.
🧠 Conceptual
advanced
2:30remaining
Understanding implicitly unwrapped optional declaration
Which statement about implicitly unwrapped optionals in Swift is true?
AThey must always be initialized with a non-nil value and never change to nil.
BThey behave like normal optionals and require explicit unwrapping every time.
CThey can be used as normal values but cause a runtime error if nil when accessed.
DThey automatically convert to non-optional types without any risk.
Attempts:
2 left
💡 Hint
Think about how implicitly unwrapped optionals behave differently from normal optionals.
Predict Output
expert
3:00remaining
Output of chained implicitly unwrapped optionals
What is the output of this Swift code?
Swift
class Node {
    var value: Int
    var next: Node!
    init(_ value: Int) {
        self.value = value
    }
}

let first = Node(1)
let second = Node(2)
first.next = second
second.next = nil

print(first.next.next.value)
ARuntime error: unexpectedly found nil while unwrapping an Optional value
B2
C1
DOptional(2)
Attempts:
2 left
💡 Hint
Consider what happens when accessing 'next' of a nil implicitly unwrapped optional.