0
0
Kotlinprogramming~10 mins

Higher-order function declaration in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a higher-order function that takes a function as a parameter.

Kotlin
fun operateOnNumber(x: Int, operation: (Int) -> Int): Int {
    return operation([1])
}
Drag options to blanks, or click blank then click option'
AInt
Bx
Coperation
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function name instead of the argument.
Using a variable that is not defined in the function.
2fill in blank
medium

Complete the code to declare a higher-order function that returns a function.

Kotlin
fun makeMultiplier(factor: Int): (Int) -> Int {
    return [1](number: Int) = number * factor
}
Drag options to blanks, or click blank then click option'
Afun
Bval
Clambda
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using val instead of fun to declare the function.
Trying to return a lambda without the fun keyword.
3fill in blank
hard

Fix the error in the higher-order function declaration that takes two functions as parameters.

Kotlin
fun combineOperations(x: Int, op1: (Int) -> Int, op2: (Int) -> Int): Int {
    return op2(op1([1]))
}
Drag options to blanks, or click blank then click option'
Aop1
Bop2
Cx
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function op1 itself instead of its argument.
Using an undefined variable instead of x.
4fill in blank
hard

Fill both blanks to declare a higher-order function that takes a function and returns a function.

Kotlin
fun transformFunction(f: (Int) -> Int): (Int) -> Int {
    return [1](x: Int) = f(x) + [2]
}
Drag options to blanks, or click blank then click option'
Afun
Bx
C1
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using val instead of fun.
Forgetting to add 1 to the result.
5fill in blank
hard

Fill all three blanks to declare a higher-order function that takes a function and returns a function that applies it twice.

Kotlin
fun twice(f: (Int) -> Int): (Int) -> Int {
    return [1](x: Int) = f(f([2])) + [3]
}
Drag options to blanks, or click blank then click option'
Afun
Bx
C0
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using val instead of fun.
Forgetting to pass x to f.
Adding a number other than 0.