MCQs on Advanced Protocols | Swift

Explore Advanced Protocols in Swift, including Protocol-Oriented Programming, using protocols for dependency injection, understanding Self and associated types, and combining protocols with extensions to write cleaner, more reusable code.


MCQs on Advanced Protocols in Swift

Section 1: Protocol-Oriented Programming (6 Questions)

  1. What is the primary concept of Protocol-Oriented Programming (POP) in Swift?
    • a) Focusing on classes and inheritance
    • b) Defining protocols and ensuring types conform to them
    • c) Creating objects using abstract classes
    • d) Managing memory with automatic reference counting
  2. Which of the following is a key benefit of Protocol-Oriented Programming?
    • a) It enables code reuse through class inheritance
    • b) It allows for high flexibility by defining requirements in protocols
    • c) It promotes dependency on concrete classes
    • d) It reduces the use of generics in Swift
  3. What can protocols define in Protocol-Oriented Programming?
    • a) Methods and properties
    • b) Only properties
    • c) Only methods
    • d) Only computed properties
  4. In Protocol-Oriented Programming, can protocols have default method implementations?
    • a) Yes, using protocol extensions
    • b) No, protocols only declare methods without implementation
    • c) Yes, but only for properties
    • d) No, methods cannot be added to protocols
  5. What is a protocol extension used for in POP?
    • a) To provide default implementations for protocol methods
    • b) To allow protocols to inherit from classes
    • c) To restrict protocol conformance
    • d) To define protocol properties
  6. How does Protocol-Oriented Programming differ from Object-Oriented Programming (OOP)?
    • a) POP relies on classes for inheritance, while OOP uses protocols
    • b) POP emphasizes composition over inheritance, whereas OOP emphasizes inheritance
    • c) POP uses more concrete classes than OOP
    • d) There is no difference between POP and OOP

Section 2: Using Protocols for Dependency Injection (6 Questions)

  1. What is Dependency Injection in the context of Swift protocols?
    • a) Passing dependencies to a class or struct at runtime
    • b) Storing data inside a class or struct
    • c) Creating a global shared instance for all components
    • d) Automatically creating instances of dependencies
  2. How can protocols facilitate Dependency Injection in Swift?
    • a) By declaring interfaces that the dependent objects can conform to
    • b) By creating global objects that store dependencies
    • c) By making the dependencies static
    • d) By manually injecting dependencies at compile time
  3. In dependency injection, what role do protocols play in making code more testable?
    • a) They allow for easy mocking and stubbing of dependencies during testing
    • b) They make dependencies static and unchangeable
    • c) They allow automatic injection of test data
    • d) They remove the need for unit testing
  4. Which of the following is an example of constructor injection using a protocol?
  • a) init(service: ServiceProtocol)
  • b) service.method()
  • c) service = ServiceProtocol()
  • d) var service: ServiceProtocol!
  1. How does protocol-based dependency injection improve decoupling of components?
  • a) It binds components to specific implementations
  • b) It ensures that all dependencies are tightly coupled
  • c) It allows components to work independently by using abstract protocol definitions
  • d) It makes code monolithic
  1. Which of the following describes protocol-oriented dependency injection in Swift?
  • a) Injecting concrete instances through protocols
  • b) Using protocols to define the behavior required by dependencies
  • c) Defining hardcoded dependencies within classes
  • d) Avoiding the use of protocols

Section 3: Self and Associated Types (6 Questions)

  1. What does the Self keyword refer to in a protocol?
  • a) A reference to the class that adopts the protocol
  • b) A type-safe reference to the protocol itself
  • c) A generic placeholder for any type
  • d) The current instance of the protocol
  1. In a protocol, what is an associated type?
  • a) A placeholder for a concrete type that conforms to the protocol
  • b) A reference to a property in a class
  • c) A variable for storing a type’s memory address
  • d) A special type of constant in a protocol
  1. How does an associated type in a protocol differ from a generic type?
  • a) Associated types are resolved at compile time, while generics are resolved at runtime
  • b) Generics define type constraints, while associated types do not
  • c) Associated types are placeholders for specific types, while generics are always specific to types
  • d) Associated types are used for type inference within the protocol, while generics are used in method declarations
  1. Which of the following is a valid use of associated types in a protocol?
  • a) associatedtype T
  • b) associatedtype T: Comparable
  • c) associatedtype T = String
  • d) All of the above
  1. What is the purpose of Self in a protocol method declaration?
  • a) It refers to the type that conforms to the protocol
  • b) It refers to the current instance of a class or struct
  • c) It refers to the super class of the type
  • d) It refers to a static type, not an instance
  1. Can Self in a protocol be used to refer to the conforming type?
  • a) Yes, when the protocol defines an instance method
  • b) No, Self only refers to instance variables
  • c) Yes, but only in protocols that include associated types
  • d) No, Self only refers to the superclass of the type

