Learn the key concepts of Generics in Swift with these 30 MCQs. Dive into generic functions, types, type constraints, and associated types in protocols to master Swift’s powerful generics system.
1. Introduction to Generics
What is the primary purpose of generics in Swift?
A) To allow writing code that works with specific types
B) To create reusable and type-safe code
C) To reduce the code size
D) To enable polymorphism
Which of the following is a benefit of using generics in Swift?
A) It increases the number of types used in the code
B) It improves code readability and reusability
C) It allows inheritance between classes
D) It simplifies the user interface
What does T typically represent in a generic declaration in Swift?
A) A specific type
B) A placeholder for a type
C) A protocol
D) A method signature
Can generics be used with structures, classes, and enums in Swift?
A) Yes
B) No
C) Only with classes
D) Only with structures
Which of these is an example of a generic type in Swift?
A) String
B) Array<T>
C) Int
D) Any
2. Generic Functions and Types
What is the purpose of a generic function in Swift?
A) To limit the function to only one data type
B) To allow the function to accept different types of inputs
C) To define constant values for a function
D) To enforce the function’s execution order
Which of the following syntax correctly defines a generic function in Swift?
A) func genericFunction<T>(param: T)
B) generic func <T> (param: T)
C) func genericFunction(param: T)
D) func <T> genericFunction(param: T)
What does the where clause in a generic function allow you to do?
A) Restrict the input type based on its conditions
B) Specify the return type
C) Declare a default type for the generic
D) Make the function return multiple types
How can you constrain a generic type to a specific protocol in Swift?
A) By using as keyword
B) By using a type constraint with where clause
C) By specifying the protocol in angle brackets
D) By using a type alias
Which of these is an example of a generic type alias in Swift?
A) typealias ArrayType<T> = [T]
B) typealias StringType = String
C) typealias IntType<T> = Int
D) typealias TArray = Array
3. Type Constraints
What does a type constraint do in Swift?
A) It restricts the type of a parameter in a function
B) It defines a generic type without any restrictions
C) It creates a default type for a function
D) It allows automatic type inference
How would you constrain a generic type T to only accept types that conform to a protocol?
A) func function<T: Protocol>(param: T)
B) func function<T: Protocol>(param: T) -> T
C) func function<Protocol>(param: T)
D) func function<T<Protocol>>(param: T)
Which of the following is an example of a generic function with a type constraint?
A) func swap<T>(a: T, b: T)
B) func swap<T: Equatable>(a: T, b: T)
C) func swap<T>(a: T, b: Int)
D) func swap<T: Int>(a: T, b: T)
Can a type constraint be applied to multiple types in Swift?
A) Yes
B) No
How does the T: Comparable type constraint affect a generic function in Swift?
A) It allows the function to compare T values
B) It restricts T to be a class
C) It limits the number of parameters
D) It allows T to be a tuple
4. Associated Types in Protocols
What is an associated type in a protocol in Swift?
A) A placeholder type defined within a protocol
B) A variable type within a struct
C) A constant defined within a class
D) A type that must be initialized inside a method
How is an associated type declared within a protocol?
A) associatedtype T
B) typealias T
C) type T
D) protocol T
What does an associated type allow a protocol to do?
A) Define a placeholder for a concrete type
B) Use multiple protocols in one type
C) Define methods inside a protocol
D) Restrict the functionality of a type
Which of these protocols defines an associated type?
A) Comparable
B) Equatable
C) Collection
D) Hashable
How can you use an associated type in a protocol method?
A) By specifying the type directly in the method
B) By using the associatedtype keyword
C) By declaring the associated type in the method signature
D) By assigning it to a constant
5. More on Generics
What happens if you don’t provide a specific type when using a generic in Swift?
A) The compiler infers the type automatically
B) A compilation error occurs
C) A default value is assigned
D) The function cannot be used
How would you define a generic array in Swift?
A) var arr: Array<T>
B) var arr: [T]
C) var arr: T[]
D) var arr: Array[]
Which of the following allows a generic type to work with both structs and classes in Swift?
A) Type constraints
B) Associated types
C) Generics cannot be used with both
D) Typealias
Which keyword is used to declare a generic parameter in a class?
A) typealias
B) generic
C) class
D) class <T>
What is the purpose of the Self keyword in Swift generics?
A) To refer to the type of the current instance
B) To refer to the protocol
C) To refer to the superclass type
D) To refer to the associated type
How can you create a generic stack class in Swift?
A) class Stack<T> {}
B) class Stack {}
C) class Stack<T: Any> {}
D) generic class Stack<T> {}
Can you define a generic struct inside another generic struct in Swift?
A) Yes
B) No
Which of these is a correct use of a type constraint in Swift?
A) func sort<T: Comparable>(items: [T])
B) func sort<T>(items: [T: Comparable])
C) func sort<T: Any>(items: [T])
D) func sort<T: Comparable>(items: [T, T])
Can an associated type be constrained in Swift?
A) Yes
B) No
What would the following generic method definition do: func swapValues<T>(_ a: T, _ b: T)?
A) Swap two values of any type
B) Only swap values of the same type
C) Only swap integer values
D) Swap values of two different types
Answer Key
Qno
Answer (Option with Text)
1
B) To allow writing code that works with specific types
2
B) It improves code readability and reusability
3
B) A placeholder for a type
4
A) Yes
5
B) Array<T>
6
B) To allow the function to accept different types of inputs
7
A) func genericFunction<T>(param: T)
8
A) Restrict the input type based on its conditions
9
B) func swap<T: Equatable>(a: T, b: T)
10
A) typealias ArrayType<T> = [T]
11
A) It restricts the type of a parameter in a function
12
A) func function<T: Protocol>(param: T)
13
B) func swap<T: Equatable>(a: T, b: T)
14
A) Yes
15
A) It allows the function to compare T values
16
A) A placeholder type defined within a protocol
17
A) associatedtype T
18
A) Define a placeholder for a concrete type
19
C) Collection
20
C) By declaring the associated type in the method signature