0
0
Swiftprogramming~20 mins

Protocol inheritance in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Protocol Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of protocol inheritance with default implementation
What is the output of this Swift code using protocol inheritance and default implementations?
Swift
protocol A {
    func greet() -> String
}

protocol B: A {
    func greet() -> String
}

extension A {
    func greet() -> String {
        return "Hello from A"
    }
}

extension B {
    func greet() -> String {
        return "Hello from B"
    }
}

struct MyStruct: B {}

let obj = MyStruct()
print(obj.greet())
ACompilation error: Ambiguous method
BHello from A
CHello from B
DRuntime error
Attempts:
2 left
💡 Hint
Remember that protocol inheritance allows overriding default implementations in child protocols.
Predict Output
intermediate
2:00remaining
Value of property from multiple inherited protocols
What is the value of the property 'description' when accessed from an instance of struct C?
Swift
protocol P1 {
    var description: String { get }
}

protocol P2: P1 {
    var description: String { get }
}

extension P1 {
    var description: String { "P1" }
}

extension P2 {
    var description: String { "P2" }
}

struct C: P2 {}

let c = C()
print(c.description)
AP2
BCompilation error: Protocol requirement not satisfied
CRuntime error
DP1
Attempts:
2 left
💡 Hint
Check which protocol's extension provides the property implementation that is used.
🔧 Debug
advanced
2:00remaining
Identify the error in protocol inheritance with conflicting requirements
What error does this Swift code produce?
Swift
protocol Alpha {
    func doSomething() -> Int
}

protocol Beta {
    func doSomething() -> String
}

protocol Gamma: Alpha, Beta {}

struct Test: Gamma {
    func doSomething() -> Int {
        return 42
    }
}
AType 'Test' does not conform to protocol 'Beta'
BType 'Test' does not conform to protocol 'Alpha'
CNo error, compiles successfully
DAmbiguous use of 'doSomething()'
Attempts:
2 left
💡 Hint
Check the return types required by each protocol and what the struct implements.
📝 Syntax
advanced
1:00remaining
Correct syntax for protocol inheritance declaration
Which option shows the correct syntax to declare a protocol 'C' that inherits from protocols 'A' and 'B'?
Aprotocol C inherits A, B {}
Bprotocol C: A, B {}
Cprotocol C extends A, B {}
Dprotocol C < A, B > {}
Attempts:
2 left
💡 Hint
Swift uses a colon ':' to declare protocol inheritance.
🚀 Application
expert
3:00remaining
Determine the number of methods required by a conforming type
Given these protocols and extensions, how many methods must a struct implement to conform to protocol Z?
Swift
protocol X {
    func method1()
}

protocol Y: X {
    func method2()
}

protocol Z: Y {
    func method3()
}

extension X {
    func method1() { print("X method1") }
}

extension Y {
    func method2() { print("Y method2") }
}

// No extension for Z

struct MyStruct: Z {
    func method3() { print("Z method3") }
}
A0
B2
C3
D1
Attempts:
2 left
💡 Hint
Check which methods have default implementations and which do not.