0
0
Swiftprogramming~20 mins

Why protocol-oriented design 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 will be printed when this Swift code runs?
Swift
protocol Greetable {
    func greet()
}

extension Greetable {
    func greet() {
        print("Hello from protocol extension")
    }
}

struct Person: Greetable {
    func greet() {
        print("Hello from Person struct")
    }
}

let p: Greetable = Person()
p.greet()
AHello from protocol extension
BRuntime error: method not found
CCompilation error: greet() not implemented
DHello from Person struct
Attempts:
2 left
💡 Hint
Think about how protocol extensions provide default implementations and how method dispatch works with protocol-typed variables.
🧠 Conceptual
intermediate
1:30remaining
Why use protocols instead of base classes?
Which of the following is the main advantage of using protocols over base classes in Swift's protocol-oriented design?
AProtocols can be instantiated directly, classes cannot.
BProtocols can store properties with default values, classes cannot.
CProtocols allow multiple inheritance of behavior, while classes do not.
DProtocols enforce implementation of stored properties, classes do not.
Attempts:
2 left
💡 Hint
Think about how Swift handles inheritance and how protocols differ from classes.
🔧 Debug
advanced
2:30remaining
Fix the unexpected output in protocol extension
This Swift code prints "Hello from protocol extension" but the developer expects "Hello from Dog struct". What is the cause?
Swift
protocol Animal {
    func speak()
}

extension Animal {
    func speak() {
        print("Hello from protocol extension")
    }
}

struct Dog: Animal {
    func speak() {
        print("Hello from Dog struct")
    }
}

let pet: Animal = Dog()
pet.speak()
ADog struct does not implement speak(), so protocol extension is used.
BThe variable pet is typed as Animal, so protocol extension method is called due to static dispatch.
CThe speak() method in Dog is private, so protocol extension is called.
DThe protocol Animal requires a default implementation, so Dog's method is ignored.
Attempts:
2 left
💡 Hint
Consider how Swift decides which method to call when using protocol-typed variables.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in protocol conformance
Which option contains the correct syntax to make a struct conform to a protocol with a method requirement?
Swift
protocol Runner {
    func run()
}

struct Athlete {
    // Conformance here
}
A
struct Athlete: Runner {
    func run() {
        print("Running")
    }
}
B
struct Athlete implements Runner {
    func run() {
        print("Running")
    }
}
C
struct Athlete extends Runner {
    func run() {
        print("Running")
    }
}
D
struct Athlete conforms Runner {
    func run() {
        print("Running")
    }
}
Attempts:
2 left
💡 Hint
Remember the Swift keyword to declare protocol conformance.
🚀 Application
expert
3:00remaining
Determine the number of methods available via protocol composition
Given the protocols and struct below, how many unique methods can be called on the variable 'obj' typed as 'A & B'?
Swift
protocol A {
    func methodA()
}

protocol B {
    func methodB()
    func methodC()
}

struct C: A, B {
    func methodA() { print("A") }
    func methodB() { print("B") }
    func methodC() { print("C") }
    func methodD() { print("D") }
}

let obj: A & B = C()
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
Count only the methods declared in the protocols A and B accessible via the protocol composition type.