Challenge - 5 Problems
Protocol Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a struct conforming to a protocol with property requirements
What is the output of this Swift code when run?
Swift
protocol Vehicle { var numberOfWheels: Int { get } func description() -> String } struct Bike: Vehicle { var numberOfWheels: Int = 2 func description() -> String { return "Bike with \(numberOfWheels) wheels" } } let myBike = Bike() print(myBike.description())
Attempts:
2 left
💡 Hint
Check how the struct implements the protocol's property and method.
✗ Incorrect
The struct Bike correctly implements the required property numberOfWheels and method description(). The print statement outputs the string with the number 2.
❓ Predict Output
intermediate2:00remaining
Protocol method requirement with mutating keyword
What will be printed when this Swift code runs?
Swift
protocol Toggleable { mutating func toggle() } struct LightSwitch: Toggleable { var isOn = false mutating func toggle() { isOn = !isOn } } var light = LightSwitch() light.toggle() print(light.isOn)
Attempts:
2 left
💡 Hint
Look at how the toggle method changes the isOn property.
✗ Incorrect
The toggle method flips the isOn boolean from false to true. The print statement outputs true.
🔧 Debug
advanced2:00remaining
Identify the error in protocol property implementation
Why does this Swift code fail to compile?
Swift
protocol Identifiable { var id: String { get set } } struct User: Identifiable { let id: String }
Attempts:
2 left
💡 Hint
Check the difference between 'let' and 'var' in property requirements.
✗ Incorrect
The protocol requires a property 'id' that can be read and written (get set). The struct declares 'id' as a constant (let), which cannot be set after initialization, causing a compile error.
📝 Syntax
advanced2:00remaining
Correct syntax for protocol property with optional get and set
Which option correctly declares a protocol property that can be read and optionally written?
Attempts:
2 left
💡 Hint
Protocols specify property requirements with get and set keywords.
✗ Incorrect
Option A declares a property that must be readable and writable. Option A is read-only. Option A is write-only (rare). Option A is invalid syntax.
🚀 Application
expert3:00remaining
Implement a protocol with a class and demonstrate property observer
Given this protocol, which class implementation correctly conforms and prints a message when the property changes?
Swift
protocol TemperatureMonitor { var temperature: Double { get set } } class Sensor: TemperatureMonitor { var temperature: Double = 0.0 { didSet { print("Temperature changed to \(temperature)") } } } let sensor = Sensor() sensor.temperature = 25.5
Attempts:
2 left
💡 Hint
Property observers can be used in classes to react to changes.
✗ Incorrect
The class Sensor conforms to the protocol and uses didSet to print a message whenever temperature changes. Setting temperature to 25.5 triggers the print.