0
0
Swiftprogramming~10 mins

Why protocol-oriented programming matters in Swift - Test Your Understanding

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 'Drivable'.

Swift
protocol [1] {
    func drive()
}
Drag options to blanks, or click blank then click option'
ADrivable
BVehicle
CDriveable
DMovable
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class or struct name instead of a protocol name.
Misspelling the protocol name.
2fill in blank
medium

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

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

Fix the error in the protocol extension to provide a default implementation.

Swift
extension Drivable {
    func [1]() {
        print("Default driving behavior")
    }
}
Drag options to blanks, or click blank then click option'
Adrive
BDrive
Cdriving
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name in the extension.
Capitalizing the method name incorrectly.
4fill in blank
hard

Fill both blanks to create a protocol and a struct that uses protocol-oriented programming.

Swift
protocol [1] {
    func [2]()
}

struct Bike: [1] {
    func [2]() {
        print("Bike is moving")
    }
}
Drag options to blanks, or click blank then click option'
AMovable
Bmove
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent names between protocol and struct.
Using method names that don't match the protocol.
5fill in blank
hard

Fill all three blanks to create a protocol with a default implementation and a struct that uses it.

Swift
protocol [1] {
    func [2]()
}

extension [1] {
    func [2]() {
        print("Default implementation")
    }
}

struct Scooter: [1] {}
Drag options to blanks, or click blank then click option'
ARideable
Bride
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching protocol names between declaration and extension.
Not matching method names exactly.