0
0
Swiftprogramming~20 mins

Why protocol-oriented programming matters in Swift - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Protocol Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of protocol extension method call

What is the output of this Swift code using protocol extensions?

Swift
protocol Greetable {
    func greet() -> String
}

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

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

let p: Greetable = Person()
print(p.greet())
A"Hello from Person"
B"Hello from protocol extension"
CCompilation error: protocol method not implemented
DRuntime error: method not found
Attempts:
2 left
💡 Hint

Remember that when a concrete type implements a protocol method, that implementation is called instead of the protocol extension's default.

🧠 Conceptual
intermediate
1:30remaining
Why use protocols instead of base classes?

Which of the following is a key advantage of using protocols over base classes in Swift?

AProtocols allow multiple inheritance of behavior, while classes do not.
BProtocols can store properties with default values, classes cannot.
CProtocols automatically generate initializers for conforming types.
DProtocols enforce implementation of stored properties.
Attempts:
2 left
💡 Hint

Think about how Swift handles multiple inheritance and code reuse.

🔧 Debug
advanced
2:00remaining
Why does this protocol extension method not get called?

Given this code, why does the protocol extension method describe() not get called when using a concrete type?

Swift
protocol Describable {
    func describe() -> String
}

extension Describable {
    func describe() -> String {
        return "Default description"
    }
}

struct Item: Describable {
    // No describe() implementation here
}

let item = Item()
print(item.describe())
ARuntime error because <code>describe()</code> is not implemented in <code>Item</code>.
BCompilation error because <code>Item</code> does not implement <code>describe()</code>.
C"Default description" is printed because the extension provides a default implementation.
D"describe()" prints nothing because the method is empty.
Attempts:
2 left
💡 Hint

Protocol extensions can provide default implementations that conforming types inherit automatically.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in protocol conformance

Which option contains a syntax error when trying to conform to a protocol with an associated type?

Swift
protocol Container {
    associatedtype Item
    func append(_ item: Item)
}

struct IntStack: Container {
    var items = [Int]()
    func append(_ item: Int) {
        items.append(item)
    }
}
A
struct IntStack: Container {
    var items = [Int]()
    mutating func append(_ item: Int) {
        items = items + [item]
    }
}
B
struct IntStack: Container {
    var items = [Int]()
    mutating func append(_ item: Int) {
        items.append(item)
    }
}
C
struct IntStack: Container {
    var items = [Int]()
    func append(item: Int) {
        items.append(item)
    }
}
D
struct IntStack: Container {
    var items = [Int]()
    func append(_ item: Int) {
        items.append(item)
    }
}
Attempts:
2 left
💡 Hint

Consider whether the method modifies the struct's properties and what keyword is needed.

🚀 Application
expert
2:30remaining
How does protocol-oriented programming improve code flexibility?

Which statement best explains how protocol-oriented programming improves flexibility in Swift code?

AIt forces all types to inherit from a common base class, simplifying type hierarchies.
BIt allows behavior to be shared through protocols and extensions without requiring inheritance, enabling composition.
CIt restricts types to implement only one protocol, reducing complexity.
DIt eliminates the need for generics by using protocols exclusively.
Attempts:
2 left
💡 Hint

Think about how protocols and extensions enable code reuse without inheritance.