Master Kotlin’s generics, including generic classes and functions, type variance (in, out), and reified types. These 30 questions will test your understanding and prepare you for real-world Kotlin programming.
MCQs on Generics in Kotlin
1. Generic Classes and Functions
What is the correct syntax to define a generic class in Kotlin? a) class Box<T> {} b) class Box<T>: Any() {} c) class Box[] {} d) class Box<T: Any> {}
Which of the following correctly defines a generic function in Kotlin? a) fun <T> printValue(value: T) { println(value) } b) fun printValue<T>(value: T) { println(value) } c) fun printValue(value: T): Unit { println(value) } d) fun <T: Comparable> printValue(value: T) { println(value) }
How can you restrict the type parameter in a generic class to be a subtype of Number? a) class Box<T : Number> {} b) class Box<T : Int> {} c) class Box<Number> {} d) class Box<T : Comparable<Number>> {}
Which of the following generic functions will compile successfully in Kotlin? a) fun <T> printValue(value: T): T b) fun <T> printValue(value: Any): T c) fun <T> printValue(value: T) where T: String d) fun <T: Int> printValue(value: T)
What does the T represent in a generic class? a) Type parameter b) Template type c) Transformation d) None of the above
2. Type Variance (in, out)
What does the out keyword indicate in Kotlin’s type variance? a) The type can only be used as an output b) The type can only be used as an input c) The type can be both input and output d) It specifies an optional type
In the context of generic types, what does in denote? a) Type is covariant b) Type is contravariant c) Type is invariant d) Type is a subtype
Which of the following is an example of using the out keyword correctly in a generic class? a) class Box<out T>(val value: T) b) class Box<in T>(val value: T) c) class Box<T> { val value: T } d) class Box<T>(val value: out T)
What is the primary use of type variance in Kotlin? a) To control type inheritance and substitution b) To convert types dynamically c) To create polymorphic methods d) To enforce stricter type checking
Which statement is true about the in keyword in Kotlin? a) It marks the type as invariant b) It can only be used for input types c) It ensures type safety in contravariant types d) It makes the type non-nullable
What will be the result of the following code snippet? class Box<in T>(val value: T) val box: Box<Number> = Box<Int>(5) a) Compilation error b) It compiles successfully c) Type mismatch at runtime d) It compiles but causes a runtime exception
What is the return type of the following function? fun <T> getFirstElement(list: List<T>): T { return list[0] } a) List<T> b) T c) Any? d) Unit
Can in be used in a read-only context in Kotlin? a) Yes, it allows covariance b) No, it’s used for mutation only c) Yes, but only for primitive types d) No, it applies to input types only
Which type variance keyword allows reading from a generic type in Kotlin? a) in b) out c) covariant d) contravariant
What happens if you attempt to use in on a generic type that is being accessed in a read-only fashion? a) It works without any issues b) The code will not compile c) It will compile but produce a runtime error d) The generic class is transformed automatically
3. Reified Types
What is the purpose of the reified keyword in Kotlin? a) To enable type checks at runtime b) To enable type erasure c) To retain the type information at runtime d) To declare a type as generic
How can you use reified types in a function in Kotlin? a) By adding reified before the type parameter in a function b) By marking the function as inline c) By using ::class with the type parameter d) Both a and b
Which of the following code snippets correctly defines a reified function? a) inline fun <reified T> printType(value: T) b) fun <reified T> printType(value: T) c) inline fun <T> reified printType(value: T) d) inline fun printType<T: reified>(value: T)
What does reified enable that standard generics in Kotlin cannot? a) Type erasure b) Type checks at runtime c) Extension functions d) Advanced polymorphism
What will the following code print? inline fun <reified T> printClassName() { println(T::class.java.name) } printClassName<String>() a) java.lang.String b) String c) java.lang.Object d) NullPointerException
Which of the following is true about the reified keyword? a) It can only be used with inline functions b) It preserves the type parameter at runtime c) It allows type erasure d) It can be used in any function
What does T::class.java return in a reified function? a) The class object of T b) The object of T at runtime c) The class literal of T d) The generic type signature
Can reified types be used in non-inline functions in Kotlin? a) No, it only works with inline functions b) Yes, reified types can be used anywhere c) Only for specific types of functions d) Yes, but requires type casting
Which of the following correctly demonstrates reified type use in an inline function? a) inline fun <T> checkType(value: T) b) inline fun <reified T> checkType(value: T) c) inline fun checkType(value: T) d) fun <reified T> checkType(value: T)
How does using reified in Kotlin affect performance? a) It can slightly degrade performance due to runtime type checks b) It improves performance by removing type checks c) No impact on performance d) It only impacts memory usage
Which keyword allows you to access the type of a generic type parameter at runtime in Kotlin? a) type b) reified c) inline d) generic
What is the main advantage of reified types in Kotlin? a) They support runtime type information b) They avoid type checking errors at compile time c) They make generics faster d) They enforce compile-time type safety
How can you achieve runtime type checking in Kotlin with generics? a) By using reified types b) By using !! operator c) By using ::class d) By using the in keyword
Can reified types be used with generic properties in Kotlin? a) No, only with generic functions b) Yes, but requires type casting c) Yes, with certain restrictions d) No, reified types are function-only
What does the following code do?kotlinCopy codeinline fun <reified T> isString(value: Any): Boolean { return value is T } isString<String>("Test") a) Returns true b) Returns false c) Throws an error d) None of the above
Answers Table
Qno
Answer (Option with Text)
1
a) class Box<T> {}
2
a) fun <T> printValue(value: T) { println(value) }
3
a) class Box<T : Number> {}
4
a) fun <T> printValue(value: T): T
5
a) Type parameter
6
a) The type can only be used as an output
7
b) Type is contravariant
8
a) class Box<out T>(val value: T)
9
a) To control type inheritance and substitution
10
c) It ensures type safety in contravariant types
11
a) Compilation error
12
b) T
13
b) No, it’s used for mutation only
14
b) out
15
a) It works without any issues
16
c) To retain the type information at runtime
17
d) Both a and b
18
a) inline fun <reified T> printType(value: T)
19
b) Type checks at runtime
20
a) java.lang.String
21
a) It can only be used with inline functions
22
a) The class object of T
23
a) No, it only works with inline functions
24
b) inline fun <reified T> checkType(value: T)
25
a) It can slightly degrade performance due to runtime type checks