Bird
Raised Fist0
Swiftprogramming~20 mins

Implicitly unwrapped optionals in Swift - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of an implicitly unwrapped optional in Swift?
easy
A. To allow a variable to start as nil but be used without unwrapping once set
B. To force unwrap an optional every time it is accessed
C. To prevent a variable from ever being nil
D. To declare a constant that cannot change

Solution

  1. Step 1: Understand the behavior of implicitly unwrapped optionals

    They start as nil but can be used without unwrapping once assigned a value.
  2. Step 2: Compare options with this behavior

    Only To allow a variable to start as nil but be used without unwrapping once set describes this behavior correctly.
  3. Final Answer:

    To allow a variable to start as nil but be used without unwrapping once set -> Option A
  4. Quick Check:

    Implicitly unwrapped optional = skip unwrapping after set [OK]
Hint: Think: nil at start, no unwrap needed after set [OK]
Common Mistakes:
  • Confusing with regular optionals that always need unwrapping
  • Thinking it prevents nil values entirely
  • Assuming it forces unwrap every time
2. Which of the following is the correct syntax to declare an implicitly unwrapped optional String in Swift?
easy
A. var name: !String
B. var name: String?
C. var name: String!
D. var name: String

Solution

  1. Step 1: Recall syntax for implicitly unwrapped optionals

    They use an exclamation mark after the type, like String!.
  2. Step 2: Check each option

    var name: String! uses String!, which is correct syntax.
  3. Final Answer:

    var name: String! -> Option C
  4. Quick Check:

    Implicitly unwrapped optional syntax ends with ! [OK]
Hint: Look for exclamation mark after type, not before [OK]
Common Mistakes:
  • Using question mark instead of exclamation mark
  • Placing exclamation mark before the type
  • Omitting the exclamation mark entirely
3. What will be the output of the following Swift code?
var greeting: String! = nil
greeting = "Hello"
print(greeting)
medium
A. nil
B. Hello
C. Runtime error
D. Optional("Hello")

Solution

  1. Step 1: Analyze variable initialization and assignment

    The implicitly unwrapped optional greeting starts as nil but is then assigned the string "Hello".
  2. Step 2: Understand print behavior for implicitly unwrapped optional

    Since greeting is set, printing it outputs the string directly without Optional(...) wrapper.
  3. Final Answer:

    Hello -> Option B
  4. Quick Check:

    Implicitly unwrapped optional prints value directly when set [OK]
Hint: If set, prints value without Optional(...) [OK]
Common Mistakes:
  • Expecting 'nil' output when variable is assigned
  • Thinking it prints Optional(...) wrapper
  • Assuming runtime error because of initial nil
4. Identify the error in the following Swift code using implicitly unwrapped optionals:
var number: Int!
print(number + 5)
medium
A. Runtime error: force unwrap nil value
B. Compile-time error: cannot add Int and Int!
C. No error, prints 5
D. Syntax error: missing initialization

Solution

  1. Step 1: Check variable initialization

    number is declared as implicitly unwrapped optional but not assigned any value, so it is nil.
  2. Step 2: Understand what happens when using nil implicitly unwrapped optional

    Using number + 5 forces unwrap number, causing a runtime crash because it is nil.
  3. Final Answer:

    Runtime error: force unwrap nil value -> Option A
  4. Quick Check:

    Using nil implicitly unwrapped optional causes runtime crash [OK]
Hint: Never use implicitly unwrapped optional before assigning value [OK]
Common Mistakes:
  • Assuming it prints 5 because nil treated as zero
  • Thinking it's a compile-time error
  • Ignoring runtime crash possibility
5. You have a class property declared as var data: String! which is set asynchronously after initialization. How should you safely use data in a method to avoid runtime crashes?
hard
A. Force unwrap data! without checking
B. Use print(data) directly without checks
C. Change data to a non-optional String
D. Use if data != nil { print(data) } to check before use

Solution

  1. Step 1: Understand asynchronous setting of implicitly unwrapped optional

    Since data is set later, it may be nil when accessed early.
  2. Step 2: Use safe check before accessing

    Checking if data != nil ensures data is set before use, avoiding runtime crash.
  3. Final Answer:

    Use if data != nil { print(data) } to check before use -> Option D
  4. Quick Check:

    Check nil before using implicitly unwrapped optional set later [OK]
Hint: Always check nil before using async-set implicitly unwrapped optional [OK]
Common Mistakes:
  • Using implicitly unwrapped optional without nil check
  • Changing type unnecessarily
  • Forcing unwrap without safety
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard