0
0
Kotlinprogramming~10 mins

Returning functions from functions 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 return a function that adds 5 to its input.

Kotlin
fun addFive(): (Int) -> Int {
    return { x -> x + 5 }
}
Drag options to blanks, or click blank then click option'
A{ x -> x + 5 }
B{ x -> x - 5 }
C{ x -> x * 5 }
D{ x -> x / 5 }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a lambda that subtracts or multiplies instead of adding.
Not returning a lambda at all.
2fill in blank
medium

Complete the code to return a function that multiplies its input by 3.

Kotlin
fun multiplyByThree(): (Int) -> Int {
    return { x -> x * 3 }
}
Drag options to blanks, or click blank then click option'
A{ x -> x / 3 }
B{ x -> 3 - x }
C{ x -> x + 3 }
D{ x -> x * 3 }
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Dividing instead of multiplying.
3fill in blank
hard

Fix the error in the code to correctly return a function that subtracts 2 from its input.

Kotlin
fun subtractTwo(): (Int) -> Int {
    return { x -> x - 2 }
}
Drag options to blanks, or click blank then click option'
A{ x -> x + 2 }
B{ x -> x - 2 }
C{ x -> 2 - x }
D{ x -> x * 2 }
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 2 instead of subtracting.
Subtracting input from 2 instead of the other way around.
4fill in blank
hard

Fill both blanks to create a function that returns a function which adds a given number to its input.

Kotlin
fun adder(n: Int): (Int) -> Int {
    return { x -> x [1] n[2] }
}
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Adding extra operators after n.
5fill in blank
hard

Fill all three blanks to create a function that returns a function which multiplies its input by a given number and then adds another number.

Kotlin
fun multiplyAndAdd(multiplier: Int, addend: Int): (Int) -> Int {
    return { x -> x [1] multiplier [2] addend[3] }
}
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or division instead of addition.
Mixing the order of operations.