A function reference in Kotlin is a way to refer to a function by its name using the :: operator, allowing you to pass the function as a value or assign it to a variable.
Click to reveal answer
beginner
How do you create a function reference for a top-level function named printMessage?
You create it by writing ::printMessage. This refers to the function itself without calling it.
Click to reveal answer
beginner
What is the difference between printMessage() and ::printMessage in Kotlin?
printMessage() calls the function immediately, while ::printMessage creates a reference to the function without calling it.
Click to reveal answer
intermediate
Can you use function references to pass functions as arguments in Kotlin? Give an example.
Yes. For example, listOf(1, 2, 3).forEach(::println) passes the println function as a reference to forEach.
Click to reveal answer
intermediate
How do you reference a member function of a class in Kotlin?
You use the class name followed by :: and the function name, like <code>String::toUpperCase</code>. This creates a reference to the member function.
Click to reveal answer
What does the Kotlin expression ::myFunction represent?
AA reference to the function named myFunction
BA call to the function myFunction
CA variable named myFunction
DAn error in syntax
✗ Incorrect
Using :: before a function name creates a reference to that function without calling it.
Which of the following is a correct way to pass a function reference to a higher-order function in Kotlin?
Alist.forEach(::print)
Blist.forEach(print())
Clist.forEach(print)
Dlist.forEach(:print)
✗ Incorrect
The correct syntax to pass a function reference is ::functionName, so ::print is correct.
What is the output of this Kotlin code?
fun greet() = println("Hello")
val ref = ::greet
ref()
ANo output
BHello
Cref
DCompilation error
✗ Incorrect
Calling ref() calls the function greet(), which prints "Hello".
How do you reference a member function length of the String class in Kotlin?
A::String.length
Blength::String
CString::length
DString.length::
✗ Incorrect
The correct syntax for referencing a member function is ClassName::functionName.
Which statement is true about function references in Kotlin?
AThey are not supported in Kotlin.
BThey immediately execute the function.
CThey can only reference top-level functions.
DThey allow functions to be passed as values.
✗ Incorrect
Function references let you treat functions as values to pass or store without calling them.
Explain what a function reference (::functionName) is in Kotlin and how it differs from calling a function.
Think about how you can pass a function itself, not its result.
You got /4 concepts.
Describe how to use function references with higher-order functions in Kotlin with an example.
Consider how you can pass println directly to forEach.
You got /4 concepts.
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
Step 1: Understand the :: operator in Kotlin
The :: operator is used to get a reference to a function, not to call it.
Step 2: Differentiate between calling and referencing
Calling a function uses parentheses like functionName(), while referencing uses ::functionName.
Final Answer:
A reference to the function named functionName -> Option D
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
Step 1: Identify how to pass function references in Kotlin
Function references use the :: operator before the function name when passed as arguments.
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.
Final Answer:
list.map(::functionName) -> Option A
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
Step 1: Understand the function reference usage
The ::square passes the square function to map, which applies it to each element.
Step 2: Calculate the result of applying square to each number