Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main benefit of using reified type parameters in Kotlin inline functions?
easy
A. They improve performance by avoiding inline expansion.
B. They allow access to the actual type at runtime inside the function.
C. They enable functions to be called without specifying type arguments.
D. They automatically convert types to strings.

Solution

  1. Step 1: Understand reified type parameters

    Reified type parameters let you use the real type inside an inline function at runtime, unlike normal generics which erase type info.
  2. Step 2: Identify the main benefit

    This means you can do type checks and casts safely inside the function using the actual type.
  3. Final Answer:

    They allow access to the actual type at runtime inside the function. -> Option B
  4. Quick Check:

    Reified = real type access [OK]
Hint: Reified means real type info available at runtime [OK]
Common Mistakes:
  • Thinking reified improves performance by avoiding inline
  • Believing reified removes need for type arguments
  • Confusing reified with automatic type conversion
2. Which of the following is the correct syntax to declare an inline function with a reified type parameter T in Kotlin?
easy
A. inline fun myFunction() { }
B. inline fun <T> myFunction() { }
C. fun inline myFunction() { }
D. fun inline myFunction() { }

Solution

  1. Step 1: Recall syntax for inline with reified

    The correct syntax places inline before the function and reified before the type parameter inside angle brackets.
  2. Step 2: Check each option

    inline fun myFunction() { } correctly uses inline fun <reified T> myFunction(). Others have wrong order or missing keywords.
  3. Final Answer:

    inline fun <reified T> myFunction() { } -> Option A
  4. Quick Check:

    inline + reified before T = correct syntax [OK]
Hint: Put 'inline' before fun and 'reified' before type param [OK]
Common Mistakes:
  • Placing 'reified' after the type parameter
  • Writing 'fun inline' instead of 'inline fun'
  • Omitting 'inline' keyword
3. What will be the output of the following Kotlin code?
inline fun  isString(value: Any): Boolean {
    return value is T
}

fun main() {
    println(isString<String>("hello"))
    println(isString<Int>(123))
    println(isString<Double>(123.0))
}
medium
A. false\nfalse\nfalse
B. true\nfalse\ntrue
C. false\ntrue\nfalse
D. true\ntrue\ntrue

Solution

  1. Step 1: Understand the function behavior

    The function checks if the given value is of type T using the reified type parameter for runtime type check.
  2. Step 2: Evaluate each println call

    First call: isString<String>("hello") -- "hello" is String -> true.
    Second call: isString<Int>(123) -- 123 is Int -> true.
    Third call: isString<Double>(123.0) -- 123.0 is Double -> true.
    Output: true\ntrue\ntrue.
  3. Final Answer:

    true\ntrue\ntrue -> Option D
  4. Quick Check:

    All type checks match = true true true [OK]
Hint: Reified lets 'is' check real type at runtime [OK]
Common Mistakes:
  • Assuming type erasure causes false results
  • Mixing up types in the is check
  • Ignoring that reified allows runtime type check
4. Identify the error in the following Kotlin code snippet:
fun  printTypeName() {
    println(T::class.simpleName)
}

fun main() {
    printTypeName<String>()
}
medium
A. Missing type argument when calling printTypeName.
B. T::class.simpleName is not a valid expression.
C. Cannot use reified type parameter in a non-inline function.
D. Function printTypeName must return a value.

Solution

  1. Step 1: Check function declaration

    The function uses reified type parameter but is not marked inline. This is not allowed in Kotlin.
  2. Step 2: Understand Kotlin rules for reified

    Reified type parameters require the function to be inline to preserve type info at runtime.
  3. Final Answer:

    Cannot use reified type parameter in a non-inline function. -> Option C
  4. Quick Check:

    Reified requires inline function [OK]
Hint: Reified always needs inline keyword [OK]
Common Mistakes:
  • Forgetting to mark function as inline
  • Thinking reified works in normal functions
  • Misunderstanding T::class usage
5. You want to create a generic inline function createInstance that returns a new instance of type T using its no-argument constructor. Which of the following implementations correctly uses reified type parameters to achieve this?
hard
A. inline fun createInstance(): T = T::class.java.newInstance()
B. fun createInstance(): T = T::class.java.getDeclaredConstructor().newInstance()
C. inline fun <T> createInstance(): T = T::class.java.newInstance()
D. inline fun createInstance(): T = T()

Solution

  1. Step 1: Understand how to create instance with reified

    Using reified lets us access T::class.java at runtime to call newInstance() for no-arg constructor.
  2. Step 2: Check each option

    inline fun createInstance(): T = T::class.java.newInstance() uses inline and reified correctly and calls newInstance().
    fun createInstance(): T = T::class.java.getDeclaredConstructor().newInstance() is not inline, so reified is invalid.
    inline fun <T> createInstance(): T = T::class.java.newInstance() lacks reified, so T::class is invalid.
    inline fun createInstance(): T = T() tries to call T() which is invalid syntax in Kotlin.
  3. Final Answer:

    inline fun <reified T> createInstance(): T = T::class.java.newInstance() -> Option A
  4. Quick Check:

    Inline + reified + newInstance() = correct instance creation [OK]
Hint: Use inline + reified + T::class.java.newInstance() [OK]
Common Mistakes:
  • Omitting inline with reified
  • Trying to call T() directly
  • Using reified without inline
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard