MCQs on Generics | Kotlin

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

  1. 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> {}
  2. 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) }
  3. 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>> {}
  4. 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)
  5. 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)

  1. 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
  2. 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
  3. 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)
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. Which type variance keyword allows reading from a generic type in Kotlin?
    a) in
    b) out
    c) covariant
    d) contravariant
  10. 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

  1. 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
  2. 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
  3. 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)
  4. What does reified enable that standard generics in Kotlin cannot?
    a) Type erasure
    b) Type checks at runtime
    c) Extension functions
    d) Advanced polymorphism
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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)
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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

QnoAnswer (Option with Text)
1a) class Box<T> {}
2a) fun <T> printValue(value: T) { println(value) }
3a) class Box<T : Number> {}
4a) fun <T> printValue(value: T): T
5a) Type parameter
6a) The type can only be used as an output
7b) Type is contravariant
8a) class Box<out T>(val value: T)
9a) To control type inheritance and substitution
10c) It ensures type safety in contravariant types
11a) Compilation error
12b) T
13b) No, it’s used for mutation only
14b) out
15a) It works without any issues
16c) To retain the type information at runtime
17d) Both a and b
18a) inline fun <reified T> printType(value: T)
19b) Type checks at runtime
20a) java.lang.String
21a) It can only be used with inline functions
22a) The class object of T
23a) No, it only works with inline functions
24b) inline fun <reified T> checkType(value: T)
25a) It can slightly degrade performance due to runtime type checks
26b) reified
27a) They support runtime type information
28a) By using reified types
29a) No, only with generic functions
30a) Returns true

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