Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a lambda that adds two numbers.
Kotlin
val sum = { a: Int, b: Int -> a [1] b } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting the operator inside the lambda.
✗ Incorrect
The lambda uses the plus operator + to add two numbers.
2fill in blank
mediumComplete the code to filter a list using a lambda that keeps even numbers.
Kotlin
val evens = numbers.filter { [1] % 2 == 0 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name without declaring it.
Not using the implicit
it parameter.✗ Incorrect
The implicit lambda parameter it represents each element in the list.
3fill in blank
hardFix the error in the lambda that multiplies each number by 2.
Kotlin
val doubled = numbers.map { [1] * 2 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names not declared in the lambda.
Forgetting to use
it for single-parameter lambdas.✗ Incorrect
The lambda uses the implicit parameter it to refer to each element.
4fill in blank
hardFill both blanks to create a lambda that filters words longer than 3 letters.
Kotlin
val longWords = words.filter { word -> word.[1] > [2] } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
size which is not a string property.Comparing to 4 instead of 3.
✗ Incorrect
The property length gives the number of letters in a word, and we compare it to 3.
5fill in blank
hardFill all three blanks to create a map of word lengths for words longer than 4 letters.
Kotlin
val wordLengths = words.filter { [1] -> [1].[2] > [3] }.associateWith { it.length } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
size which is not a string property.Using inconsistent variable names.
✗ Incorrect
Use w as the lambda parameter, length to get string length, and compare to 4.