Bird
Raised Fist0
Kotlinprogramming~20 mins

Function references (::functionName) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Function Reference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of function reference with map
What is the output of this Kotlin code using function references?
Kotlin
fun square(x: Int) = x * x

fun main() {
    val numbers = listOf(1, 2, 3, 4)
    val squares = numbers.map(::square)
    println(squares)
}
ACompilation error
B[1, 2, 3, 4]
C[2, 4, 6, 8]
D[1, 4, 9, 16]
Attempts:
2 left
💡 Hint
Remember that ::square passes the function itself to map, which applies it to each element.
❓ Predict Output
intermediate
2:00remaining
Using bound function reference
What will this Kotlin code print?
Kotlin
class Greeter(val greeting: String) {
    fun greet(name: String) = "$greeting, $name!"
}

fun main() {
    val greeter = Greeter("Hello")
    val greetFunction = greeter::greet
    println(greetFunction("Alice"))
}
AHello, greetFunction!
BCompilation error
CHello, Alice!
DgreetFunction
Attempts:
2 left
💡 Hint
Bound function references keep the instance context.
🔧 Debug
advanced
2:00remaining
Identify the error with function reference usage
What error does this Kotlin code produce?
Kotlin
fun add(a: Int, b: Int) = a + b

fun main() {
    val numbers = listOf(1, 2, 3)
    val result = numbers.map(::add)
    println(result)
}
AType mismatch: Expected a function of type (Int) -> R but found (Int, Int) -> Int
BCompilation error: Unresolved reference 'add'
C[2, 3, 4]
D[1, 2, 3]
Attempts:
2 left
💡 Hint
Check the expected function type for map and the signature of add.
❓ Predict Output
advanced
2:00remaining
Using function reference with filter and predicate
What is the output of this Kotlin code?
Kotlin
fun isEven(n: Int) = n % 2 == 0

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5, 6)
    val evens = numbers.filter(::isEven)
    println(evens)
}
A[2, 4, 6]
B[1, 3, 5]
C[1, 2, 3, 4, 5, 6]
DCompilation error
Attempts:
2 left
💡 Hint
filter uses the function reference as a predicate to select elements.
❓ Predict Output
expert
2:00remaining
Output of function reference with overloaded functions
What will this Kotlin code print?
Kotlin
fun printValue(value: Int) = println("Int: $value")
fun printValue(value: String) = println("String: $value")

fun main() {
    val printer: (Any) -> Unit = ::printValue
    printer(42)
}
ARuntime error
BCompilation error: Overload resolution ambiguity
CString: 42
DInt: 42
Attempts:
2 left
💡 Hint
Consider how Kotlin resolves overloaded function references with a general type.

Practice

(1/5)
1. What does the Kotlin syntax ::functionName represent?
easy
A. A call to the function named functionName
B. A comment about the function named functionName
C. A variable declaration named functionName
D. A reference to the function named functionName

Solution

  1. Step 1: Understand the :: operator in Kotlin

    The :: operator is used to get a reference to a function, not to call it.
  2. Step 2: Differentiate between calling and referencing

    Calling a function uses parentheses like functionName(), while referencing uses ::functionName.
  3. Final Answer:

    A reference to the function named functionName -> Option D
  4. Quick Check:

    Function reference = ::functionName [OK]
Hint: Remember :: means 'point to function', not call it [OK]
Common Mistakes:
  • Confusing function reference (::) with function call (())
  • Thinking :: declares a variable
  • Assuming :: is a comment or annotation
2. Which of the following is the correct syntax to pass a function reference to map in Kotlin?
easy
A. list.map(::functionName)
B. list.map(:functionName)
C. list.map(functionName())
D. list.map(functionName)

Solution

  1. Step 1: Identify how to pass function references in Kotlin

    Function references use the :: operator before the function name when passed as arguments.
  2. Step 2: Check each option for correct syntax

    list.map(::functionName) uses ::functionName, which is the correct way to pass a function reference to map.
  3. Final Answer:

    list.map(::functionName) -> Option A
  4. Quick Check:

    Pass function reference with :: [OK]
Hint: Use :: before function name when passing as argument [OK]
Common Mistakes:
  • Passing function call instead of reference (using parentheses)
  • Omitting :: operator
  • Using incorrect single colon : instead of double ::
3. What is the output of this Kotlin code?
fun square(x: Int) = x * x
val numbers = listOf(1, 2, 3)
val squares = numbers.map(::square)
println(squares)
medium
A. [1, 4, 9]
B. [square, square, square]
C. [1, 2, 3]
D. Compilation error

Solution

  1. Step 1: Understand the function reference usage

    The ::square passes the square function to map, which applies it to each element.
  2. Step 2: Calculate the result of applying square to each number

    Applying square to 1, 2, 3 gives 1, 4, 9 respectively.
  3. Final Answer:

    [1, 4, 9] -> Option A
  4. Quick Check:

    map(::square) squares each element [OK]
Hint: map(::function) applies function to each list item [OK]
Common Mistakes:
  • Expecting original list instead of transformed list
  • Confusing function reference with function call
  • Thinking output is function names instead of results
4. Identify the error in this Kotlin code snippet:
fun greet(name: String) = "Hello, $name"
val greeter = greet
println(greeter("Alice"))
medium
A. Missing parentheses when assigning function reference
B. Cannot assign function directly without :: operator
C. Function greet must return Unit
D. No error, code runs fine

Solution

  1. Step 1: Check how function references are assigned

    Assigning a function to a variable requires the :: operator to get a reference.
  2. Step 2: Identify the missing :: operator

    The code assigns greet instead of ::greet, causing a type mismatch error.
  3. Final Answer:

    Cannot assign function directly without :: operator -> Option B
  4. Quick Check:

    Use :: to assign function reference [OK]
Hint: Use :: when assigning function to variable [OK]
Common Mistakes:
  • Assigning function name without ::
  • Adding parentheses which calls function instead of referencing
  • Assuming function must return Unit
5. Given this Kotlin code, what will be the output?
fun isEven(n: Int) = n % 2 == 0
val numbers = listOf(1, 2, 3, 4, 5, 6)
val evenNumbers = numbers.filter(::isEven)
println(evenNumbers)

Choose the correct output and explain why function references are useful here.
hard
A. [true, false, true, false, true, false]
B. [1, 3, 5]
C. [2, 4, 6]
D. Compilation error due to filter usage

Solution

  1. Step 1: Understand the function reference with filter

    The ::isEven passes the isEven function to filter, which keeps elements where the function returns true.
  2. Step 2: Apply isEven to each number and filter

    Numbers 2, 4, and 6 are even, so they remain in the filtered list.
  3. Step 3: Explain usefulness of function references

    Function references let us pass existing functions easily without writing extra lambdas.
  4. Final Answer:

    [2, 4, 6] -> Option C
  5. Quick Check:

    filter(::isEven) keeps even numbers [OK]
Hint: filter(::function) keeps items where function returns true [OK]
Common Mistakes:
  • Confusing filter with map output
  • Expecting boolean list instead of filtered list
  • Not using :: for function reference
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