0
0
Kotlinprogramming~10 mins

Collection size and emptiness checks 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 check if the list is empty.

Kotlin
if (myList.[1]()) {
    println("List is empty")
}
Drag options to blanks, or click blank then click option'
AisEmpty
Bcount
Csize
DisNotEmpty
Attempts:
3 left
💡 Hint
Common Mistakes
Using size == 0 instead of isEmpty()
Using isNotEmpty() which checks the opposite
2fill in blank
medium

Complete the code to check if the list has exactly 3 elements.

Kotlin
if (myList.[1] == 3) {
    println("List has 3 elements")
}
Drag options to blanks, or click blank then click option'
Acapacity
Bcount
Clength
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using length which is for arrays or strings
Using capacity which is unrelated
3fill in blank
hard

Fix the error in the code to check if the list is not empty.

Kotlin
if (myList.[1]()) {
    println("List has elements")
}
Drag options to blanks, or click blank then click option'
AhasElements
BisNotEmpty
CnotEmpty
DisEmpty
Attempts:
3 left
💡 Hint
Common Mistakes
Using isEmpty() which checks the opposite
Using non-existent functions like notEmpty or hasElements
4fill in blank
hard

Fill both blanks to create a map of words to their lengths, but only for words longer than 3 characters.

Kotlin
val lengths = words.filter { word -> word.length > 3 }.associate { word -> [1] to [2] }
Drag options to blanks, or click blank then click option'
Aword
Bword.length
Cword.size
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.size which is not a valid property for String
Using 'length' as a variable instead of word.length
5fill in blank
hard

Fill all three blanks to create a filtered list of words with length greater than 4, then map to their uppercase forms.

Kotlin
val result = words.filter { it.[1] > [2] }.map { it.[3]() }
Drag options to blanks, or click blank then click option'
Alength
B4
Cuppercase
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size instead of length for string length
Using uppercase without parentheses