0
0
Swiftprogramming~10 mins

POP vs OOP decision patterns in Swift - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a protocol named 'Vehicle'.

Swift
protocol [1] {
    func drive()
}
Drag options to blanks, or click blank then click option'
AVehicle
BCar
CDriveable
DTransport
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name instead of a protocol name
Naming the protocol something unrelated
2fill in blank
medium

Complete the code to make 'Car' conform to the 'Vehicle' protocol.

Swift
struct Car: [1] {
    func drive() {
        print("Car is driving")
    }
}
Drag options to blanks, or click blank then click option'
ADriveable
BTransport
CVehicle
DMovable
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different protocol name
Forgetting to conform to the protocol
3fill in blank
hard

Fix the error in the class inheritance to use OOP correctly.

Swift
class [1] {
    func drive() {
        print("Driving")
    }
}

class SportsCar: Vehicle {
}
Drag options to blanks, or click blank then click option'
ACar
BVehicle
CDriveable
DTransport
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the base class differently than the inherited class
Using a protocol name instead of a class name
4fill in blank
hard

Fill both blanks to create a protocol extension that provides a default implementation.

Swift
protocol Vehicle {
    func drive()
}

extension Vehicle {
    func [1]() {
        print("[2] driving")
    }
}
Drag options to blanks, or click blank then click option'
Adrive
Bstart
CVehicle
DCar
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name
Printing the wrong type name
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension filtering vehicles by speed.

Swift
let vehicles = ["car": 120, "bike": 80, "truck": 100]
let fastVehicles = vehicles.filter { $0.value [1] [2] }.map { $0.key.uppercased() }
let result = fastVehicles.reduce(into: [[3]: true]) { $0[$1] = true }
Drag options to blanks, or click blank then click option'
A>
B90
C""
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>'
Using a wrong threshold value
Using wrong dictionary key type