Challenge - 5 Problems
POP vs OOP Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of protocol extension vs class inheritance
Consider the following Swift code. What will be printed when calling
dog.speak()?Swift
protocol Animal { func speak() } extension Animal { func speak() { print("Animal sound") } } class Dog: Animal { func speak() { print("Bark") } } let dog: Animal = Dog() dog.speak()
Attempts:
2 left
💡 Hint
Think about how protocol requirements and class methods interact in Swift.
✗ Incorrect
The class Dog implements the speak() method, which overrides the protocol extension default. When called on a Dog instance typed as Animal, the Dog's speak() is used, printing "Bark".
🧠 Conceptual
intermediate1:30remaining
Choosing between POP and OOP for code reuse
Which statement best describes when to prefer Protocol-Oriented Programming (POP) over Object-Oriented Programming (OOP) in Swift?
Attempts:
2 left
💡 Hint
Think about how protocols allow behavior sharing without inheritance.
✗ Incorrect
POP is ideal for sharing behavior across different types without forcing them into a class hierarchy. It uses protocols and protocol extensions to provide default implementations.
🔧 Debug
advanced2:30remaining
Why does this OOP code cause a runtime error?
Examine the Swift code below. Why does calling
vehicle.drive() cause a runtime crash?Swift
class Vehicle { func drive() { fatalError("Must override") } } class Car: Vehicle { } let vehicle: Vehicle = Car() vehicle.drive()
Attempts:
2 left
💡 Hint
Look at the method drive() in Vehicle and what happens if a subclass does not override it.
✗ Incorrect
The base class Vehicle's drive() method calls fatalError, which crashes the program. Since Car does not override drive(), calling drive() on a Car instance calls the base method and crashes.
📝 Syntax
advanced2:00remaining
Which code correctly implements protocol conformance with default implementation?
Given the protocol
Flyable with a default fly() method, which option compiles and prints "Flying" when calling bird.fly()?Swift
protocol Flyable { func fly() } extension Flyable { func fly() { print("Flying") } } struct Bird: Flyable { // Implementation here } let bird = Bird() bird.fly()
Attempts:
2 left
💡 Hint
Check if Bird needs to implement fly() explicitly when protocol extension provides it.
✗ Incorrect
Option A compiles and uses the default fly() from the protocol extension, printing "Flying". Option A also compiles but is redundant. Option A has wrong return type causing errors.
🚀 Application
expert3:00remaining
Deciding design pattern for a shape drawing app
You are designing a Swift app to draw shapes: circles, squares, and triangles. Each shape can be drawn and resized. You want to add new shapes easily and share drawing code without forcing a class hierarchy. Which approach fits best?
Attempts:
2 left
💡 Hint
Think about flexibility and code reuse without inheritance.
✗ Incorrect
POP allows adding new shapes easily by conforming to the Shape protocol. Protocol extensions enable sharing default behavior without class inheritance, making the design flexible and extensible.