0
0
Kotlinprogramming~10 mins

Why lambdas enable functional style in Kotlin - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting the operator inside the lambda.
2fill in blank
medium

Complete 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'
Ait
Bx
Cnum
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name without declaring it.
Not using the implicit it parameter.
3fill in blank
hard

Fix 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'
Anumber
Bit
Cnum
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names not declared in the lambda.
Forgetting to use it for single-parameter lambdas.
4fill in blank
hard

Fill 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'
Alength
Bsize
C3
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using size which is not a string property.
Comparing to 4 instead of 3.
5fill in blank
hard

Fill 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'
Aw
Blength
C4
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size which is not a string property.
Using inconsistent variable names.