Bird
Raised Fist0
Swiftprogramming~20 mins

Protocol conformance via extension in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Protocol Extension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of protocol conformance via extension
What is the output of this Swift code when calling greet() on Person()?
Swift
protocol Greetable {
    func greet() -> String
}

struct Person {}

extension Person: Greetable {
    func greet() -> String {
        return "Hello from extension!"
    }
}

let p = Person()
print(p.greet())
AHello from extension!
BHello from Person struct!
CCompilation error: 'Person' does not conform to protocol 'Greetable'
DRuntime error: method not found
Attempts:
2 left
💡 Hint
Think about how Swift allows adding protocol conformance in extensions.
❓ Predict Output
intermediate
2:00remaining
Protocol method call with extension conformance
What will be printed when running this Swift code?
Swift
protocol Describable {
    func describe() -> String
}

struct Car {}

extension Car: Describable {
    func describe() -> String {
        "A fast car"
    }
}

func printDescription(_ item: Describable) {
    print(item.describe())
}

let myCar = Car()
printDescription(myCar)
ACompilation error: Cannot convert value of type 'Car' to expected argument type 'Describable'
BEmpty string
CRuntime error: method describe() not found
DA fast car
Attempts:
2 left
💡 Hint
Check how the extension adds protocol conformance to Car.
🔧 Debug
advanced
2:30remaining
Why does this protocol conformance fail?
Given this Swift code, why does it fail to compile?
Swift
protocol Runnable {
    func run() -> String
}

struct Animal {}

extension Animal: Runnable {}

extension Animal {
    func run() -> String {
        "Running fast"
    }
}

let a = Animal()
print(a.run())
ANo error, prints Running fast
BCompilation error because protocol conformance requires the method inside the extension that declares conformance
CCompilation error because Animal already has run() method in extension
DRuntime error because method run() is not implemented
Attempts:
2 left
💡 Hint
Check where the protocol method is implemented relative to the conformance declaration.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in protocol conformance via extension
Which option contains the syntax error preventing this Swift code from compiling?
Swift
protocol Flyable {
    func fly() -> String
}

struct Bird {}

extension Bird: Flyable {
    func fly() -> String {
        return "Flying high"
    }
}

