It is a design approach that uses protocols to define blueprints of methods and properties. Types then conform to these protocols to share behavior, promoting code reuse and flexibility.
Click to reveal answer
intermediate
How does protocol-oriented architecture differ from class inheritance?
Instead of inheriting from a base class, types conform to protocols. This avoids tight coupling and allows multiple behaviors to be combined, unlike single inheritance.
Click to reveal answer
intermediate
What is a protocol extension in Swift?
A protocol extension adds default method or property implementations to a protocol, so conforming types get this behavior automatically unless they override it.
Click to reveal answer
intermediate
Why is protocol-oriented architecture beneficial for testing?
Protocols allow you to create mock or stub types easily by conforming to the protocol, enabling isolated and flexible unit testing.
Click to reveal answer
beginner
Give an example of a simple protocol in Swift.
Example:
protocol Drivable {
func drive()
}
Any type that conforms to Drivable must implement the drive() method.
Click to reveal answer
What keyword defines a protocol in Swift?
Astruct
Binterface
Cclass
Dprotocol
✗ Incorrect
Swift uses the keyword protocol to define protocols.
Which of these is a benefit of protocol-oriented architecture?
APrevents code reuse
BForces all types to inherit from a base class
CAllows multiple inheritance of behavior
DRequires use of classes only
✗ Incorrect
Protocols let types adopt multiple behaviors, similar to multiple inheritance.
What feature lets you provide default implementations in protocols?
C. Method signature in Athlete does not match protocol requirement
D. Missing return type in run() method
Solution
Step 1: Compare protocol and struct method signatures
The protocol requires func run() with no parameters, but Athlete implements func run(distance: Int) which has a parameter.
Step 2: Understand conformance rules
Method signatures must match exactly for conformance; differing parameters cause a mismatch error.
Final Answer:
Method signature in Athlete does not match protocol requirement -> Option C
Quick Check:
Method signatures must match exactly [OK]
Hint: Method signatures must match protocol exactly [OK]
Common Mistakes:
Adding parameters to protocol methods in implementation
Thinking structs can't conform to protocols
Ignoring method signature differences
5. You want to create a flexible system where different types can be "playable" with a play() method, and some can also be "recordable" with a record() method. Which protocol-oriented approach below best supports this design?
hard
A. Define separate protocols Playable and Recordable, then have types conform to one or both as needed
B. Create a single protocol with both play() and record() methods, and require all types to implement both
C. Use a base class with play() and record() methods and inherit from it
D. Implement play() and record() as global functions
Solution
Step 1: Understand protocol composition
Defining separate protocols allows types to conform only to behaviors they support, making the design flexible and reusable.
Step 2: Evaluate other options
Single protocol forces all types to implement all methods, base class limits flexibility, and global functions do not enforce behavior contracts.
Final Answer:
Define separate protocols Playable and Recordable, then have types conform to one or both as needed -> Option A
Quick Check:
Separate protocols = flexible conformance [OK]
Hint: Use multiple protocols for flexible behavior [OK]
Common Mistakes:
Forcing all types to implement all methods
Using inheritance instead of protocols
Not leveraging protocol composition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.