0
0
Kotlinprogramming~20 mins

Reified type parameters with inline in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reified Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of inline function with reified type
What is the output of this Kotlin code snippet?
Kotlin
inline fun <reified T> printTypeName() {
    println(T::class.simpleName)
}

fun main() {
    printTypeName<String>()
    printTypeName<Int>()
}
AString\nInt
Bjava.lang.String\njava.lang.Integer
CT\nT
DCompilation error
Attempts:
2 left
💡 Hint
Reified type parameters allow access to the actual type at runtime inside inline functions.
🧠 Conceptual
intermediate
2:00remaining
Why use reified type parameters with inline functions?
Which of the following best explains why Kotlin requires the inline modifier when using reified type parameters?
ABecause reified types are only available in Java, not Kotlin.
BBecause reified types can only be used with suspend functions.
CBecause inline functions run slower and need reified types to optimize.
DBecause reified types need to be replaced with actual types at compile time, which inline functions allow.
Attempts:
2 left
💡 Hint
Think about how Kotlin handles generics and type erasure.
🔧 Debug
advanced
2:00remaining
Identify the error in this code using reified type parameter
What error does this Kotlin code produce?
Kotlin
fun <reified T> checkType(value: Any) {
    if (value is T) {
        println("Value is of type T")
    } else {
        println("Value is NOT of type T")
    }
}

fun main() {
    checkType<String>(123)
}
ACompilation error: 'reified' type parameters can only be used in inline functions
BPrints: Value is NOT of type T
CPrints: Value is of type T
DRuntime error: ClassCastException
Attempts:
2 left
💡 Hint
Check the function declaration for inline keyword.
📝 Syntax
advanced
2:00remaining
Correct syntax to use reified type parameter in inline function
Which option shows the correct syntax to declare an inline function with a reified type parameter and check if an object is of that type?
Ainline fun <T reified> isOfType(obj: Any) = obj is T
Binline fun <reified T> isOfType(obj: Any) = obj is T
Cfun <reified T> inline isOfType(obj: Any) = obj is T
Dfun <T> inline isOfType(obj: Any) = obj is reified T
Attempts:
2 left
💡 Hint
The order of keywords and placement of 'reified' matters.
🚀 Application
expert
2:00remaining
Using reified type parameters to filter a list
Given this Kotlin function, what is the output when filtering a mixed list for elements of type String?
Kotlin
inline fun <reified T> List<Any>.filterType(): List<T> {
    return this.filterIsInstance<T>()
}

fun main() {
    val mixedList = listOf(1, "apple", 2.5, "banana", 3)
    val strings = mixedList.filterType<String>()
    println(strings)
}
ACompilation error: filterIsInstance requires reified type
B[1, 2.5, 3]
C["apple", "banana"]
D["apple", 2.5, "banana"]
Attempts:
2 left
💡 Hint
filterIsInstance() returns elements of type T from the list.