0
0
Kotlinprogramming~5 mins

Filter and filterNot in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AKeeps only odd numbers
BKeeps only even numbers
CRemoves odd numbers
DRemoves all numbers
Which function keeps elements that satisfy the condition?
AfilterNot
Bmap
Creduce
Dfilter
What will listOf(1,2,3).filter { it > 5 } return?
A[] (empty list)
BError
C[5]
D[1, 2, 3]
If you want to remove elements that are less than 10, which function and condition would you use?
Afilter { it < 10 }
BfilterNot { it < 10 }
Cfilter { it > 10 }
DfilterNot { it > 10 }
Are filter and filterNot functions destructive (do they change the original list)?
AYes, they modify the original list
BOnly filter modifies the list
CNo, they return a new list
DOnly filterNot modifies the list
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.