0
0
Kotlinprogramming~10 mins

For loop over collections in Kotlin - Interactive Code Practice

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

Complete the code to print each number in the list.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
for ([1] in numbers) {
    println([1])
}
Drag options to blanks, or click blank then click option'
Ai
Bitem
Cx
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not declared in the loop.
Using the list name instead of a variable.
2fill in blank
medium

Complete the code to print each fruit in the list.

Kotlin
val fruits = listOf("apple", "banana", "cherry")
for ([1] in fruits) {
    println([1].uppercase())
}
Drag options to blanks, or click blank then click option'
Afruit
BfruitName
Cf
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name different from the one in the loop.
Trying to use the list name instead of the variable.
3fill in blank
hard

Fix the error in the loop to print each character in the string.

Kotlin
val word = "hello"
for ([1] in word) {
    println([1])
}
Drag options to blanks, or click blank then click option'
Ac
Bchar
Cletter
Dch
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is too long or inconsistent.
Trying to use the string name instead of a variable.
4fill in blank
hard

Fill both blanks to create a map of word lengths for words longer than 3 letters.

Kotlin
val words = listOf("cat", "house", "tree", "a", "dog")
val lengths = words.filter { it.length [1] 3 }.associateWith { it.[2] }
println(lengths)
Drag options to blanks, or click blank then click option'
A>
Blength
Ccount
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size which is not a Kotlin string property.
Using count without parentheses which is a function.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths for words starting with 'b'.

Kotlin
val words = listOf("bat", "ball", "cat", "bird")
val result = words.filter { it.startsWith([1]) }
    .associateBy([2]) { it.[3] }
println(result)
Drag options to blanks, or click blank then click option'
A"b"
Bit.uppercase()
Clength
Dit.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes for strings.
Using it.length which causes duplicate it. prefix.
Using count instead of length.