0
0
Swiftprogramming~20 mins

Protocol requirements (methods and properties) in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Protocol Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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())
ARuntime error
BBike with wheels
CCompilation error: missing property implementation
DBike with 2 wheels
Attempts:
2 left
💡 Hint
Check how the struct implements the protocol's property and method.
Predict Output
intermediate
2: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)
Atrue
Bfalse
CCompilation error: missing mutating keyword
DRuntime error
Attempts:
2 left
💡 Hint
Look at how the toggle method changes the isOn property.
🔧 Debug
advanced
2: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
}
AMissing method implementation
BCannot assign let property 'id' to protocol requiring get set
CProtocol requires static property 'id'
DNo error, code compiles fine
Attempts:
2 left
💡 Hint
Check the difference between 'let' and 'var' in property requirements.
📝 Syntax
advanced
2: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?
Avar name: String { get set }
Bvar name: String { get }
Cvar name: String { set }
Dvar name: String { get optional set }
Attempts:
2 left
💡 Hint
Protocols specify property requirements with get and set keywords.
🚀 Application
expert
3: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
ACompilation error: property observer not allowed
BNo output printed
CPrints 'Temperature changed to 25.5'
DRuntime error
Attempts:
2 left
💡 Hint
Property observers can be used in classes to react to changes.