Challenge - 5 Problems
Protocol Composition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of protocol composition with method calls
What is the output of the following Swift code using protocol composition?
Swift
protocol A { func greet() -> String } protocol B { func farewell() -> String } struct Person: A, B { func greet() -> String { "Hello" } func farewell() -> String { "Goodbye" } } func communicate(with entity: A & B) { print(entity.greet()) print(entity.farewell()) } let p = Person() communicate(with: p)
Attempts:
2 left
💡 Hint
Remember that protocol composition requires the instance to conform to all protocols listed.
✗ Incorrect
The function communicate accepts an argument conforming to both protocols A and B. Person conforms to both, so both greet() and farewell() methods are called, printing "Hello" and "Goodbye".
❓ Predict Output
intermediate2:00remaining
Result of accessing properties in protocol composition
What will be printed when running this Swift code that uses protocol composition with properties?
Swift
protocol X { var xValue: Int { get } } protocol Y { var yValue: Int { get } } struct Point: X, Y { var xValue: Int var yValue: Int } func printSum(of point: X & Y) { print(point.xValue + point.yValue) } let pt = Point(xValue: 3, yValue: 7) printSum(of: pt)
Attempts:
2 left
💡 Hint
Check how the sum of the two property values is calculated.
✗ Incorrect
The Point struct has xValue = 3 and yValue = 7. The function prints their sum, which is 10.
🔧 Debug
advanced2:00remaining
Identify the error in protocol composition usage
What error does this Swift code produce and why?
Swift
protocol P1 { func foo() } protocol P2 { func bar() } struct S: P1 { func foo() { print("foo") } } func test(param: P1 & P2) { param.foo() param.bar() } let s = S() test(param: s)
Attempts:
2 left
💡 Hint
Check which protocols the struct conforms to and what the function requires.
✗ Incorrect
The struct S only conforms to P1, but the function test requires an argument conforming to both P1 and P2. This causes a compile-time error.
🧠 Conceptual
advanced2:00remaining
Understanding protocol composition with class inheritance
Given these Swift declarations, what is true about the variable 'obj' declared below?
Swift
protocol Alpha { func alphaMethod() } protocol Beta { func betaMethod() } class Base: Alpha { func alphaMethod() { print("Alpha") } } class Derived: Base, Beta { func betaMethod() { print("Beta") } } let obj: Alpha & Beta = Derived()
Attempts:
2 left
💡 Hint
Consider what protocols 'obj' is declared to conform to and the actual class instance.
✗ Incorrect
The variable 'obj' is declared as conforming to both Alpha and Beta. The instance is Derived, which implements both methods, so both can be called.
❓ Predict Output
expert3:00remaining
Output of complex protocol composition with extensions
What is the output of this Swift code involving protocol composition and protocol extensions?
Swift
protocol First { func action() -> String } protocol Second { func action() -> String } extension First { func action() -> String { "First" } } extension Second { func action() -> String { "Second" } } struct Combo: First, Second { func action() -> String { "Combo" } } func performAction(on obj: First & Second) { print(obj.action()) } let c = Combo() performAction(on: c)
Attempts:
2 left
💡 Hint
Check which implementation of action() is called when the struct provides its own.
✗ Incorrect
The struct Combo implements its own action() method, which overrides the default implementations from protocol extensions. So "Combo" is printed.