Bird
Raised Fist0
Swiftprogramming~20 mins

Protocol conformance in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Protocol Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of protocol conformance with default implementation
What is the output of this Swift code when calling dog.speak()?
Swift
protocol Animal {
    func speak() -> String
}

extension Animal {
    func speak() -> String {
        return "Some sound"
    }
}

struct Dog: Animal {
    func speak() -> String {
        return "Woof!"
    }
}

let dog = Dog()
print(dog.speak())
A"Dog speaks"
B"Some sound"
CCompilation error: Missing method implementation
D"Woof!"
Attempts:
2 left
💡 Hint
Check if the struct provides its own implementation of the protocol method.
❓ Predict Output
intermediate
2:00remaining
Protocol conformance with optional method
What will be printed when running this Swift code?
Swift
@objc protocol Greeter {
    @objc optional func greet() -> String
}

class Person: Greeter {
}

let p = Person()
print(p.greet?() ?? "No greeting")
ARuntime error: Method not found
B"Optional greeting"
C"No greeting"
DCompilation error: Optional protocol methods require @objc
Attempts:
2 left
💡 Hint
Check if the class implements the optional method or not.
🔧 Debug
advanced
2:00remaining
Fix the protocol conformance error
This code does not compile. What is the cause of the error?
Swift
protocol Vehicle {
    var speed: Int { get set }
    func description() -> String
}

struct Car: Vehicle {
    var speed: Int
    func description() -> String {
        return "Car speed is \(speed)"
    }
}

let myCar = Car(speed: 100)
print(myCar.description())
AThe protocol Vehicle must declare speed as 'let' instead of 'var'.
BThe struct Car must mark speed as 'var' to conform to the protocol's get set requirement.
CThe struct Car must mark speed as 'let' to conform to the protocol.
DThe description() method must be marked as 'mutating' in Car.
Attempts:
2 left
💡 Hint
Check the property requirements in the protocol and how Car declares speed.
❓ Predict Output
advanced
2:00remaining
Protocol conformance with associated types
What is the output of this Swift code?
Swift
protocol Container {
    associatedtype Item
    var items: [Item] { get set }
    func count() -> Int
}

struct IntContainer: Container {
    var items: [Int]
    func count() -> Int {
        items.count
    }
}

let container = IntContainer(items: [1, 2, 3, 4])
print(container.count())
A4
BCompilation error: Missing associatedtype implementation
C0
DRuntime error
Attempts:
2 left
💡 Hint
Check how many items are in the array and what count() returns.
❓ Predict Output
expert
2:00remaining
Protocol conformance and method dispatch
What is the output of this Swift code?
Swift
protocol Speaker {
    func speak() -> String
}

struct Cat: Speaker {
    func speak() -> String {
        "Meow"
    }
}

func makeSpeak(_ speaker: Speaker) {
    print(speaker.speak())
}

let cat = Cat()
makeSpeak(cat)
A"Meow"
B"speak()"
CCompilation error: Protocol method not implemented
DRuntime error: Protocol witness table missing
Attempts:
2 left
💡 Hint
Check if Cat implements the required method and how protocol methods are called.

Practice

(1/5)
1. What does it mean for a Swift type to conform to a protocol?
easy
A. It can only be a class, not a struct or enum.
B. It inherits all properties and methods from the protocol automatically.
C. It implements all required methods and properties defined by the protocol.
D. It must override all methods from the protocol.

Solution

  1. Step 1: Understand protocol requirements

    A protocol defines methods and properties that a type must implement to conform.
  2. Step 2: Check what conformance means

    Conforming means the type provides implementations for all required members.
  3. Final Answer:

    It implements all required methods and properties defined by the protocol. -> Option C
  4. Quick Check:

    Protocol conformance = Implement required members [OK]
Hint: Conformance means implementing all required protocol parts [OK]
Common Mistakes:
  • Thinking conformance means inheritance
  • Believing only classes can conform
  • Assuming methods must be overridden
