Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a protocol with a required method.
Swift
protocol Vehicle {
func [1]()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that are not descriptive of vehicle actions.
Forgetting to include parentheses for method declaration.
✗ Incorrect
The protocol requires a method named 'drive' with no parameters.
2fill in blank
mediumComplete the code to declare a protocol property requirement that is gettable and settable.
Swift
protocol Person {
var name: String { [1] }
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using only 'get' which makes the property read-only.
Using 'set' alone which is invalid syntax.
✗ Incorrect
The property must be both gettable and settable, so use 'get set'.
3fill in blank
hardFix the error in the protocol method declaration by completing the code.
Swift
protocol Calculator {
func calculate(_ a: Int, _ b: Int) -> [1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Void which means no return value.
Using String or Bool which do not match the calculation result type.
✗ Incorrect
The method should return an Int as it calculates a result from two Int inputs.
4fill in blank
hardFill both blanks to declare a protocol with a property and a method requirement.
Swift
protocol Shape {
var area: Double { [1] }
func [2]()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' for area which is usually read-only.
Choosing method names unrelated to shape actions.
✗ Incorrect
The property 'area' is read-only (get), and the method 'draw' is required.
5fill in blank
hardFill all three blanks to declare a protocol with a gettable property, a settable property, and a method.
Swift
protocol Device {
var model: String { [1] }
var isOn: Bool { [2] }
func [3]()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' alone for properties which is invalid.
Choosing method names unrelated to device actions.
✗ Incorrect
The 'model' property is read-only (get), 'isOn' is read-write (get set), and the method is 'turnOn'.