Recall & Review
beginner
What does the
filter function do in Kotlin?The
filter function returns a list containing only the elements that satisfy a given condition (predicate).Click to reveal answer
beginner
What is the difference between
filter and filterNot in Kotlin?filter keeps elements that match the condition, while filterNot keeps elements that do NOT match the condition.Click to reveal answer
beginner
How would you use
filterNot to remove all even numbers from a list?Use
filterNot { it % 2 == 0 } to keep only odd numbers by removing even numbers.Click to reveal answer
beginner
Given
val numbers = listOf(1, 2, 3, 4, 5), what is the result of numbers.filter { it > 3 }?The result is
[4, 5] because only numbers greater than 3 are kept.Click to reveal answer
intermediate
Can
filter and filterNot be used with any Kotlin collection?Yes, both functions can be used with any Kotlin collection like lists, sets, and sequences.
Click to reveal answer
What does
filterNot { it % 2 == 0 } do to a list of integers?✗ Incorrect
It removes elements where the condition is true (even numbers), so only odd numbers remain.
Which function keeps elements that satisfy the condition?
✗ Incorrect
filter keeps elements matching the condition.What will
listOf(1,2,3).filter { it > 5 } return?✗ Incorrect
No elements are greater than 5, so the result is an empty list.
If you want to remove elements that are less than 10, which function and condition would you use?
✗ Incorrect
filterNot removes elements where the condition is true, so it removes elements less than 10.
Are
filter and filterNot functions destructive (do they change the original list)?✗ Incorrect
Both return new lists and do not change the original collection.
Explain how
filter and filterNot work in Kotlin with an example.Think about keeping or removing elements based on a condition.
You got /3 concepts.
Describe a real-life situation where you might use
filter and filterNot in a Kotlin program.Imagine sorting or selecting items from a list based on a rule.
You got /3 concepts.