2. Which of the following is the correct syntax to declare that a struct conforms to a protocol named Drawable?
easy
A. struct Shape inherits Drawable { }
B. struct Shape : Drawable { }
C. struct Shape implements Drawable { }
D. struct Shape extends Drawable { }

Solution

  1. Step 1: Recall Swift syntax for protocol conformance

    Swift uses a colon (:) after the type name to declare protocol conformance.
  2. Step 2: Match syntax options

    Only struct Shape : Drawable { } uses the correct colon syntax for protocol conformance.
  3. Final Answer:

    struct Shape : Drawable { } -> Option B
  4. Quick Check:

    Protocol conformance syntax uses ':' [OK]
Hint: Use ':' after type name to conform to protocol [OK]
Common Mistakes:
  • Using 'implements' or 'inherits' keywords
  • Confusing inheritance with protocol conformance
  • Using 'extends' which is not Swift syntax
3. What will be the output of this Swift code?
protocol Greetable {
    func greet() -> String
}

struct Person: Greetable {
    func greet() -> String {
        "Hello!"
    }
}

let p = Person()
print(p.greet())
medium
A. Hello!
B. Error: Missing greet() implementation
C. nil
D. Hello! Person

Solution

  1. Step 1: Check protocol and struct conformance

    The protocol requires a greet() method returning String. Person implements greet() returning "Hello!".
  2. Step 2: Analyze print output

    Calling p.greet() returns "Hello!", so print outputs that string.
  3. Final Answer:

    Hello! -> Option A
  4. Quick Check:

    Implemented greet() returns "Hello!" [OK]
Hint: Implemented method returns string printed directly [OK]
Common Mistakes:
  • Assuming missing implementation error
  • Expecting default protocol method output
  • Confusing print output with type description
4. Identify the error in this Swift code that tries to conform to the Equatable protocol:
struct Point: Equatable {
    var x: Int
    var y: Int
}
medium
A. Missing implementation of == operator function
B. Struct cannot conform to Equatable
C. Properties must be constants (let) to conform
D. No error, code compiles fine

Solution

  1. Step 1: Understand Equatable conformance rules

    Swift automatically synthesizes == for structs with Equatable properties.
  2. Step 2: Check struct properties and conformance

    Both x and y are Int (Equatable), so compiler provides == automatically.
  3. Final Answer:

    No error, code compiles fine -> Option D
  4. Quick Check:

    Automatic Equatable synthesis = No error [OK]
Hint: Swift auto-synthesizes Equatable for simple structs [OK]
Common Mistakes:
  • Thinking manual == is always needed
  • Believing structs can't conform
  • Assuming properties must be let
5. Given this protocol and struct, how can you modify Vehicle to conform to CustomStringConvertible and print a description?
protocol CustomStringConvertible {
    var description: String { get }
}

struct Vehicle {
    var make: String
    var model: String
}
hard
A. Add extension Vehicle: CustomStringConvertible { var description: String { "\(make) \(model)" } }
B. Add func description() -> String { return "\(make) \(model)" } inside Vehicle
C. Add var description: String = "\(make) \(model)" inside Vehicle
D. Add override var description: String { "\(make) \(model)" } inside Vehicle

Solution

  1. Step 1: Understand protocol property requirement

    CustomStringConvertible requires a read-only computed property 'description' of type String.
  2. Step 2: Implement description correctly

    Implementing description as a computed property in an extension is the correct way.
  3. Final Answer:

    Add extension Vehicle: CustomStringConvertible { var description: String { "\(make) \(model)" } } -> Option A
  4. Quick Check:

    Computed property in extension = Correct conformance [OK]
Hint: Use computed property in extension for protocol conformance [OK]
Common Mistakes:
  • Trying to add description as a method
  • Using stored property instead of computed
  • Using override keyword in struct
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