0
0
Swiftprogramming~10 mins

Protocol composition in Swift - Interactive Code 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 Drivable.

Swift
protocol [1] {
    func drive()
}
Drag options to blanks, or click blank then click option'
ADriving
BDriveable
CDrivable
DDriver
Attempts:
3 left
💡 Hint
Common Mistakes
Using a noun instead of an adjective for the protocol name.
Misspelling the protocol name.
2fill in blank
medium

Complete the code to declare a protocol named Flyable with a fly() method.

Swift
protocol [1] {
    func fly()
}
Drag options to blanks, or click blank then click option'
AFlyable
BFlier
CFlying
DFly
Attempts:
3 left
💡 Hint
Common Mistakes
Using a noun instead of an adjective for the protocol name.
Forgetting to include the method signature.
3fill in blank
hard

Fix the error in the protocol composition syntax to combine Drivable and Flyable.

Swift
func operate(vehicle: [1]) {
    vehicle.drive()
    vehicle.fly()
}
Drag options to blanks, or click blank then click option'
ADrivable | Flyable
BDrivable & Flyable
CDrivable, Flyable
DDrivable + Flyable
Attempts:
3 left
💡 Hint
Common Mistakes
Using commas instead of '&' to combine protocols.
Using '|' or '+' which are invalid operators here.
4fill in blank
hard

Fill both blanks to declare a variable vehicle that conforms to both Drivable and Flyable protocols.

Swift
var vehicle: [1] = [2]()
Drag options to blanks, or click blank then click option'
ADrivable & Flyable
BCarPlane
CDrivable
DFlyable
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one protocol in the type annotation.
Using a class name that does not conform to both protocols.
5fill in blank
hard

Fill all three blanks to define a function testVehicle that accepts a parameter conforming to Drivable and Flyable, then calls both methods.

Swift
func testVehicle(vehicle: [1]) {
    vehicle.[2]()
    vehicle.[3]()
}
Drag options to blanks, or click blank then click option'
ADrivable & Flyable
Bdrive
Cfly
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like 'run'.
Not combining protocols correctly in the parameter type.