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 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())
Attempts:
2 left
💡 Hint
Think about how Swift allows adding protocol conformance in extensions.
✗ Incorrect
In Swift, you can make a type conform to a protocol by implementing the required methods inside an extension. Here,
Person conforms to Greetable via the extension, so calling greet() returns the string defined there.❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Check how the extension adds protocol conformance to Car.
✗ Incorrect
The extension makes
Car conform to Describable by implementing describe(). Passing myCar to printDescription works because Car conforms to Describable. The output is the string returned by describe().🔧 Debug
advanced2: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())
Attempts:
2 left
💡 Hint
Check where the protocol method is implemented relative to the conformance declaration.
✗ Incorrect
In Swift, when you declare protocol conformance in an extension, the required methods must be implemented in the same extension. Here,
run() is implemented in a separate extension, so the compiler does not see it as fulfilling the protocol requirement, causing a compilation error.📝 Syntax
advanced2: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())
Attempts:
2 left
💡 Hint
Look for missing braces or incomplete blocks.
✗ Incorrect
Option A is missing the closing brace for the extension, causing a syntax error. Option A returns void from a function expecting String, causing a type error. Options A and B are valid syntax; B uses implicit return which is allowed.
🚀 Application
expert2: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
Attempts:
2 left
💡 Hint
Protocols require all their methods to be implemented to conform.
✗ Incorrect
The protocol
Worker requires two methods: start() and stop(). To conform, the extension must implement both methods. Declaring conformance without implementing methods causes a compile error.