Section 4: Combining Protocols with Extensions (6 Questions)

  1. What is the benefit of extending protocols in Swift?
  • a) It allows the protocol to adopt default method implementations
  • b) It makes the protocol more complex
  • c) It allows for changes to protocol inheritance
  • d) It makes the protocol non-optional
  1. Can protocol extensions define default implementations for required methods?
  • a) Yes, they can provide default implementations
  • b) No, extensions can only add properties
  • c) Yes, but only for properties
  • d) No, extensions cannot change protocols
  1. How can you use protocol extensions to provide default functionality?
  • a) By defining methods inside the protocol extension
  • b) By adding static methods to the protocol
  • c) By making all methods optional
  • d) By requiring conformance to other protocols
  1. What is the main advantage of combining protocol extensions with protocols?
  • a) You can add functionality to existing protocols without changing the original protocol
  • b) It allows you to make all methods in a protocol mandatory
  • c) It creates a new version of the protocol with different methods
  • d) It removes the need for any method implementations
  1. Which of the following is a valid way to extend a protocol?
  • a) extension MyProtocol { func doSomething() { print("Doing something") } }
  • b) protocol MyProtocol { func doSomething() { print("Doing something") } }
  • c) func MyProtocol.doSomething() { print("Doing something") }
  • d) protocol MyProtocol: extension { func doSomething() }
  1. Can you extend a protocol with an associated type?
  • a) Yes, by providing default implementations for the associated type
  • b) No, associated types cannot be extended
  • c) Yes, but only for static properties
  • d) No, extensions can only add methods
  1. How can protocol extensions improve code reuse in Swift?
  • a) By adding default implementations for methods required by the protocol
  • b) By allowing protocols to inherit from other protocols
  • c) By making every protocol method abstract
  • d) By allowing multiple inheritance
  1. Which of the following is true about combining protocols with extensions in Swift?
  • a) You can add methods and properties to protocols without changing their original definition
  • b) You can only add methods, not properties, to protocols
  • c) Extensions can change existing protocol methods
  • d) Extensions can remove existing methods in a protocol
  1. How can protocol extensions be used to avoid code duplication?
  • a) By providing common implementations for protocol methods shared by multiple types
  • b) By allowing a protocol to inherit from multiple protocols
  • c) By enforcing different behavior for different types
  • d) By automatically deallocating memory
  1. Which of the following is the result of applying a protocol extension?
  • a) Methods in the protocol can now have default implementations
  • b) The protocol becomes immutable
  • c) The protocol can inherit from other protocols
  • d) All methods become static
  1. What happens when a type conforms to a protocol that has an extension with a default method implementation?
  • a) The type automatically adopts the default method
  • b) The type must implement the method explicitly
  • c) The type cannot conform to the protocol
  • d) The type can implement the method but is not required to
  1. Can protocol extensions be used to add functionality to existing types?
  • a) Yes, they allow you to add methods or properties to any type that conforms to the protocol
  • b) No, only new types can be extended with protocol extensions
  • c) Yes, but only for concrete types like classes
  • d) No, protocol extensions cannot modify existing types

Answers Table

QnoAnswer (Option with the text)
1b) Defining protocols and ensuring types conform to them
2b) It allows for high flexibility by defining requirements in protocols
3a) Methods and properties
4a) Yes, using protocol extensions
5a) To provide default implementations for protocol methods
6b) POP emphasizes composition over inheritance, whereas OOP emphasizes inheritance
7a) Passing dependencies to a class or struct at runtime
8a) By declaring interfaces that the dependent objects can conform to
9a) They allow for easy mocking and stubbing of dependencies during testing
10a) init(service: ServiceProtocol)
11c) It allows components to work independently by using abstract protocol definitions
12b) Using protocols to define the behavior required by dependencies
13a) A reference to the class that adopts the protocol
14a) A placeholder for a concrete type that conforms to the protocol
15a) Associated types are resolved at compile time, while generics are resolved at runtime
16d) All of the above
17a) It refers to the type that conforms to the protocol
18a) Yes, when the protocol defines an instance method
19a) It allows the protocol to adopt default method implementations
20a) Yes, they can provide default implementations
21a) By defining methods inside the protocol extension
22a) You can add functionality to existing protocols without changing the original protocol
23a) extension MyProtocol { func doSomething() { print("Doing something") } }
24a) Yes, by providing default implementations for the associated type
25a) By providing common implementations for protocol methods shared by multiple types
26a) You can add methods and properties to protocols without changing their original definition
27a) By providing common implementations for protocol methods shared by multiple types
28a) Methods in the protocol can now have default implementations
29a) The type automatically adopts the default method
30a) Yes, they allow you to add methods or properties to any type that conforms to the protocol

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