Back to questions
JuniorTheoryCommon
What does the program print?
fun main() {
val nums = listOf(1, 2, 3, 4, 5)
println(nums.filter { it % 2 == 1 }.map { it * 10 })
}[10, 30, 50]
- Expecting
[20, 40]:filterkeeps the elements for which the condition istrue, soit % 2 == 1keeps the odd numbers. - Expecting each call in a chain to start from the original list:
mapworks on whatfilterreturned, andnumsitself never changes.
- What does the program print if the calls swap places, as
map { it * 10 }.filter { it % 2 == 1 }? - What does
println(nums)print after the chain, and why?