0
0
Swiftprogramming~20 mins

Protocol conformance via extension in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Protocol Extension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of protocol conformance via extension
What is the output of this Swift code when calling greet() on Person()?
Swift
protocol Greetable {
    func greet() -> String
}

struct Person {}

extension Person: Greetable {
    func greet() -> String {
        return "Hello from extension!"
    }
}

let p = Person()
print(p.greet())
AHello from extension!
BHello from Person struct!
CCompilation error: 'Person' does not conform to protocol 'Greetable'
DRuntime error: method not found
Attempts:
2 left
💡 Hint
Think about how Swift allows adding protocol conformance in extensions.
Predict Output
intermediate
2:00remaining
Protocol method call with extension conformance
What will be printed when running this Swift code?
Swift
protocol Describable {
    func describe() -> String
}

struct Car {}

extension Car: Describable {
    func describe() -> String {
        "A fast car"
    }
}

func printDescription(_ item: Describable) {
    print(item.describe())
}

let myCar = Car()
printDescription(myCar)
ACompilation error: Cannot convert value of type 'Car' to expected argument type 'Describable'
BEmpty string
CRuntime error: method describe() not found
DA fast car
Attempts:
2 left
💡 Hint
Check how the extension adds protocol conformance to Car.
🔧 Debug
advanced
2:30remaining
Why does this protocol conformance fail?
Given this Swift code, why does it fail to compile?
Swift
protocol Runnable {
    func run() -> String
}

struct Animal {}

extension Animal: Runnable {}

extension Animal {
    func run() -> String {
        "Running fast"
    }
}

let a = Animal()
print(a.run())
ANo error, prints Running fast
BCompilation error because protocol conformance requires the method inside the extension that declares conformance
CCompilation error because Animal already has run() method in extension
DRuntime error because method run() is not implemented
Attempts:
2 left
💡 Hint
Check where the protocol method is implemented relative to the conformance declaration.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in protocol conformance via extension
Which option contains the syntax error preventing this Swift code from compiling?
Swift
protocol Flyable {
    func fly() -> String
}

struct Bird {}

extension Bird: Flyable {
    func fly() -> String {
        return "Flying high"
    }
}

let b = Bird()
print(b.fly())
Aextension Bird: Flyable { func fly() -> String { return "Flying high" }
Bextension Bird: Flyable { func fly() -> String { "Flying high" } }
Cextension Bird: Flyable { func fly() -> String { return "Flying high" } }
Dextension Bird: Flyable { func fly() -> String { print("Flying high") } }
Attempts:
2 left
💡 Hint
Look for missing braces or incomplete blocks.
🚀 Application
expert
2:30remaining
How many methods must be implemented for protocol conformance via extension?
Given this protocol and struct, how many methods must be implemented inside the extension to make Robot conform to Worker?
Swift
protocol Worker {
    func start() -> String
    func stop() -> String
}

struct Robot {}

// Extension to make Robot conform to Worker goes here
ANo methods, just declare conformance in extension
BOne method: either start() or stop()
CTwo methods: start() and stop()
DThree methods: start(), stop(), and reset()
Attempts:
2 left
💡 Hint
Protocols require all their methods to be implemented to conform.