Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a lambda that adds 5 to its input.
Kotlin
val addFive = { x: Int -> x [1] 5 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes subtraction.
Using '*' or '/' changes the operation to multiplication or division.
✗ Incorrect
The lambda adds 5 to the input using the + operator.
2fill in blank
mediumComplete the code to capture the variable 'factor' inside the lambda.
Kotlin
fun makeMultiplier(factor: Int): (Int) -> Int = { x -> x [1] factor } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds instead of multiplies.
Using '-' or '/' changes the operation incorrectly.
✗ Incorrect
The lambda multiplies the input by the captured 'factor' using '*'.
3fill in blank
hardFix the error in the lambda to correctly capture the variable 'count'.
Kotlin
var count = 0 val increment = { count [1] 1 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' alone does not increment the value.
Using '-=' or '*=' changes the value incorrectly.
✗ Incorrect
Use '+=' to increment the variable 'count' inside the lambda.
4fill in blank
hardFill both blanks to create a lambda that filters a list by length greater than 3.
Kotlin
val words = listOf("cat", "house", "dog", "elephant") val filtered = words.filter { word -> word.length [1] [2] }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' filters words shorter than 3.
Using 4 instead of 3 changes the filter condition.
✗ Incorrect
The lambda filters words with length greater than 3 using '>' and 3.
5fill in blank
hardFill all three blanks to create a map of words to their uppercase versions for words longer than 4.
Kotlin
val words = listOf("apple", "bat", "carrot", "dog") val result = words.filter { [1] -> [1].length [2] 4 } .associateWith { [3].uppercase() }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'it' in filter parameter causes error.
Using '<' filters wrong words.
Using 'length' as variable name is incorrect.
✗ Incorrect
The lambda filters words with length > 4 and maps each word to its uppercase version using 'word' and '>'.