Complete the code to print each number in the list using forEach.
val numbers = listOf(1, 2, 3, 4, 5) numbers.forEach { [1] -> println([1]) }
The lambda parameter name can be any valid identifier. Here, number is used to represent each element.
Complete the code to print each fruit in uppercase using forEach.
val fruits = listOf("apple", "banana", "cherry") fruits.forEach { [1] -> println([1].uppercase()) }
The lambda parameter fruit represents each element in the list fruits.
Fix the error in the forEach lambda parameter to correctly print each name.
val names = listOf("Anna", "Bob", "Cara") names.forEach { [1] -> println([1]) }
The lambda parameter should be a single element, so name is correct. Using names (plural) causes confusion and errors.
Fill both blanks to create a map of word lengths for words longer than 3 characters.
val words = listOf("cat", "house", "tree", "a", "elephant") val lengths = words.filter { it.length [1] 3 }.associateWith { [2].length }
The filter uses it.length > 3 to select words longer than 3 characters. The associateWith uses it.length to map each word to its length.
Fill all three blanks to create a map of uppercase keys and their lengths for words starting with 'a'.
val words = listOf("apple", "banana", "apricot", "cherry", "avocado") val result = words.filter { [1].startsWith("a") }.associateBy([2].uppercase()) { [3].length }
In the filter, it represents each word. In associateBy, word.uppercase() is the key, and word.length is the value.