0
0
Swiftprogramming~20 mins

Protocol composition in practice in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Protocol Composition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
ARuntime error
BNo output
CHello from Person
DCompile-time error due to ambiguous greet()
Attempts:
2 left
💡 Hint
Think about how protocol composition works when both protocols have the same method.
Predict Output
intermediate
2: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)
ARuntime error: Missing property implementation
BAlice is 30 years old.
CCompile-time error: Cannot use protocol composition with properties
DAlice is years old.
Attempts:
2 left
💡 Hint
Check how protocol composition works with properties.
Predict Output
advanced
2: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)
ADerived action
BRuntime error
CCompile-time error: ambiguous method
DBase action
Attempts:
2 left
💡 Hint
Consider method overriding and protocol composition.
Predict Output
advanced
2: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)
ANo method implemented
BRuntime error: optionalMethod not found
CCompile-time error: optional methods not supported
DEmpty output
Attempts:
2 left
💡 Hint
Think about how optional protocol methods behave when not implemented.
🧠 Conceptual
expert
2: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
AA union type that can be either Alpha or Beta
BA tuple containing Alpha and Beta instances
CAn array of Alpha and Beta types
DA single type that conforms to both Alpha and Beta protocols
Attempts:
2 left
💡 Hint
Recall what protocol composition means in Swift type system.