Challenge - 5 Problems
Protocol Extension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of protocol extension default method call
What is the output of this Swift code when calling
greet() on person?Swift
protocol Greeter { func greet() } extension Greeter { func greet() { print("Hello from protocol extension") } } struct Person: Greeter {} let person = Person() person.greet()
Attempts:
2 left
💡 Hint
Protocol extensions provide default implementations if the conforming type does not implement the method.
✗ Incorrect
Since Person does not implement greet(), the default implementation in the protocol extension is called, printing the message.
❓ Predict Output
intermediate2:00remaining
Overriding default implementation in protocol extension
What will be printed when
greet() is called on dog?Swift
protocol Greeter { func greet() } extension Greeter { func greet() { print("Hello from protocol extension") } } struct Dog: Greeter { func greet() { print("Woof! Hello from Dog") } } let dog = Dog() dog.greet()
Attempts:
2 left
💡 Hint
If the conforming type implements the method, it overrides the default from the extension.
✗ Incorrect
Dog provides its own greet() method, so calling greet() on dog uses Dog's implementation.
🔧 Debug
advanced3:00remaining
Why does this code print the protocol extension method instead of the struct's method?
Given the code below, why does calling
greeter.greet() print "Hello from protocol extension" instead of "Hi from Cat"?Swift
protocol Greeter { } extension Greeter { func greet() { print("Hello from protocol extension") } } struct Cat: Greeter { func greet() { print("Hi from Cat") } } let cat = Cat() let greeter: Greeter = cat greeter.greet()
Attempts:
2 left
💡 Hint
Consider how Swift dispatches methods when called on protocol-typed variables.
✗ Incorrect
When calling a method on a variable typed as the protocol, Swift uses static dispatch for protocol extension methods, so the default implementation is called instead of the struct's override.
📝 Syntax
advanced2:00remaining
Which option correctly adds a default implementation to a protocol method with parameters?
Given the protocol below, which option correctly provides a default implementation for
describe(age:) in a protocol extension?Swift
protocol Describable { func describe(age: Int) }
Attempts:
2 left
💡 Hint
The method signature in the extension must exactly match the protocol's method signature.
✗ Incorrect
Option A matches the protocol method signature exactly, providing a valid default implementation. Option A has no parameters, C misses the parameter type, and A changes the parameter type, all causing errors or mismatches.
🚀 Application
expert3:00remaining
Using protocol extensions to add default behavior with constraints
Consider the protocol
Summable below. Which option correctly uses a protocol extension with a constraint to provide a default implementation of sum() only for arrays of Int?Swift
protocol Summable { associatedtype Element func sum() -> Element }
Attempts:
2 left
💡 Hint
You must constrain the extension to Element == Int and cast self to [Int] to use reduce.
✗ Incorrect
Option D correctly constrains the extension to Element == Int and safely casts self to [Int] to call reduce. Option D provides a generic implementation returning 0, which may not be correct. Option D uses Element: Numeric but casts self to [Element] without guaranteeing self is an array. Option D tries to call reduce directly on self, which is not guaranteed to be a collection.