A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. Classes, structs, or enums can adopt protocols to provide actual implementations.
Click to reveal answer
beginner
How do you declare a protocol in Swift?
Use the protocol keyword followed by the protocol name and curly braces. Inside, list the required methods or properties without implementations.
Click to reveal answer
intermediate
Can a Swift protocol require properties? If yes, what kind?
Yes, protocols can require properties. They can specify whether the property must be gettable or gettable and settable, but they do not provide storage or implementation.
Click to reveal answer
beginner
What does it mean for a type to conform to a protocol?
It means the type agrees to implement all the requirements defined in the protocol, such as methods and properties, ensuring it meets the protocol's contract.
Click to reveal answer
intermediate
Can protocols be used as types in Swift? Give an example.
Yes, protocols can be used as types to define variables, parameters, or return types. For example, a variable of type MyProtocol can hold any instance of a type that conforms to MyProtocol.
Click to reveal answer
Which keyword is used to define a protocol in Swift?
Aclass
Bprotocol
Cinterface
Dstruct
✗ Incorrect
Swift uses the keyword protocol to define protocols.
What must a type do to conform to a protocol?
AImplement all required methods and properties
BInherit from the protocol
COverride the protocol
DDeclare the protocol as a variable
✗ Incorrect
Conforming means implementing all the protocol's required methods and properties.
Can a protocol specify property requirements without providing storage?
ANo, it must provide storage
BOnly for classes
CYes, it only specifies get/set requirements
DOnly for structs
✗ Incorrect
Protocols specify whether properties must be gettable or settable but do not provide storage.
Which of these can adopt a protocol in Swift?
AClass, struct, and enum
BOnly classes
COnly structs
DOnly enums
✗ Incorrect
Classes, structs, and enums can all adopt protocols.
Can you use a protocol as a type for a variable?
AOnly inside functions
BNo, protocols are not types
COnly if the protocol is a class
DYes, to hold any conforming instance
✗ Incorrect
Protocols can be used as types to hold any instance that conforms to them.
Explain what a protocol is in Swift and why it is useful.
Think about how protocols help different types agree on a set of rules.
You got /4 concepts.
Describe how a type conforms to a protocol and what happens if it does not implement all requirements.
Consider what the compiler expects when you say a type adopts a protocol.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a protocol in Swift?
easy
A. To create a new class with default behavior
B. To define a blueprint of methods and properties that a type must implement
C. To store data persistently on the device
D. To handle user interface layout automatically
Solution
Step 1: Understand what a protocol defines
A protocol specifies methods and properties that a type must implement, acting as a blueprint.
Step 2: Differentiate from other concepts
Protocols do not create classes or store data; they only define requirements.
Final Answer:
To define a blueprint of methods and properties that a type must implement -> Option B
Quick Check:
Protocol purpose = blueprint [OK]
Hint: Protocols define required methods and properties [OK]
Common Mistakes:
Confusing protocols with classes
Thinking protocols store data
Assuming protocols handle UI layout
2. Which of the following is the correct syntax to declare a protocol named Drivable in Swift?
C. Error: Athlete does not implement required method run()
D. Error: Protocol Runnable cannot be adopted by struct
Solution
Step 1: Check protocol requirements
The protocol Runnable requires a method run() with no return value.
Step 2: Verify struct conformance
The struct Athlete adopts Runnable but does not implement run(), causing a compile error.
Final Answer:
Error: Athlete does not implement required method run() -> Option C
Quick Check:
Missing required method causes error [OK]
Hint: All protocol methods must be implemented [OK]
Common Mistakes:
Assuming structs can't adopt protocols
Thinking missing method is allowed
Expecting run() to require return type
5. You want to create a protocol Vehicle with a property speed that can be read and set. Which of these protocol declarations is correct?
hard
A. protocol Vehicle { var speed: Int { get set } }
B. protocol Vehicle { var speed: Int }
C. protocol Vehicle { func speed() -> Int }
D. protocol Vehicle { let speed: Int }
Solution
Step 1: Understand property requirements in protocols
To require a property to be readable and writable, use { get set } in the protocol.
Step 2: Analyze each option
protocol Vehicle { var speed: Int { get set } } correctly declares speed as a variable property with get set. protocol Vehicle { var speed: Int } misses get set, so it's read-only. protocol Vehicle { func speed() -> Int } declares a method, not a property. protocol Vehicle { let speed: Int } uses let, which is not allowed in protocols.
Final Answer:
protocol Vehicle { var speed: Int { get set } } -> Option A
Quick Check:
Writable property = var with get set [OK]
Hint: Use 'var' with { get set } for read-write properties [OK]
Common Mistakes:
Using 'let' in protocol properties
Omitting { get set } for writable properties
Confusing methods with properties
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.