0
0
Kotlinprogramming~20 mins

Inline functions and performance in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Inline Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of inline function with lambda
What is the output of this Kotlin code using an inline function with a lambda parameter?
Kotlin
inline fun operate(x: Int, operation: (Int) -> Int): Int {
    return operation(x)
}

fun main() {
    val result = operate(5) { it * 2 }
    println(result)
}
A10
B5
C25
DCompilation error
Attempts:
2 left
💡 Hint
Think about what the lambda does to the input number.
🧠 Conceptual
intermediate
1:30remaining
Effect of inline on function call overhead
Which statement best describes the effect of marking a function as inline in Kotlin?
AIt eliminates the function call overhead by copying the function body at call sites.
BIt makes the function run on a separate thread automatically.
CIt prevents the function from being called recursively.
DIt disables all optimizations for the function.
Attempts:
2 left
💡 Hint
Think about what 'inline' means in terms of code generation.
🔧 Debug
advanced
2:30remaining
Why does this inline function cause a compilation error?
Consider this Kotlin code snippet. Why does it cause a compilation error?
Kotlin
inline fun runTwice(action: () -> Unit) {
    action()
    action()
}

fun main() {
    var x = 0
    runTwice { x += 1 }
    println(x)
}
AThe function 'runTwice' must be marked 'noinline' for this lambda.
BInline functions cannot accept lambdas as parameters.
CThe lambda must return a value, but it returns Unit.
DCannot modify 'val' captured in lambda; 'x' should be 'var'.
Attempts:
2 left
💡 Hint
Check the mutability of the variable 'x' used inside the lambda.
Predict Output
advanced
2:00remaining
Output of inline function with crossinline lambda
What is the output of this Kotlin code using an inline function with a crossinline lambda?
Kotlin
inline fun runCrossinline(crossinline block: () -> Unit) {
    val runnable = Runnable {
        block()
    }
    runnable.run()
}

fun main() {
    runCrossinline { println("Hello from crossinline") }
}
ANo output
BCompilation error due to crossinline usage
CHello from crossinline
DRuntime exception
Attempts:
2 left
💡 Hint
Crossinline prevents non-local returns but allows lambda execution inside Runnable.
🧠 Conceptual
expert
3:00remaining
Why use inline functions with reified type parameters?
Why are inline functions required to use reified type parameters in Kotlin?
ABecause reified types allow the function to run on the JVM bytecode level only.
BBecause type information is erased at runtime, inlining preserves it for reified types.
CBecause inline functions cannot have generic parameters unless reified.
DBecause reified types disable inlining to preserve type safety.
Attempts:
2 left
💡 Hint
Think about how Kotlin handles generic types at runtime and what inlining does.