0
0
Swiftprogramming~20 mins

Protocol conformance in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Protocol Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of protocol conformance with default implementation
What is the output of this Swift code when calling dog.speak()?
Swift
protocol Animal {
    func speak() -> String
}

extension Animal {
    func speak() -> String {
        return "Some sound"
    }
}

struct Dog: Animal {
    func speak() -> String {
        return "Woof!"
    }
}

let dog = Dog()
print(dog.speak())
A"Dog speaks"
B"Some sound"
CCompilation error: Missing method implementation
D"Woof!"
Attempts:
2 left
💡 Hint
Check if the struct provides its own implementation of the protocol method.
Predict Output
intermediate
2:00remaining
Protocol conformance with optional method
What will be printed when running this Swift code?
Swift
@objc protocol Greeter {
    @objc optional func greet() -> String
}

class Person: Greeter {
}

let p = Person()
print(p.greet?() ?? "No greeting")
ARuntime error: Method not found
B"Optional greeting"
C"No greeting"
DCompilation error: Optional protocol methods require @objc
Attempts:
2 left
💡 Hint
Check if the class implements the optional method or not.
🔧 Debug
advanced
2:00remaining
Fix the protocol conformance error
This code does not compile. What is the cause of the error?
Swift
protocol Vehicle {
    var speed: Int { get set }
    func description() -> String
}

struct Car: Vehicle {
    var speed: Int
    func description() -> String {
        return "Car speed is \(speed)"
    }
}

let myCar = Car(speed: 100)
print(myCar.description())
AThe protocol Vehicle must declare speed as 'let' instead of 'var'.
BThe struct Car must mark speed as 'var' to conform to the protocol's get set requirement.
CThe struct Car must mark speed as 'let' to conform to the protocol.
DThe description() method must be marked as 'mutating' in Car.
Attempts:
2 left
💡 Hint
Check the property requirements in the protocol and how Car declares speed.
Predict Output
advanced
2:00remaining
Protocol conformance with associated types
What is the output of this Swift code?
Swift
protocol Container {
    associatedtype Item
    var items: [Item] { get set }
    func count() -> Int
}

struct IntContainer: Container {
    var items: [Int]
    func count() -> Int {
        items.count
    }
}

let container = IntContainer(items: [1, 2, 3, 4])
print(container.count())
A4
BCompilation error: Missing associatedtype implementation
C0
DRuntime error
Attempts:
2 left
💡 Hint
Check how many items are in the array and what count() returns.
Predict Output
expert
2:00remaining
Protocol conformance and method dispatch
What is the output of this Swift code?
Swift
protocol Speaker {
    func speak() -> String
}

struct Cat: Speaker {
    func speak() -> String {
        "Meow"
    }
}

func makeSpeak(_ speaker: Speaker) {
    print(speaker.speak())
}

let cat = Cat()
makeSpeak(cat)
A"Meow"
B"speak()"
CCompilation error: Protocol method not implemented
DRuntime error: Protocol witness table missing
Attempts:
2 left
💡 Hint
Check if Cat implements the required method and how protocol methods are called.