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 this Swift code using protocol composition?
Swift
protocol A { func greet() } protocol B { func greet() } struct Person: A, B { func greet() { print("Hello from Person") } } func welcome(_ entity: A & B) { entity.greet() } let p = Person() welcome(p)
Attempts:
2 left
💡 Hint
Think about how protocol composition works when both protocols have the same method.
✗ Incorrect
The struct Person implements greet() once, satisfying both protocols A and B. The function welcome accepts an argument conforming to both A and B, so calling greet() calls Person's implementation, printing "Hello from Person".
❓ Predict Output
intermediate2:00remaining
Result of protocol composition with property requirements
What will be printed when this Swift code runs?
Swift
protocol Named { var name: String { get } } protocol Aged { var age: Int { get } } struct User: Named, Aged { var name: String var age: Int } func describe(_ entity: Named & Aged) { print("\(entity.name) is \(entity.age) years old.") } let user = User(name: "Alice", age: 30) describe(user)
Attempts:
2 left
💡 Hint
Check how protocol composition works with properties.
✗ Incorrect
The struct User implements both protocols Named and Aged by providing name and age properties. The function describe accepts an argument conforming to both protocols and prints the combined information.
❓ Predict Output
advanced2:00remaining
Output of protocol composition with class and protocol inheritance
What is the output of this Swift code involving protocol composition and class inheritance?
Swift
protocol P1 { func action() } protocol P2 { func action() } class Base: P1 { func action() { print("Base action") } } class Derived: Base, P2 { override func action() { print("Derived action") } } func perform(_ obj: P1 & P2) { obj.action() } let d = Derived() perform(d)
Attempts:
2 left
💡 Hint
Consider method overriding and protocol composition.
✗ Incorrect
Derived overrides action(), so when perform calls action() on an object conforming to both P1 and P2, Derived's implementation runs, printing "Derived action".
❓ Predict Output
advanced2:00remaining
Behavior of protocol composition with optional protocol requirements
What happens when this Swift code runs?
Swift
@objc protocol OptionalProtocol { @objc optional func optionalMethod() } class MyClass: OptionalProtocol { } func callOptional(_ obj: OptionalProtocol) { if let method = obj.optionalMethod { method() } else { print("No method implemented") } } let instance = MyClass() callOptional(instance)
Attempts:
2 left
💡 Hint
Think about how optional protocol methods behave when not implemented.
✗ Incorrect
Since MyClass does not implement optionalMethod, the optional chaining fails and the else branch runs, printing "No method implemented".
🧠 Conceptual
expert2:00remaining
Understanding protocol composition type identity
Given the following Swift code, what is the type of the variable 'combined'?
Swift
protocol Alpha { func alphaMethod() } protocol Beta { func betaMethod() } struct S: Alpha, Beta { func alphaMethod() { print("Alpha") } func betaMethod() { print("Beta") } } let s = S() let combined: Alpha & Beta = s
Attempts:
2 left
💡 Hint
Recall what protocol composition means in Swift type system.
✗ Incorrect
The type Alpha & Beta means a single type that conforms to both protocols simultaneously, not a tuple or union.