let b = Bird()
print(b.fly())
Aextension Bird: Flyable { func fly() -> String { return "Flying high" }
Bextension Bird: Flyable { func fly() -> String { "Flying high" } }
Cextension Bird: Flyable { func fly() -> String { return "Flying high" } }
Dextension Bird: Flyable { func fly() -> String { print("Flying high") } }
Attempts:
2 left
💡 Hint
Look for missing braces or incomplete blocks.
🚀 Application
expert
2:30remaining
How many methods must be implemented for protocol conformance via extension?
Given this protocol and struct, how many methods must be implemented inside the extension to make Robot conform to Worker?
Swift
protocol Worker {
    func start() -> String
    func stop() -> String
}

struct Robot {}

// Extension to make Robot conform to Worker goes here
ANo methods, just declare conformance in extension
BOne method: either start() or stop()
CTwo methods: start() and stop()
DThree methods: start(), stop(), and reset()
Attempts:
2 left
💡 Hint
Protocols require all their methods to be implemented to conform.

Practice

(1/5)
1. What is the main purpose of using an extension to conform a type to a protocol in Swift?
easy
A. To add protocol requirements to a type without modifying its original definition
B. To create a new subclass from an existing class
C. To override existing methods in a class
D. To declare a new protocol

Solution

  1. Step 1: Understand protocol conformance

    Protocols define a set of methods or properties a type must implement.
  2. Step 2: Role of extensions

    Extensions allow adding these implementations to a type without changing its original code.
  3. Final Answer:

    To add protocol requirements to a type without modifying its original definition -> Option A
  4. Quick Check:

    Extensions add protocol conformance without altering original type [OK]
Hint: Extensions add protocol methods without touching original code [OK]
Common Mistakes:
  • Thinking extensions create subclasses
  • Confusing extensions with method overriding
  • Believing extensions declare new protocols
2. Which of the following is the correct syntax to make a struct Car conform to protocol Drivable using an extension?
easy
A. protocol Drivable extension Car { func drive() { print("Driving") } }
B. struct Car: Drivable { extension { func drive() { print("Driving") } } }
C. extension Drivable: Car { func drive() { print("Driving") } }
D. extension Car: Drivable { func drive() { print("Driving") } }

Solution

  1. Step 1: Identify correct extension syntax

    Extensions use the format: extension TypeName: ProtocolName { ... }.
  2. Step 2: Check each option

    extension Car: Drivable { func drive() { print("Driving") } } correctly extends Car to conform to Drivable and implements drive().
  3. Final Answer:

    extension Car: Drivable { func drive() { print("Driving") } } -> Option D
  4. Quick Check:

    Correct extension syntax = extension Car: Drivable { func drive() { print("Driving") } } [OK]
Hint: Use 'extension Type: Protocol { }' to conform [OK]
Common Mistakes:
  • Placing extension inside struct
  • Swapping type and protocol names
  • Using 'protocol' keyword incorrectly
3. Consider the code below:
protocol Greetable {
    func greet() -> String
}

struct Person {
    let name: String
}

extension Person: Greetable {
    func greet() -> String {
        return "Hello, \(name)!"
    }
}

let p = Person(name: "Anna")
print(p.greet())

What is the output when this code runs?
medium
A. Hello, !
B. Hello, name!
C. "Hello, Anna!"
D. Error: Person does not conform to Greetable

Solution

  1. Step 1: Check protocol conformance

    Person is extended to conform to Greetable by implementing greet() returning "Hello, \(name)!".
  2. Step 2: Analyze output

    Creating Person with name "Anna" and calling greet() returns "Hello, Anna!" which is printed.
  3. Final Answer:

    "Hello, Anna!" -> Option C
  4. Quick Check:

    Extension adds greet() returning personalized string [OK]
Hint: Extension adds greet() returning personalized message [OK]
Common Mistakes:
  • Expecting error due to missing conformance
  • Ignoring string interpolation
  • Assuming default greet() implementation
4. The following code tries to make Rectangle conform to Shape via extension, but it causes a compile error. What is the error?
protocol Shape {
    var area: Double { get }
}

struct Rectangle {
    var width: Double
    var height: Double
}

extension Rectangle: Shape {
    func area() -> Double {
        return width * height
    }
}
medium
A. Method 'area()' should be a computed property, not a function
B. Cannot extend a struct to conform to a protocol
C. Missing initializer in Rectangle
D. Protocol Shape requires a method named 'area()', not a property

Solution

  1. Step 1: Check protocol requirement

    Shape requires a property area of type Double with a getter.
  2. Step 2: Analyze extension implementation

    The extension implements area() as a method, but protocol expects a computed property var area: Double { get }.
  3. Final Answer:

    Method 'area()' should be a computed property, not a function -> Option A
  4. Quick Check:

    Property requirement needs computed property, not method [OK]
Hint: Match protocol property with computed property, not method [OK]
Common Mistakes:
  • Implementing property as method
  • Thinking structs can't conform via extension
  • Ignoring protocol property vs method difference
5. You have a protocol Printable requiring a method printDetails(). You want to add this conformance to an existing class Book without changing its original code. Which approach correctly achieves this and allows printDetails() to print the book's title and author?
class Book {
    let title: String
    let author: String
    init(title: String, author: String) {
        self.title = title
        self.author = author
    }
}
hard
A. class Book: Printable { func printDetails() { print(title + author) } }
B. extension Book: Printable { func printDetails() { print("Title: \(title), Author: \(author)") } }
C. protocol Printable { func printDetails() } extension Printable { func printDetails() { print("No details") } }
D. extension Printable: Book { func printDetails() { print(title + author) } }

Solution

  1. Step 1: Understand requirement

    We want to add Printable conformance to Book without changing Book's original code.
  2. Step 2: Use extension to conform

    extension Book: Printable { func printDetails() { print("Title: \(title), Author: \(author)") } } extends Book to conform to Printable and implements printDetails() accessing Book's properties.
  3. Final Answer:

    extension Book: Printable { func printDetails() { print("Title: \(title), Author: \(author)") } } -> Option B
  4. Quick Check:

    Extension adds protocol conformance and method using existing properties [OK]
Hint: Use extension on class to add protocol methods accessing properties [OK]
Common Mistakes:
  • Trying to modify original class code
  • Incorrectly extending protocol instead of class
  • Not implementing required method
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