Bird
Raised Fist0
Kotlinprogramming~20 mins

Filter and filterNot in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 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'.

Practice

(1/5)
1. What does the filter function do in Kotlin collections?
easy
A. Sorts the collection in ascending order
B. Removes all items from the collection
C. Keeps items where the condition is false
D. Keeps items where the condition is true

Solution

  1. Step 1: Understand the purpose of filter

    The filter function selects elements that satisfy a given condition (true).
  2. Step 2: Compare with other options

    Options B and D describe different operations, and C describes filterNot, not filter.
  3. Final Answer:

    Keeps items where the condition is true -> Option D
  4. Quick Check:

    filter = keep true items [OK]
Hint: Remember: filter keeps true, filterNot keeps false [OK]
Common Mistakes:
  • Confusing filter with filterNot
  • Thinking filter removes all items
  • Mixing filter with sorting functions
2. Which of the following is the correct syntax to filter even numbers from a list numbers in Kotlin?
easy
A. numbers.filter { it / 2 == 0 }
B. numbers.filterNot { it % 2 == 0 }
C. numbers.filter { it % 2 == 0 }
D. numbers.filter { it % 2 != 0 }

Solution

  1. Step 1: Identify the condition for even numbers

    Even numbers satisfy it % 2 == 0.
  2. Step 2: Choose correct filter syntax

    filter keeps items where condition is true, so numbers.filter { it % 2 == 0 } keeps even numbers.
  3. Final Answer:

    numbers.filter { it % 2 == 0 } -> Option C
  4. Quick Check:

    filter with even condition = numbers.filter { it % 2 == 0 } [OK]
Hint: Use filter with condition true to keep desired items [OK]
Common Mistakes:
  • Using filterNot instead of filter
  • Using division instead of modulo
  • Confusing condition signs (== vs !=)
3. What is the output of this Kotlin code?
val nums = listOf(1, 2, 3, 4, 5)
val result = nums.filterNot { it > 3 }
println(result)
medium
A. [1, 2, 3]
B. [4, 5]
C. [1, 2, 3, 4, 5]
D. []

Solution

  1. Step 1: Understand filterNot condition

    filterNot { it > 3 } keeps items where it > 3 is false, so items ≤ 3.
  2. Step 2: Apply condition to list

    Numbers 1, 2, 3 are ≤ 3, so they are kept; 4 and 5 are removed.
  3. Final Answer:

    [1, 2, 3] -> Option A
  4. Quick Check:

    filterNot > 3 = keep ≤ 3 [OK]
Hint: filterNot keeps items where condition is false [OK]
Common Mistakes:
  • Confusing filterNot with filter
  • Including numbers greater than 3
  • Expecting empty list output
4. Find the error in this Kotlin code that tries to filter out odd numbers:
val numbers = listOf(1, 2, 3, 4, 5)
val evens = numbers.filterNot { it % 2 == 0 }
println(evens)
medium
A. The list is empty after filtering
B. It incorrectly keeps odd numbers instead of even
C. filterNot cannot be used with modulo operator
D. Syntax error in lambda expression

Solution

  1. Step 1: Analyze the filterNot condition

    filterNot { it % 2 == 0 } keeps items where it % 2 == 0 is false, so it keeps odd numbers.
  2. Step 2: Check intended output

    The code tries to get even numbers but actually keeps odd numbers, so logic is reversed.
  3. Final Answer:

    It incorrectly keeps odd numbers instead of even -> Option B
  4. Quick Check:

    filterNot with even condition keeps odd [OK]
Hint: filterNot reverses condition; use filter to keep true items [OK]
Common Mistakes:
  • Using filterNot instead of filter for positive condition
  • Assuming filterNot removes items instead of keeping false
  • Misreading modulo condition
5. Given a list val words = listOf("apple", "banana", "", "cherry", ""), which Kotlin expression returns a list of only the non-empty words using filter and filterNot correctly?
hard
A. words.filter { it.isNotEmpty() }
B. words.filterNot { it.isNotEmpty() }
C. words.filter { it.isEmpty() }
D. words.filterNot { it.length > 0 }

Solution

  1. Step 1: Identify condition for non-empty strings

    Non-empty strings satisfy it.isNotEmpty().
  2. Step 2: Choose correct filter usage

    filter keeps items where condition is true, so words.filter { it.isNotEmpty() } returns only non-empty words.
  3. Final Answer:

    words.filter { it.isNotEmpty() } -> Option A
  4. Quick Check:

    filter with isNotEmpty = keep non-empty [OK]
Hint: Use filter with positive condition to keep desired items [OK]
Common Mistakes:
  • Using filterNot with isNotEmpty to keep empty strings
  • Confusing isEmpty and isNotEmpty
  • Expecting filterNot to remove empty strings
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard