Challenge - 5 Problems
Inline Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Think about what the lambda does to the input number.
✗ Incorrect
The inline function 'operate' takes an integer and a lambda that doubles it. So 5 * 2 = 10 is printed.
🧠 Conceptual
intermediate1:30remaining
Effect of inline on function call overhead
Which statement best describes the effect of marking a function as
inline in Kotlin?Attempts:
2 left
💡 Hint
Think about what 'inline' means in terms of code generation.
✗ Incorrect
Inlining copies the function body to the call site, removing the overhead of a function call.
🔧 Debug
advanced2: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) }
Attempts:
2 left
💡 Hint
Check the mutability of the variable 'x' used inside the lambda.
✗ Incorrect
The variable 'x' is declared as 'val' (immutable), but the lambda tries to increment it, causing a compilation error.
❓ Predict Output
advanced2: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") } }
Attempts:
2 left
💡 Hint
Crossinline prevents non-local returns but allows lambda execution inside Runnable.
✗ Incorrect
The crossinline lambda is executed inside Runnable.run(), printing the message successfully.
🧠 Conceptual
expert3:00remaining
Why use inline functions with reified type parameters?
Why are inline functions required to use
reified type parameters in Kotlin?Attempts:
2 left
💡 Hint
Think about how Kotlin handles generic types at runtime and what inlining does.
✗ Incorrect
Kotlin erases generic types at runtime, but marking a function inline with reified type parameters keeps type info available inside the function.