Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a function reference to the 'println' function.
Kotlin
val printer = [1]println printer("Hello, Kotlin!")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.' instead of '::' which calls the function instead of referencing it.
Using '->' or '=>' which are not valid for function references in Kotlin.
✗ Incorrect
In Kotlin, the '::' operator is used to create a function reference. Here, '::println' creates a reference to the println function.
2fill in blank
mediumComplete the code to pass a function reference of 'isEven' to the 'filter' function.
Kotlin
fun isEven(n: Int) = n % 2 == 0 val numbers = listOf(1, 2, 3, 4) val evens = numbers.filter([1]isEven) println(evens)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.' which tries to call the function instead of referencing it.
Using lambda syntax '->' or '=>' which is incorrect here.
✗ Incorrect
The '::' operator creates a reference to the 'isEven' function, which can be passed to 'filter' to select even numbers.
3fill in blank
hardFix the error by completing the code to create a function reference to the 'toUpperCase' method of String.
Kotlin
val toUpper: (String) -> String = [1]String::toUpperCase println(toUpper("kotlin"))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'String::' before 'String::toUpperCase' which is redundant.
Using '.' which tries to call the method instead of referencing it.
✗ Incorrect
The correct syntax to reference a member function is '::' followed by the class and method name, like 'String::toUpperCase'.
4fill in blank
hardFill both blanks to create a map of words to their lengths using a function reference and a property reference.
Kotlin
val words = listOf("apple", "banana", "cherry") val lengths = words.associateWith([1]) { it.[2] } println(lengths)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'String.length' which is invalid syntax.
Using 'length' without context or as a string.
✗ Incorrect
The 'associateWith' function takes a function reference to get the value for each key. 'String::length' is the correct function reference, and 'length' is the property accessed on 'it'.
5fill in blank
hardFill all three blanks to filter a list of strings by length using a function reference and a lambda.
Kotlin
val words = listOf("cat", "dog", "elephant", "fox") val filtered = words.filter { word -> word.length [1] [2] } val lengthCheck: (String) -> Boolean = [3] { it.length > 3 } println(filtered) println(filtered.filter(lengthCheck))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' which filters the wrong words.
Not using '::' when assigning the lambda to the function variable.
✗ Incorrect
The filter uses '>' and '3' to select words longer than 3 characters. The function reference operator '::' is used to assign the lambda to 'lengthCheck'.