0
0
Swiftprogramming~10 mins

Why protocol-oriented design 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'
ACar
BDriveable
CDrivable
DVehicle
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a class name instead of a 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
BRunnable
CDriveable
DDrivable
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong protocol name or a class name instead.
3fill in blank
hard

Fix the error in the code by completing the protocol method requirement.

Swift
protocol Drivable {
    func [1]()
}

struct Bike: Drivable {
    func drive() {
        print("Bike is driving")
    }
}
Drag options to blanks, or click blank then click option'
Adrive
Brun
Cstart
Dmove
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the one required by the protocol.
4fill in blank
hard

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

Swift
protocol [1] {
    func start()
}

extension [2] {
    func start() {
        print("Starting...")
    }
}
Drag options to blanks, or click blank then click option'
AStartable
BRunnable
DDrivable
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for protocol and extension.
5fill in blank
hard

Fill all three blanks to create a protocol with a property and a method, then extend it with default behavior.

Swift
protocol [1] {
    var name: String { get }
    func [2]()
}

extension [3] {
    func greet() {
        print("Hello, \(name)!")
    }
}
Drag options to blanks, or click blank then click option'
AGreetable
Bgreet
DDrivable
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching protocol and extension names or method names.