Bird
Raised Fist0
Swiftprogramming~20 mins

POP vs OOP decision patterns in Swift - Practice Questions

Choose your learning style10 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.

Practice

(1/5)
1. Which statement best describes the main difference between Protocol-Oriented Programming (POP) and Object-Oriented Programming (OOP) in Swift?
easy
A. POP focuses on protocols and structs, while OOP focuses on classes and inheritance.
B. POP uses only classes, and OOP uses only structs.
C. POP does not support code reuse, but OOP does.
D. OOP uses protocols, but POP does not.

Solution

  1. Step 1: Understand POP and OOP basics

    POP uses protocols to define behavior and structs or classes to implement it. OOP uses classes and inheritance to share and override behavior.
  2. Step 2: Compare the main focus of each paradigm

    POP emphasizes protocols and value types (structs), while OOP emphasizes classes and inheritance.
  3. Final Answer:

    POP focuses on protocols and structs, while OOP focuses on classes and inheritance. -> Option A
  4. Quick Check:

    POP = protocols & structs, OOP = classes & inheritance [OK]
Hint: POP = protocols + structs, OOP = classes + inheritance [OK]
Common Mistakes:
  • Thinking POP uses only classes
  • Believing OOP does not use protocols
  • Assuming POP cannot reuse code
2. Which of the following Swift code snippets correctly declares a protocol and a struct conforming to it?
easy
A. protocol Vehicle { func drive() }; struct Car: Vehicle { var speed: Int = 0 }
B. class Vehicle { func drive() }; struct Car inherits Vehicle { func drive() { print("Driving") } }
C. protocol Vehicle { func drive() }; struct Car: Vehicle { func drive() { print("Driving") } }
D. protocol Vehicle { func drive() }; class Car: Vehicle { func drive() { print("Driving") } }

Solution

  1. Step 1: Check protocol and struct syntax

    protocol Vehicle { func drive() }; struct Car: Vehicle { func drive() { print("Driving") } } correctly declares a protocol with a method and a struct conforming by implementing that method.
  2. Step 2: Identify syntax errors in other options

    class Vehicle { func drive() }; struct Car inherits Vehicle { func drive() { print("Driving") } } uses invalid inheritance syntax for struct. protocol Vehicle { func drive() }; struct Car: Vehicle { var speed: Int = 0 } misses method requirement. protocol Vehicle { func drive() }; class Car: Vehicle { func drive() { print("Driving") } } uses class instead of struct.
  3. Final Answer:

    protocol Vehicle { func drive() }; struct Car: Vehicle { func drive() { print("Driving") } } -> Option C
  4. Quick Check:

    Protocol + struct with method implementation = protocol Vehicle { func drive() }; struct Car: Vehicle { func drive() { print("Driving") } } [OK]
Hint: Protocols declare methods; structs implement them with correct syntax [OK]
Common Mistakes:
  • Using 'inherits' keyword with structs
  • Not implementing all protocol methods
  • Confusing class and struct declarations
3. What will be the output of the following Swift code?
protocol Greetable {
    func greet() -> String
}

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

class Employee: Person {
    override func greet() -> String {
        "Hello from Employee"
    }
}

let p: Greetable = Person()
let e: Greetable = Employee()
print(p.greet())
print(e.greet())
medium
A. Hello from Person Hello from Employee
B. Hello from Person Hello from Person
C. Hello from Employee Hello from Employee
D. Compilation error due to protocol conformance

Solution

  1. Step 1: Understand protocol conformance and inheritance

    Person conforms to Greetable with greet() returning "Hello from Person". Employee inherits Person and overrides greet() to return "Hello from Employee".
  2. Step 2: Analyze output of print statements

    Variable p is Person instance, so p.greet() returns "Hello from Person". Variable e is Employee instance, so e.greet() returns "Hello from Employee".
  3. Final Answer:

    Hello from Person Hello from Employee -> Option A
  4. Quick Check:

    Inheritance override works, prints Person then Employee [OK]
Hint: Subclass overrides method; output reflects actual instance type [OK]
Common Mistakes:
  • Assuming protocol type erases override
  • Confusing struct and class inheritance
  • Expecting compilation error for override
4. Identify the error in this Swift code that tries to use POP and OOP together:
protocol Drawable {
    func draw()
}

class Shape: Drawable {
    func draw() {
        print("Drawing shape")
    }
}

struct Circle: Shape {
    func draw() {
        print("Drawing circle")
    }
}
medium
A. Circle struct must use 'override' keyword for draw().
B. Protocol Drawable is missing required methods.
C. Class Shape cannot conform to protocol Drawable.
D. Struct cannot inherit from a class; Circle cannot inherit from Shape.

Solution

  1. Step 1: Check inheritance rules for structs and classes

    In Swift, structs cannot inherit from classes. Circle is a struct trying to inherit from class Shape, which is invalid.
  2. Step 2: Verify protocol conformance and method implementation

    Shape correctly conforms to Drawable. Circle tries to override draw(), but inheritance is invalid for structs.
  3. Final Answer:

    Struct cannot inherit from a class; Circle cannot inherit from Shape. -> Option D
  4. Quick Check:

    Structs cannot inherit classes in Swift [OK]
Hint: Structs cannot inherit classes; use protocols for shared behavior [OK]
Common Mistakes:
  • Thinking structs can inherit classes
  • Confusing protocol conformance with inheritance
  • Using 'override' in struct methods
5. You want to design a Swift app where multiple types share behavior but also have unique data. You want flexibility to add new behaviors without deep inheritance chains. Which approach fits best?
hard
A. Use deep class inheritance with base classes and subclasses for all types.
B. Use Protocol-Oriented Programming with protocols and structs to share behavior and data.
C. Use only classes without protocols to simplify code.
D. Use global functions instead of protocols or classes.

Solution

  1. Step 1: Analyze design goals

    The goal is to share behavior, have unique data, and avoid deep inheritance chains for flexibility.
  2. Step 2: Match goals to programming paradigms

    POP uses protocols and structs to share behavior flexibly without inheritance. OOP with deep inheritance is less flexible and can be complex.
  3. Final Answer:

    Use Protocol-Oriented Programming with protocols and structs to share behavior and data. -> Option B
  4. Quick Check:

    Flexible behavior sharing = POP with protocols & structs [OK]
Hint: For flexible shared behavior, prefer POP with protocols and structs [OK]
Common Mistakes:
  • Choosing deep inheritance causing rigid design
  • Ignoring protocols for behavior sharing
  • Using global functions losing type safety
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard