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
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
Which keyword is used to declare an extension? a) extend b) extension c) add d) append
Can extensions add stored properties to a type? a) Yes, always b) No, never c) Only with a custom initializer d) Only with protocols
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
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
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
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
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
Can you use extensions to add subscripts to a type? a) Yes b) No c) Only for classes d) Only for structs
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
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
Can a protocol extension include stored properties? a) Yes b) No c) Only with default initializers d) Only if declared final
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
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
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
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
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
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
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
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
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
Which of the following can contain nested types? a) Classes b) Structures c) Enumerations d) All of the above
How can you access a nested type? a) ParentType.NestedType b) NestedType.ParentType c) Using typealias d) Using self
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
What access modifier is used to limit visibility to the same file? a) private b) fileprivate c) protected d) internal
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
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
Can you define private nested types in Swift? a) Yes b) No c) Only with classes d) Only with enumerations
What is the default access level in Swift? a) public b) internal c) private d) fileprivate
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
Qno
Answer (Option with the text)
1
b) To add new functionality to an existing type
2
b) extension
3
b) No, never
4
c) Both a and b
5
d) All of the above
6
b) Adds a new method square to the Int type
7
a) Yes, always
8
b) The program throws an error
9
a) Yes
10
c) Designated initializer
11
b) To add default behavior to all conforming types
12
b) No
13
a) By defining the method in a protocol extension
14
d) Both a and b
15
b) The conforming type’s implementation overrides the default implementation
16
b) No, never
17
a) Outputs “Hello!”
18
a) Yes, using constraints
19
a) Using where Self: ClassName
20
a) Reusing common functionality across conforming types