0
0
Kotlinprogramming~10 mins

Filter and filterNot 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 filter even numbers from the list.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val evens = numbers.filter { it [1] 2 == 0 }
println(evens)
Drag options to blanks, or click blank then click option'
A%
B*
C/
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulo (%) causes wrong filtering.
Using multiplication (*) or addition (+) operators here is incorrect.
2fill in blank
medium

Complete the code to filter out odd numbers using filterNot.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val evens = numbers.filterNot { it [1] 2 != 0 }
println(evens)
Drag options to blanks, or click blank then click option'
A-
B*
C+
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or addition operators instead of modulo.
Confusing filter and filterNot logic.
3fill in blank
hard

Fix the error in the code to filter words longer than 3 characters.

Kotlin
val words = listOf("cat", "dog", "elephant", "fox")
val longWords = words.filter { it.length [1] 3 }
println(longWords)
Drag options to blanks, or click blank then click option'
A<=
B>
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than (<) instead of greater than (>).
Using equality (==) which only selects words of length exactly 3.
4fill in blank
hard

Fill both blanks to create a map of word lengths for words shorter than 5 characters.

Kotlin
val words = listOf("apple", "bat", "cat", "dog", "elephant")
val shortWordLengths = words.filter { it.length [1] 5 }.associateWith { it.[2] }
println(shortWordLengths)
Drag options to blanks, or click blank then click option'
A<
Blength
C>
DtoString
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than (>) instead of less than (<) in filter.
Using toString instead of length for mapping.
5fill in blank
hard

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

Kotlin
val words = listOf("apple", "bat", "cat", "dog", "ant")
val result = words.filterNot { it.startsWith([1]) }.associateWith { it.[2] }.mapKeys { it.key.[3]() }
println(result)
Drag options to blanks, or click blank then click option'
A"a"
Blength
Cuppercase
DtoUpperCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase instead of toUpperCase() function.
Forgetting quotes around "a" in startsWith.
Using wrong property for length.