Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a protocol named Drivable.
Swift
protocol [1] {
func drive()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a noun instead of an adjective for the protocol name.
Misspelling the protocol name.
✗ Incorrect
The protocol is named
Drivable to represent something that can be driven.2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a noun instead of an adjective for the protocol name.
Forgetting to include the method signature.
✗ Incorrect
The protocol is named
Flyable to indicate the ability to fly.3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using commas instead of '&' to combine protocols.
Using '|' or '+' which are invalid operators here.
✗ Incorrect
In Swift, protocol composition uses the ampersand (&) to combine protocols.
4fill in blank
hardFill 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'
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.
✗ Incorrect
The variable type uses protocol composition with '&', and the instance is of a class named
CarPlane that conforms to both protocols.5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like 'run'.
Not combining protocols correctly in the parameter type.
✗ Incorrect
The parameter type uses protocol composition. The function calls the
drive() and fly() methods defined in the protocols.