MCQs on Protocols and Delegation | Swift

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)

  1. What is the keyword used to define a protocol in Swift?
    a) interface
    b) protocol
    c) delegate
    d) abstract
  2. 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.
  3. Can protocols define initializer requirements?
    a) Yes
    b) No
  4. 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 {}
  5. 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
  6. Can a struct conform to a protocol in Swift?
    a) Yes
    b) No
  7. 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
  8. 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
  9. Can a protocol define a method with a default implementation?
    a) Yes, using protocol extensions
    b) No, protocols cannot implement methods
  10. 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)

  1. Can a protocol inherit from another protocol?
    a) Yes
    b) No
  2. 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 {}
  3. 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.
  4. 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 {}
  5. Can a class conform to a protocol and inherit from another class simultaneously?
    a) Yes
    b) No
  6. 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)

  1. 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
  2. How is protocol composition represented in Swift?
    a) Protocol1 + Protocol2
    b) Protocol1, Protocol2
    c) Protocol1 & Protocol2
    d) Protocol1 | Protocol2
  3. 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
  4. 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)
  5. Can protocol composition include a concrete type?
    a) Yes
    b) No
  6. 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)

  1. 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
  2. What is the main component of delegation in Swift?
    a) A protocol
    b) A class inheritance chain
    c) A closure
    d) A concrete subclass
  3. 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
  4. What keyword is used to define a weak reference to a delegate?
    a) weak
    b) delegate
    c) lazy
    d) optional
  5. 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
  6. Can the delegation pattern be used without protocols?
    a) Yes, but it’s not recommended
    b) No
  7. 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
  8. 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

QNoAnswer (Option with Text)
1b) protocol
2c) Protocols can define methods and properties but do not implement them
3a) Yes
4a) class MyClass : MyProtocol {}
5c) get, set
6a) Yes
7c) All requirements defined in the protocol
8a) Compile-time error
9a) Yes, using protocol extensions
10a) To allow protocols to be used in Objective-C
11a) Yes
12a) protocol Child : Parent {}
13a) The child protocol inherits all requirements of the parent protocols
14a) protocol A : B, C {}
15a) Yes
16a) Compiler error
17a) Combining multiple protocols into a single type requirement
18c) Protocol1 & Protocol2
19c) Combines functionality without inheritance
20a) func myFunc(param: Protocol1 & Protocol2)
21a) Yes
22b) Composition of ProtocolA and ProtocolB
23a) Passing responsibilities between objects through protocols
24a) A protocol
25c) An object conforming to the protocol
26a) weak
27a) To avoid retain cycles
28a) Yes, but it’s not recommended
29a) Creates a protocol for delegation
30d) All of the above

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top