Bird
Raised Fist0
Swiftprogramming~20 mins

Protocol inheritance in Swift - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does protocol inheritance in Swift allow you to do?
easy
A. Create instances of protocols directly
B. Inherit properties and methods from a class
C. Override methods in a protocol
D. Create a new protocol that includes requirements from one or more existing protocols

Solution

  1. Step 1: Understand protocol inheritance concept

    Protocol inheritance lets you build new protocols by combining existing ones, so the new protocol requires all combined features.
  2. Step 2: Compare options with concept

    Only Create a new protocol that includes requirements from one or more existing protocols correctly describes creating a new protocol that inherits requirements from existing protocols.
  3. Final Answer:

    Create a new protocol that includes requirements from one or more existing protocols -> Option D
  4. Quick Check:

    Protocol inheritance = create combined protocols [OK]
Hint: Protocol inheritance combines requirements from multiple protocols [OK]
Common Mistakes:
  • Confusing protocol inheritance with class inheritance
  • Thinking protocols can be instantiated
  • Believing protocols can override methods
2. Which of the following is the correct syntax to declare a protocol AdvancedProtocol that inherits from BasicProtocol in Swift?
easy
A. protocol AdvancedProtocol: BasicProtocol {}
B. protocol AdvancedProtocol inherits BasicProtocol {}
C. protocol AdvancedProtocol extends BasicProtocol {}
D. protocol AdvancedProtocol implements BasicProtocol {}

Solution

  1. Step 1: Recall Swift protocol inheritance syntax

    In Swift, a protocol inherits from another by listing the parent protocol after a colon (:).
  2. Step 2: Match syntax with options

    Only protocol AdvancedProtocol: BasicProtocol {} uses the correct syntax: protocol NewProtocol: ExistingProtocol {}.
  3. Final Answer:

    protocol AdvancedProtocol: BasicProtocol {} -> Option A
  4. Quick Check:

    Protocol inheritance syntax uses colon (:) [OK]
Hint: Use colon (:) to inherit protocols in Swift [OK]
Common Mistakes:
  • Using 'inherits' or 'extends' keywords which are invalid in Swift
  • Using 'implements' keyword which is for classes, not protocols
  • Missing the colon between protocol names
3. Given the following code, what will be the output when example.printInfo() is called?
protocol A {
    func printInfo()
}
protocol B: A {
    func printExtra()
}
class Example: B {
    func printInfo() {
        print("Info from A")
    }
    func printExtra() {
        print("Extra from B")
    }
}
let example = Example()
example.printInfo()
medium
A. Info from A
B. Extra from B
C. No output, compile error
D. Info from A\nExtra from B

Solution

  1. Step 1: Understand protocol inheritance and class conformance

    Protocol B inherits from A, so class Example must implement both printInfo() and printExtra(). It does.
  2. Step 2: Identify which method is called

    The code calls example.printInfo(), which prints "Info from A".
  3. Final Answer:

    Info from A -> Option A
  4. Quick Check:

    Calling printInfo() prints "Info from A" [OK]
Hint: Match called method to implemented function output [OK]
Common Mistakes:
  • Confusing which method is called
  • Expecting both methods to print automatically
  • Thinking protocol inheritance affects method output
4. What is wrong with the following Swift code?
protocol P1 {
    func method1()
}
protocol P2: P1 {
    func method2()
}
class MyClass: P2 {
    func method2() {
        print("Method 2")
    }
}
medium
A. P2 cannot inherit from P1
B. method2() should not be implemented in MyClass
C. MyClass must implement method1() because P2 inherits from P1
D. MyClass should be a protocol, not a class

Solution

  1. Step 1: Analyze protocol inheritance and class conformance

    Protocol P2 inherits from P1, so any class conforming to P2 must implement all methods from P1 and P2.
  2. Step 2: Check implemented methods in MyClass

    MyClass implements only method2() but misses method1(), causing a compile error.
  3. Final Answer:

    MyClass must implement method1() because P2 inherits from P1 -> Option C
  4. Quick Check:

    Inherited protocol methods must be implemented [OK]
Hint: Implement all inherited protocol methods in conforming class [OK]
Common Mistakes:
  • Ignoring inherited protocol requirements
  • Thinking only new protocol methods must be implemented
  • Confusing class and protocol roles
5. You have two protocols:
protocol Vehicle {
    func start()
}
protocol ElectricVehicle: Vehicle {
    func charge()
}

You want to create a class ElectricCar that conforms to ElectricVehicle. Which implementation correctly satisfies all protocol requirements?
hard
A. class ElectricCar: ElectricVehicle { func start() { print("Starting") } }
B. class ElectricCar: ElectricVehicle { func start() { print("Starting") } func charge() { print("Charging") } }
C. class ElectricCar: Vehicle { func charge() { print("Charging") } }
D. class ElectricCar: ElectricVehicle { func charge() { print("Charging") } }

Solution

  1. Step 1: Understand protocol inheritance requirements

    ElectricVehicle inherits from Vehicle, so conforming types must implement both start() and charge().
  2. Step 2: Check each class implementation

    class ElectricCar: ElectricVehicle { func start() { print("Starting") } func charge() { print("Charging") } } implements both methods, fulfilling all requirements. Options B and D miss charge() or start(). class ElectricCar: Vehicle { func charge() { print("Charging") } } does not conform to ElectricVehicle.
  3. Final Answer:

    class ElectricCar: ElectricVehicle { func start() { print("Starting") } func charge() { print("Charging") } } -> Option B
  4. Quick Check:

    Implement all inherited protocol methods in class [OK]
Hint: Implement all methods from inherited protocols in your class [OK]
Common Mistakes:
  • Implementing only one protocol method
  • Conforming to wrong protocol
  • Missing methods from inherited protocols
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard