0
0
Kotlinprogramming~20 mins

Filter and filterNot in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Filter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of filter vs filterNot on a list
What is the output of the following Kotlin code?
Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val filtered = numbers.filter { it % 2 == 0 }
val filteredNot = numbers.filterNot { it % 2 == 0 }
println("filtered: $filtered")
println("filteredNot: $filteredNot")
A
filtered: []
filteredNot: [1, 2, 3, 4, 5]
B
filtered: [2, 4]
filteredNot: [1, 3, 5]
C
filtered: [1, 2, 3, 4, 5]
filteredNot: []
D
filtered: [1, 3, 5]
filteredNot: [2, 4]
Attempts:
2 left
💡 Hint
filter keeps elements matching the condition; filterNot keeps those that do not match.
Predict Output
intermediate
1:30remaining
Result size after filter and filterNot
Given the code below, how many elements are in the list 'result' after filtering?
Kotlin
val words = listOf("apple", "banana", "cherry", "date")
val result = words.filterNot { it.length <= 5 }
println(result.size)
A2
B3
C4
D1
Attempts:
2 left
💡 Hint
filterNot keeps elements where the condition is false.
🔧 Debug
advanced
2:00remaining
Why does this filterNot code cause a compilation error?
Identify the error in this Kotlin code snippet and select the correct explanation.
Kotlin
val nums = listOf(1, 2, 3, 4)
val result = nums.filterNot { it > }
println(result)
ASyntaxError: The lambda expression is incomplete; 'it >' is missing a value to compare.
BTypeError: 'it' is not defined in the lambda expression.
CRuntimeException: The filterNot function cannot be used with comparison operators.
DNo error; the code runs and filters out numbers greater than zero.
Attempts:
2 left
💡 Hint
Check the lambda expression syntax inside filterNot.
🧠 Conceptual
advanced
1:30remaining
Difference between filter and filterNot
Which statement correctly describes the difference between filter and filterNot in Kotlin?
Afilter returns elements not matching the predicate; filterNot returns elements matching the predicate.
Bfilter removes null elements; filterNot removes non-null elements.
Cfilter and filterNot both return elements matching the predicate but in different order.
Dfilter returns elements matching the predicate; filterNot returns elements not matching the predicate.
Attempts:
2 left
💡 Hint
Think about what happens when the predicate is true or false.
🚀 Application
expert
2:30remaining
Filter and filterNot combined usage
What is the output of this Kotlin code snippet?
Kotlin
val data = listOf("cat", "dog", "bird", "fish", "cow")
val filtered = data.filter { it.length > 3 }.filterNot { it.contains('f') }
println(filtered)
A["bird", "fish"]
B["fish"]
C["bird"]
D["dog", "cat", "cow"]
Attempts:
2 left
💡 Hint
First filter keeps words longer than 3 letters, then filterNot removes words containing 'f'.