0
0
Swiftprogramming~20 mins

Optional chaining with ?. in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Optional Chaining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this optional chaining example?
Consider the following Swift code. What will be printed when it runs?
Swift
class Person {
    var pet: Pet?
}

class Pet {
    var name: String
    init(name: String) {
        self.name = name
    }
}

let person = Person()
if let petName = person.pet?.name {
    print(petName)
} else {
    print("No pet name")
}
AEmpty string
Bnil
CRuntime error
DNo pet name
Attempts:
2 left
💡 Hint
Think about what happens when you try to access a property of a nil optional using optional chaining.
Predict Output
intermediate
2:00remaining
What does this optional chaining expression return?
Given the code below, what is the value of 'length' after execution?
Swift
class Book {
    var title: String?
}

let book = Book()
book.title = "Swift Guide"
let length = book.title?.count
print(length ?? "No length")
ANo length
B11
COptional(11)
Dnil
Attempts:
2 left
💡 Hint
Optional chaining returns an optional value even if the property is non-optional.
Predict Output
advanced
2:00remaining
What is the output of this nested optional chaining?
Analyze the code and determine what will be printed.
Swift
class Engine {
    var horsepower: Int
    init(horsepower: Int) {
        self.horsepower = horsepower
    }
}

class Car {
    var engine: Engine?
}

let car = Car()
car.engine = Engine(horsepower: 150)

if let hp = car.engine?.horsepower {
    print("Horsepower: \(hp)")
} else {
    print("No engine horsepower")
}
ANo engine horsepower
BHorsepower: 150
Cnil
DRuntime error
Attempts:
2 left
💡 Hint
Check if the engine property is nil or not before accessing horsepower.
Predict Output
advanced
2:00remaining
What error does this code produce?
What happens when you run this Swift code?
Swift
class House {
    var address: String
    init(address: String) {
        self.address = address
    }
}

var house: House?
house!.address = "123 Main St"
ARuntime error: Unexpectedly found nil while unwrapping an Optional value
BNo output, runs fine
CRuntime error: Index out of range
DCompile-time error: Use of unresolved identifier 'house'
Attempts:
2 left
💡 Hint
What happens if you force unwrap a nil optional?
🧠 Conceptual
expert
2:00remaining
How many items are in the resulting array after filtering with optional chaining?
Consider this Swift code snippet. How many elements does 'validLengths' contain after execution?
Swift
class Document {
    var text: String?
    init(text: String?) {
        self.text = text
    }
}

let docs = [Document(text: "Hello"), Document(text: nil), Document(text: "Swift"), Document(text: nil)]

let validLengths = docs.compactMap { $0.text?.count }
A2
B4
C0
D3
Attempts:
2 left
💡 Hint
compactMap removes nil values after optional chaining.