0
0
Swiftprogramming~10 mins

Protocol requirements (methods and properties) 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 with a required method.

Swift
protocol Vehicle {
    func [1]()
}
Drag options to blanks, or click blank then click option'
Adrive
Brun
Cmove
DstartEngine
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that are not descriptive of vehicle actions.
Forgetting to include parentheses for method declaration.
2fill in blank
medium

Complete 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'
Aget
Bset
Cget set
Dset get
Attempts:
3 left
💡 Hint
Common Mistakes
Using only 'get' which makes the property read-only.
Using 'set' alone which is invalid syntax.
3fill in blank
hard

Fix 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'
ABool
BInt
CString
DVoid
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.
4fill in blank
hard

Fill 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'
Aget
Bset
Cdraw
Dcalculate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' for area which is usually read-only.
Choosing method names unrelated to shape actions.
5fill in blank
hard

Fill 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'
Aget
Bget set
CturnOn
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' alone for properties which is invalid.
Choosing method names unrelated to device actions.