Challenge - 5 Problems
Protocol Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of protocol extension method call
What will be printed when this Swift code runs?
Swift
protocol Greetable { func greet() } extension Greetable { func greet() { print("Hello from protocol extension") } } struct Person: Greetable { func greet() { print("Hello from Person struct") } } let p: Greetable = Person() p.greet()
Attempts:
2 left
💡 Hint
Think about how protocol extensions provide default implementations and how method dispatch works with protocol-typed variables.
✗ Incorrect
When a variable is typed as the protocol itself, the method called is the one defined in the protocol extension, not the struct's own implementation. This is because protocol extension methods are statically dispatched.
🧠 Conceptual
intermediate1:30remaining
Why use protocols instead of base classes?
Which of the following is the main advantage of using protocols over base classes in Swift's protocol-oriented design?
Attempts:
2 left
💡 Hint
Think about how Swift handles inheritance and how protocols differ from classes.
✗ Incorrect
Protocols allow a type to conform to multiple protocols, effectively enabling multiple inheritance of behavior, which classes cannot do because Swift classes support only single inheritance.
🔧 Debug
advanced2:30remaining
Fix the unexpected output in protocol extension
This Swift code prints "Hello from protocol extension" but the developer expects "Hello from Dog struct". What is the cause?
Swift
protocol Animal { func speak() } extension Animal { func speak() { print("Hello from protocol extension") } } struct Dog: Animal { func speak() { print("Hello from Dog struct") } } let pet: Animal = Dog() pet.speak()
Attempts:
2 left
💡 Hint
Consider how Swift decides which method to call when using protocol-typed variables.
✗ Incorrect
When calling a method on a variable typed as a protocol, Swift uses static dispatch and calls the protocol extension method, ignoring the struct's own implementation.
📝 Syntax
advanced1:30remaining
Identify the syntax error in protocol conformance
Which option contains the correct syntax to make a struct conform to a protocol with a method requirement?
Swift
protocol Runner { func run() } struct Athlete { // Conformance here }
Attempts:
2 left
💡 Hint
Remember the Swift keyword to declare protocol conformance.
✗ Incorrect
In Swift, a type conforms to a protocol using a colon ':' followed by the protocol name. Keywords like implements, extends, or conforms are invalid.
🚀 Application
expert3:00remaining
Determine the number of methods available via protocol composition
Given the protocols and struct below, how many unique methods can be called on the variable 'obj' typed as 'A & B'?
Swift
protocol A { func methodA() } protocol B { func methodB() func methodC() } struct C: A, B { func methodA() { print("A") } func methodB() { print("B") } func methodC() { print("C") } func methodD() { print("D") } } let obj: A & B = C()
Attempts:
2 left
💡 Hint
Count only the methods declared in the protocols A and B accessible via the protocol composition type.
✗ Incorrect
The variable obj is typed as A & B, so only methods declared in protocols A and B are accessible. methodD() is not accessible because it's only in struct C.