0
0
Kotlinprogramming~20 mins

Higher-order function declaration in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Higher-order Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple higher-order function call
What is the output of this Kotlin code that uses a higher-order function?
Kotlin
fun operateOnNumber(x: Int, operation: (Int) -> Int): Int {
    return operation(x)
}

fun main() {
    val result = operateOnNumber(5) { it * 3 }
    println(result)
}
AError: Missing return type
B5
C15
D0
Attempts:
2 left
💡 Hint
Look at how the lambda multiplies the input by 3.
🧠 Conceptual
intermediate
1:30remaining
Understanding function types in Kotlin
Which of the following is the correct Kotlin function type declaration for a function that takes a String and returns a Boolean?
ABoolean -> String
B(String) -> Boolean
Cfun(String): Boolean
DString -> Boolean
Attempts:
2 left
💡 Hint
Function types use the arrow notation with parentheses around parameters.
🔧 Debug
advanced
2:00remaining
Identify the error in this higher-order function declaration
What error does this Kotlin code produce?
Kotlin
fun applyTwice(x: Int, f: (Int) -> Int): Int {
    return f(f(x))
}

fun main() {
    println(applyTwice(2) { it + 3 })
}
ASyntaxError: Missing parentheses around function type parameter
BTypeError: Cannot invoke function
CNo error, prints 8
DRuntimeException: Null pointer
Attempts:
2 left
💡 Hint
Check the function type syntax in the parameter list.
Predict Output
advanced
2:30remaining
Output of nested higher-order functions
What is the output of this Kotlin code?
Kotlin
fun compose(f: (Int) -> Int, g: (Int) -> Int): (Int) -> Int {
    return { x -> f(g(x)) }
}

fun main() {
    val add2 = { x: Int -> x + 2 }
    val times3 = { x: Int -> x * 3 }
    val combined = compose(add2, times3)
    println(combined(4))
}
A14
B18
C24
D20
Attempts:
2 left
💡 Hint
Remember the order of function composition: f(g(x)) means g runs first.
🧠 Conceptual
expert
1:30remaining
Effect of inline keyword on higher-order functions
What is the main benefit of declaring a higher-order function as inline in Kotlin?
AIt disables the use of lambdas as parameters
BIt allows the function to be called asynchronously
CIt automatically caches the function result
DIt reduces runtime overhead by avoiding object allocation for lambdas
Attempts:
2 left
💡 Hint
Think about performance and how lambdas are handled at runtime.