0
0
Swiftprogramming~20 mins

Protocol composition 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 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)
AGoodbye
BHello
CHello\nGoodbye
DCompilation error
Attempts:
2 left
💡 Hint
Remember that protocol composition requires the instance to conform to all protocols listed.
Predict Output
intermediate
2: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)
ACompilation error
B37
C0
D10
Attempts:
2 left
💡 Hint
Check how the sum of the two property values is calculated.
🔧 Debug
advanced
2: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)
AError: 'S' does not conform to protocol 'P2'
BRuntime error: method 'bar' not found
CCompilation error: missing return in function
DNo error, prints 'foo' and 'bar'
Attempts:
2 left
💡 Hint
Check which protocols the struct conforms to and what the function requires.
🧠 Conceptual
advanced
2: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()
A'obj' can only call betaMethod()
B'obj' can call both alphaMethod() and betaMethod()
C'obj' cannot call either method
D'obj' can only call alphaMethod()
Attempts:
2 left
💡 Hint
Consider what protocols 'obj' is declared to conform to and the actual class instance.
Predict Output
expert
3: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)
ACombo
BFirst
CSecond
DCompilation error due to ambiguity
Attempts:
2 left
💡 Hint
Check which implementation of action() is called when the struct provides its own.