Bird
Raised Fist0
Kotlinprogramming~20 mins

Returning functions from functions in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Returning Functions
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code returning a function?

Look at this Kotlin code that returns a function. What will it print?

Kotlin
fun makeMultiplier(x: Int): (Int) -> Int {
    return { y -> x * y }
}

fun main() {
    val multiplyBy3 = makeMultiplier(3)
    println(multiplyBy3(5))
}
A15
B8
C35
DCompilation error
Attempts:
2 left
💡 Hint

Remember the returned function multiplies its input by the captured value.

🧠 Conceptual
intermediate
2:00remaining
Which Kotlin function type correctly represents a function returning a function?

Which of these Kotlin function types correctly describes a function that returns another function taking an Int and returning a String?

A(Int) -> Int -> String
B(Int) -> (String) -> Int
C(Int) -> (Int, String) -> String
D(Int) -> (Int) -> String
Attempts:
2 left
💡 Hint

Remember to use parentheses to group function types properly.

🔧 Debug
advanced
2:00remaining
What error does this Kotlin code produce?

Consider this Kotlin code that tries to return a function. What error will it cause?

Kotlin
fun getAdder(x: Int): (Int) -> Int {
    return { y -> x + y }
}

fun main() {
    val add5 = getAdder(5)
    println(add5(10))
}
ASyntax error: unexpected token '->' in return type
BRuntime error: NullPointerException
CNo error, prints 15
DCompilation error: Type mismatch in return statement
Attempts:
2 left
💡 Hint

Check the syntax for function types in Kotlin return types.

🚀 Application
advanced
2:00remaining
How many times is the inner function called in this Kotlin code?

Analyze this Kotlin code that returns a function and calls it multiple times. How many times is the inner function executed?

Kotlin
fun counter(): () -> Int {
    var count = 0
    return {
        count += 1
        count
    }
}

fun main() {
    val c = counter()
    println(c())
    println(c())
    println(c())
}
A1
B3
C0
DCompilation error
Attempts:
2 left
💡 Hint

Each call to c() runs the inner function once.

❓ Predict Output
expert
2:00remaining
What is the output of this Kotlin code with nested returned functions?

Examine this Kotlin code with nested functions returned from functions. What will it print?

Kotlin
fun outer(x: Int): (Int) -> (Int) -> Int {
    return { y -> { z -> x + y + z } }
}

fun main() {
    val f = outer(1)
    val g = f(2)
    println(g(3))
}
A5
B9
C6
DCompilation error
Attempts:
2 left
💡 Hint

Trace the values captured by each returned function.

Practice

(1/5)
1. What does it mean when a Kotlin function returns another function?
easy
A. The function can only return primitive data types like Int or String.
B. The function cannot have parameters if it returns another function.
C. The outer function produces a function as its result that can be called later.
D. The returned function must be called immediately inside the outer function.

Solution

  1. Step 1: Understand function return types

    In Kotlin, a function can return any type, including another function type.
  2. Step 2: Interpret returning a function

    Returning a function means the outer function gives back a function that can be stored or called later.
  3. Final Answer:

    The outer function produces a function as its result that can be called later. -> Option C
  4. Quick Check:

    Returning a function means output is a callable function [OK]
Hint: Returning function means output is a callable function [OK]
Common Mistakes:
  • Thinking only primitive types can be returned
  • Assuming returned function must be called immediately
  • Believing returned functions cannot have parameters
2. Which of the following is the correct syntax to declare a Kotlin function that returns another function taking an Int and returning a String?
easy
A. fun outer(): (Int) -> String = { i -> i.toString }
B. fun outer(): (Int) -> String { return { i -> i.toString() } }
C. fun outer(): (Int) String { return { i -> i.toString() } }
D. fun outer(): Int -> String = return { i -> i.toString() } }

Solution

  1. Step 1: Check function return type syntax

    In Kotlin, a function returning another function uses syntax like: fun name(): (ParamType) -> ReturnType
  2. Step 2: Verify the return statement

    The function returns a lambda matching (Int) -> String, e.g., { i -> i.toString() }
  3. Final Answer:

    fun outer(): (Int) -> String { return { i -> i.toString() } } -> Option B
  4. Quick Check:

    Correct function type syntax = fun outer(): (Int) -> String { return { i -> i.toString() } } [OK]
Hint: Return type for function is (ParamType) -> ReturnType [OK]
Common Mistakes:
  • Using arrow -> incorrectly in return type
  • Mixing return type syntax with function body
  • Omitting parentheses around function type
3. What is the output of this Kotlin code?
fun makeMultiplier(x: Int): (Int) -> Int {
    return { y -> x * y }
}

fun main() {
    val multiplyBy3 = makeMultiplier(3)
    println(multiplyBy3(5))
}
medium
A. 35
B. 8
C. Compilation error
D. 15

Solution

  1. Step 1: Understand makeMultiplier function

    makeMultiplier returns a function that multiplies its input by x (captured from outer function).
  2. Step 2: Trace main function calls

    multiplyBy3 is a function multiplying by 3; multiplyBy3(5) = 3 * 5 = 15.
  3. Final Answer:

    15 -> Option D
  4. Quick Check:

    3 * 5 = 15 [OK]
Hint: Returned function uses captured outer variable [OK]
Common Mistakes:
  • Confusing parameter names
  • Forgetting the closure captures x
  • Expecting immediate multiplication without calling returned function
4. Identify the error in this Kotlin code that tries to return a function:
fun outer(): (Int) -> Int {
    val inner = { x: Int -> x * 2 }
    return inner()
}
medium
A. Calling inner() instead of returning the function itself
B. Missing return type declaration for outer function
C. Incorrect lambda parameter syntax
D. Cannot return a lambda from a function

Solution

  1. Step 1: Analyze return statement

    inner is a lambda function; inner() calls it and returns Int, not a function.
  2. Step 2: Correct return value

    To return the function itself, return inner without parentheses.
  3. Final Answer:

    Calling inner() instead of returning the function itself -> Option A
  4. Quick Check:

    Return function, not function call [OK]
Hint: Return function name without () to return function itself [OK]
Common Mistakes:
  • Returning function call instead of function
  • Omitting function return type
  • Misunderstanding lambda syntax
5. Given this Kotlin code, what will be the output?
fun createCounter(start: Int): () -> Int {
    var count = start
    return {
        count += 1
        count
    }
}

fun main() {
    val counter = createCounter(10)
    println(counter())
    println(counter())
}
hard
A. 11 12
B. 10 11
C. 12 13
D. Compilation error

Solution

  1. Step 1: Understand closure capturing variable

    The returned lambda captures 'count' variable and increments it each call.
  2. Step 2: Trace calls to counter()

    First call: count = 10 + 1 = 11; prints 11. Second call: count = 11 + 1 = 12; prints 12.
  3. Final Answer:

    11 12 -> Option A
  4. Quick Check:

    Closure increments count on each call [OK]
Hint: Closures keep state between calls [OK]
Common Mistakes:
  • Assuming count resets each call
  • Confusing initial value with first output
  • Expecting compilation error due to var usage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard