0
0
Swiftprogramming~20 mins

Protocol extensions for shared behavior in Swift - Practice Problems & Coding Challenges

Choose your learning style9 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 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"
    }
}

struct Robot: Greetable {}

let p = Person()
let r = Robot()
print(p.greet())
print(r.greet())
ACompilation error: Robot does not implement greet()
B
Hello from Person
Hello from protocol extension
C
Hello from protocol extension
Hello from protocol extension
D
Hello from Person
Hello from Person
Attempts:
2 left
💡 Hint
Remember that structs can override protocol extension methods by implementing them directly.
🧠 Conceptual
intermediate
1:30remaining
Protocol extension default implementation usage
Which statement about protocol extensions providing default implementations is true?
ADefault implementations are ignored if the protocol is used as a type.
BDefault implementations override any implementation in the conforming type.
CProtocol extensions cannot provide default implementations for protocol methods.
DDefault implementations in protocol extensions are used only if the conforming type does not provide its own implementation.
Attempts:
2 left
💡 Hint
Think about how Swift chooses which method to call when both protocol extension and conforming type have the same method.
🔧 Debug
advanced
2:00remaining
Why does this protocol extension method not get called?
Given this code, why does the protocol extension method not get called when using a protocol-typed variable?
Swift
protocol Drawable {
    func draw()
}

extension Drawable {
    func draw() {
        print("Drawing from extension")
    }
}

struct Circle: Drawable {
    func draw() {
        print("Drawing Circle")
    }
}

let shape: Drawable = Circle()
shape.draw()
AThe Circle's draw() method is called because the variable is of protocol type and Circle implements draw().
BThe protocol extension's draw() method is called because the variable is of protocol type.
CCompilation error because protocol extension methods cannot be called on protocol-typed variables.
DRuntime error because draw() is ambiguous.
Attempts:
2 left
💡 Hint
Consider how Swift dispatches protocol methods when called on a variable typed as the protocol.
📝 Syntax
advanced
1:30remaining
Which option causes a syntax error in protocol extension?
Which of the following protocol extension code snippets will cause a syntax error?
A
extension Sequence {
    func firstElement() -> Element? {
        return self.first
    }
}
B
extension Collection {
    func isNotEmpty() -> Bool {
        return !self.isEmpty
    }
}
C
extension Array {
    func reversedArray() -> Array {
        return self.reversed()
    }
}
D
extension Dictionary {
    func keysArray() -> [Key] {
        return Array(self.keys)
    }
}
Attempts:
2 left
💡 Hint
Check the return type and method used in the extension carefully.
🚀 Application
expert
2:30remaining
How many items are in the resulting dictionary?
Consider this Swift code using protocol extensions and conforming types. How many key-value pairs does the dictionary contain after execution?
Swift
protocol Identifiable {
    var id: String { get }
}

extension Identifiable {
    func info() -> String {
        return "ID: \(id)"
    }
}

struct User: Identifiable {
    var id: String
}

struct Admin: Identifiable {
    var id: String
    func info() -> String {
        return "Admin ID: \(id)"
    }
}

let users: [Identifiable] = [User(id: "u1"), Admin(id: "a1"), User(id: "u2")]

var dict = [String: String]()
for user in users {
    dict[user.id] = user.info()
}

print(dict.count)
A3
B2
CCompilation error
D1
Attempts:
2 left
💡 Hint
Each user has a unique id used as dictionary key.