0
0
Swiftprogramming~20 mins

POP vs OOP decision patterns in Swift - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
POP vs OOP Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
AAnimal sound
BNo output
CCompilation error
DBark
Attempts:
2 left
💡 Hint
Think about how protocol requirements and class methods interact in Swift.
🧠 Conceptual
intermediate
1: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?
AUse OOP when you want to avoid code reuse and write separate implementations.
BUse POP when you want to share behavior across unrelated types without inheritance.
CUse OOP when you want to avoid using protocols and prefer structs only.
DUse POP only when you need to create a class hierarchy with shared state.
Attempts:
2 left
💡 Hint
Think about how protocols allow behavior sharing without inheritance.
🔧 Debug
advanced
2: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()
ABecause Car is not marked as final, it cannot override methods.
BBecause Vehicle is a class, it cannot be instantiated directly.
CBecause Car does not override drive(), calling drive() calls fatalError causing a crash.
DBecause drive() is not marked as @objc, it cannot be called dynamically.
Attempts:
2 left
💡 Hint
Look at the method drive() in Vehicle and what happens if a subclass does not override it.
📝 Syntax
advanced
2: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()
Astruct Bird: Flyable {}
Bstruct Bird: Flyable { func fly() { print("Flying") } }
Cstruct Bird: Flyable { func fly() -> String { "Flying" } }
Dstruct Bird: Flyable { func fly() -> Void { print("Flying") } }
Attempts:
2 left
💡 Hint
Check if Bird needs to implement fly() explicitly when protocol extension provides it.
🚀 Application
expert
3: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?
AUse Protocol-Oriented Programming with a Shape protocol defining draw() and resize(), and provide default implementations via protocol extensions.
BUse Object-Oriented Programming with a base Shape class and subclasses for each shape overriding draw() and resize().
CUse structs without protocols and duplicate draw() and resize() code in each shape struct.
DUse global functions for draw() and resize() that take any shape type as parameters.
Attempts:
2 left
💡 Hint
Think about flexibility and code reuse without inheritance.