Complete the code to print each number in the list.
val numbers = listOf(1, 2, 3, 4, 5) for ([1] in numbers) { println([1]) }
The variable num is used to represent each element in the list numbers during the loop.
Complete the code to print each fruit in the list.
val fruits = listOf("apple", "banana", "cherry") for ([1] in fruits) { println([1].uppercase()) }
The variable f is used to represent each fruit in the list fruits. It is used inside the loop to print the uppercase version.
Fix the error in the loop to print each character in the string.
val word = "hello" for ([1] in word) { println([1]) }
The variable c is used to represent each character in the string word. It is a common short name for characters.
Fill both blanks to create a map of word lengths for words longer than 3 letters.
val words = listOf("cat", "house", "tree", "a", "dog") val lengths = words.filter { it.length [1] 3 }.associateWith { it.[2] } println(lengths)
size which is not a Kotlin string property.count without parentheses which is a function.The filter uses it.length > to select words longer than 3 letters. The length property gives the length of each word.
Fill all three blanks to create a map of uppercase words to their lengths for words starting with 'b'.
val words = listOf("bat", "ball", "cat", "bird") val result = words.filter { it.startsWith([1]) } .associateBy([2]) { it.[3] } println(result)
it.length which causes duplicate it. prefix.count instead of length.The filter checks if words start with the string "b". The associateBy uses the uppercase word as key and the length as value.