MCQs on Extensions and Modifications | Swift

Swift extensions empower developers to enhance existing types, utilize protocol extensions, and manage nested types. These MCQs comprehensively cover adding functionality, protocol extensions, and access modifiers.


Topic 1: Adding Functionality with Extensions

  1. What is the primary purpose of extensions in Swift?
    a) To modify the existing implementation of a type
    b) To add new functionality to an existing type
    c) To define custom access modifiers
    d) To override inherited methods
  2. Which keyword is used to declare an extension?
    a) extend
    b) extension
    c) add
    d) append
  3. Can extensions add stored properties to a type?
    a) Yes, always
    b) No, never
    c) Only with a custom initializer
    d) Only with protocols
  4. How can you add a computed property to a struct using an extension?
    a) By using get and set methods
    b) By declaring the property inside the extension
    c) Both a and b
    d) None of the above
  5. Which of the following is valid for adding functionality with extensions?
    a) Adding new initializers
    b) Adding new methods
    c) Adding computed properties
    d) All of the above
  6. What does the following code do?swiftCopy codeextension Int { func square() -> Int { return self * self } } a) Modifies the Int type
    b) Adds a new method square to the Int type
    c) Overrides the default behavior of Int
    d) None of the above
  7. Can extensions conform to protocols?
    a) Yes, always
    b) No, never
    c) Only if the type is a class
    d) Only if the protocol has default methods
  8. What happens if you define a method in an extension that already exists in the original type?
    a) The extension method overrides the original method
    b) The program throws an error
    c) The extension method is ignored
    d) It causes undefined behavior
  9. Can you use extensions to add subscripts to a type?
    a) Yes
    b) No
    c) Only for classes
    d) Only for structs
  10. Which type of initializer cannot be added using extensions?
    a) Convenience initializer
    b) Required initializer
    c) Designated initializer
    d) Failable initializer

Topic 2: Protocol Extensions

  1. What is the primary purpose of protocol extensions in Swift?
    a) To extend a single type
    b) To add default behavior to all conforming types
    c) To override protocol requirements
    d) To modify access levels
  2. Can a protocol extension include stored properties?
    a) Yes
    b) No
    c) Only with default initializers
    d) Only if declared final
  3. How can you add a default implementation to a protocol method?
    a) By defining the method in a protocol extension
    b) By overriding the protocol in the conforming type
    c) By using stored properties
    d) By declaring the protocol as final
  4. Which of the following is true about protocol extensions?
    a) They can define new methods
    b) They can provide default implementations for existing methods
    c) They can modify the original protocol
    d) Both a and b
  5. What happens if a conforming type implements a method that has a default implementation in a protocol extension?
    a) The default implementation is used
    b) The conforming type’s implementation overrides the default implementation
    c) Both implementations are used
    d) A runtime error occurs
  6. Can protocol extensions add new requirements to a protocol?
    a) Yes, always
    b) No, never
    c) Only if the conforming types explicitly adopt them
    d) Only in class types
  7. What does this code do?s
    protocol Greetable { func greet() } extension Greetable { func greet() { print("Hello!") } } struct Person: Greetable {} let person = Person() person.greet() a) Outputs “Hello!”
    b) Causes a compile-time error
    c) Requires a custom implementation in Person
    d) Does nothing
  8. Can protocol extensions restrict default implementation to specific conforming types?
    a) Yes, using constraints
    b) No, default implementation applies to all
    c) Only for classes
    d) Only with generic types
  9. How do you constrain a protocol extension to apply only to classes?
    a) Using where Self: ClassName
    b) Using where Self is ClassName
    c) Using if Self: ClassName
    d) Using guard ClassName
  10. What is a common use case for protocol extensions?
    a) Reusing common functionality across conforming types
    b) Adding static methods
    c) Modifying protocol inheritance
    d) Restricting type usage

Topic 3: Nested Types and Access Modifiers

  1. What are nested types in Swift?
    a) Types defined within a module
    b) Types defined inside other types
    c) Types with restricted access
    d) None of the above
  2. Which of the following can contain nested types?
    a) Classes
    b) Structures
    c) Enumerations
    d) All of the above
  3. How can you access a nested type?
    a) ParentType.NestedType
    b) NestedType.ParentType
    c) Using typealias
    d) Using self
  4. Can nested types have their own properties and methods?
    a) Yes, always
    b) No, never
    c) Only if they are classes
    d) Only if they are protocols
  5. What access modifier is used to limit visibility to the same file?
    a) private
    b) fileprivate
    c) protected
    d) internal
  6. What does the public access level mean?
    a) The entity is accessible within its module
    b) The entity is accessible globally
    c) The entity is accessible only to subclasses
    d) None of the above
  7. How is open different from public in Swift?
    a) open allows inheritance outside the module
    b) open restricts access to the module
    c) open applies only to protocols
    d) There is no difference
  8. Can you define private nested types in Swift?
    a) Yes
    b) No
    c) Only with classes
    d) Only with enumerations
  9. What is the default access level in Swift?
    a) public
    b) internal
    c) private
    d) fileprivate
  10. Why are nested types useful?
    a) To organize code logically
    b) To improve encapsulation
    c) To provide context-specific functionality
    d) All of the above

Answers Table

QnoAnswer (Option with the text)
1b) To add new functionality to an existing type
2b) extension
3b) No, never
4c) Both a and b
5d) All of the above
6b) Adds a new method square to the Int type
7a) Yes, always
8b) The program throws an error
9a) Yes
10c) Designated initializer
11b) To add default behavior to all conforming types
12b) No
13a) By defining the method in a protocol extension
14d) Both a and b
15b) The conforming type’s implementation overrides the default implementation
16b) No, never
17a) Outputs “Hello!”
18a) Yes, using constraints
19a) Using where Self: ClassName
20a) Reusing common functionality across conforming types
21b) Types defined inside other types
22d) All of the above
23a) ParentType.NestedType
24a) Yes, always
25b) fileprivate
26b) The entity is accessible globally

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