Master Swift’s powerful protocol-oriented programming. This guide covers defining protocols, inheritance, composition, and delegation. Test your knowledge with 30 engaging MCQs designed to reinforce your understanding of these advanced concepts.
Defining and Conforming to Protocols (Questions 1-10)
What is the keyword used to define a protocol in Swift? a) interface b) protocol c) delegate d) abstract
Which statement about protocols is true? a) Protocols can contain stored properties. b) Protocols can only define methods. c) Protocols can define methods and properties but do not implement them. d) Protocols automatically provide default implementations.
Can protocols define initializer requirements? a) Yes b) No
What is the correct syntax for a class conforming to a protocol? a) class MyClass : MyProtocol {} b) class MyClass = MyProtocol {} c) class MyClass implements MyProtocol {} d) class MyClass conforms MyProtocol {}
Which keyword is used to specify a property in a protocol that can be both read and written? a) getset b) readwrite c) get, set d) readwriteable
Can a struct conform to a protocol in Swift? a) Yes b) No
Which of the following must a type conforming to a protocol implement? a) Only methods b) Only properties c) All requirements defined in the protocol d) None of the above
What happens if a type does not implement all protocol requirements? a) Compile-time error b) Runtime error c) A warning is shown d) Protocol is ignored
Can a protocol define a method with a default implementation? a) Yes, using protocol extensions b) No, protocols cannot implement methods
What is the purpose of the @objc attribute with protocols? a) To allow protocols to be used in Objective-C b) To prevent protocols from being inherited c) To restrict protocols to classes only d) To enable protocol extensions
Protocol Inheritance (Questions 11-16)
Can a protocol inherit from another protocol? a) Yes b) No
What is the correct syntax for protocol inheritance? a) protocol Child : Parent {} b) protocol Child = Parent {} c) protocol Child inherits Parent {} d) protocol Child Parent {}
What happens if a protocol inherits multiple protocols? a) The child protocol inherits all requirements of the parent protocols. b) The child protocol only inherits methods. c) Only the first protocol is inherited. d) Inheritance is not allowed in such cases.
Which of the following is valid multiple protocol inheritance? a) protocol A : B, C {} b) protocol A = B + C {} c) protocol A inherits (B, C) d) protocol A & B, C {}
Can a class conform to a protocol and inherit from another class simultaneously? a) Yes b) No
What happens if a class conforms to multiple protocols with conflicting requirements? a) Compiler error b) Implements the last defined requirement c) Overrides the conflicting requirements d) Swift handles conflicts automatically
Protocol Composition (Questions 17-22)
What is protocol composition in Swift? a) Combining multiple protocols into a single type requirement b) Inheriting multiple protocols c) Creating a subclass of a protocol d) Combining protocols with classes
How is protocol composition represented in Swift? a) Protocol1 + Protocol2 b) Protocol1, Protocol2 c) Protocol1 & Protocol2 d) Protocol1 | Protocol2
What is a key benefit of protocol composition? a) Provides a default implementation b) Reduces type restrictions c) Combines functionality without inheritance d) Enables automatic conformance
Which of the following syntax is valid for protocol composition in function parameters? a) func myFunc(param: Protocol1 & Protocol2) b) func myFunc(param: Protocol1 + Protocol2) c) func myFunc(param: Protocol1, Protocol2) d) func myFunc(param: Protocol1 | Protocol2)
Can protocol composition include a concrete type? a) Yes b) No
What does the following represent?swiftCopy codetypealias Composite = ProtocolA & ProtocolB a) Inheritance of protocols b) Composition of ProtocolA and ProtocolB c) Alias for a class conforming to multiple protocols d) An invalid syntax
Delegation Design Pattern (Questions 23-30)
What is the delegation design pattern in Swift? a) Passing responsibilities between objects through protocols b) Direct inheritance of class functionality c) Combining multiple classes into a single object d) Using global variables for shared state
What is the main component of delegation in Swift? a) A protocol b) A class inheritance chain c) A closure d) A concrete subclass
In delegation, what is typically assigned as the delegate? a) A subclass b) A protocol c) An object conforming to the protocol d) A singleton
What keyword is used to define a weak reference to a delegate? a) weak b) delegate c) lazy d) optional
Why are delegate properties often marked as weak? a) To avoid retain cycles b) To make them optional c) To increase performance d) To ensure type safety
Can the delegation pattern be used without protocols? a) Yes, but it’s not recommended b) No
What does the following code do?swiftCopy codeprotocol DelegateProtocol { func performAction() } class Delegator { var delegate: DelegateProtocol? } a) Creates a protocol for delegation b) Creates a strong delegate reference c) Allows protocol inheritance d) Creates a required delegate
Which of the following is a common use case for delegation in iOS development? a) Networking callbacks b) User interaction handling c) Custom view updates d) All of the above
Answer Key
QNo
Answer (Option with Text)
1
b) protocol
2
c) Protocols can define methods and properties but do not implement them
3
a) Yes
4
a) class MyClass : MyProtocol {}
5
c) get, set
6
a) Yes
7
c) All requirements defined in the protocol
8
a) Compile-time error
9
a) Yes, using protocol extensions
10
a) To allow protocols to be used in Objective-C
11
a) Yes
12
a) protocol Child : Parent {}
13
a) The child protocol inherits all requirements of the parent protocols
14
a) protocol A : B, C {}
15
a) Yes
16
a) Compiler error
17
a) Combining multiple protocols into a single type requirement
18
c) Protocol1 & Protocol2
19
c) Combines functionality without inheritance
20
a) func myFunc(param: Protocol1 & Protocol2)
21
a) Yes
22
b) Composition of ProtocolA and ProtocolB
23
a) Passing responsibilities between objects through protocols