Sample questions
Question 1Easy
What is the output?
println(listOf(1, 2, 3).map { it * 2 })
- [1, 2, 3]
- [2, 4, 6]
- [2, 4]
- 12
Answer: B. [2, 4, 6]
Explanation
map applies the lambda to each element and a list prints in brackets.
Step-by-step solution
- Each element doubled.
- Collected in a new list.
- [2, 4, 6].
Why the other options are wrong
- A. map returns a new transformed list.
- C. map keeps every element.
- D. println prints the list form.
Question 2Easy
What does emptyList<Int>().first() do?
- Throws NoSuchElementException
- Returns null
- Returns 0
- Returns an empty list
Answer: A. Throws NoSuchElementException
Explanation
first() throws on an empty collection. Use firstOrNull() for null instead.
Step-by-step solution
- List is empty.
- first() needs an element.
- It throws NoSuchElementException.
Why the other options are wrong
- B. That is firstOrNull().
- C. There is no default value.
- D. first() returns an element.
Tip: Prefer the *OrNull variants when emptiness is possible.
Question 3Easy
Which function creates an immutable list in Kotlin?
- listOf(1, 2, 3)
- mutableListOf(1, 2, 3)
- arrayListOf(1, 2, 3)
- List.new(1, 2, 3)
Answer: A. listOf(1, 2, 3)
Explanation
listOf returns a read-only List; the others return mutable lists or do not exist.
Step-by-step solution
- ListOf creates a read-only list, so it cannot be changed after it is made.
- MutableListOf and arrayListOf give mutable lists, and List.new is not Kotlin syntax.
- Marked answer: option A.
Why the other options are wrong
- B. MutableListOf creates a mutable list that can be changed.
- C. ArrayListOf creates a mutable ArrayList.
- D. List.new does not exist in Kotlin.
Tip: ListOf is read-only; mutableListOf can add and remove items.
Question 4Easy
What is the output?
println(setOf(1, 2, 2, 3).size)
- 3
- 4
- 2
- Error
Answer: A. 3
Explanation
A Set stores unique elements, so {1, 2, 3} has size 3.
Step-by-step solution
- Duplicates collapse.
- Elements 1, 2, 3.
- Size 3.
Why the other options are wrong
- B. A set removes duplicates.
- C. Only one duplicate value is removed.
- D. Duplicates are silently dropped.
Question 5Medium
What is the output?
println(listOf(1, 2, 3, 4).windowed(2))
- [[1, 2], [3, 4]]
- [[1, 2, 3], [2, 3, 4]]
- [[1, 2], [2, 3], [3, 4]]
- [3, 5, 7]
Answer: C. [[1, 2], [2, 3], [3, 4]]
Explanation
windowed(2) produces overlapping windows of size 2 moving by 1.
Step-by-step solution
- Window size 2.
- Step defaults to 1.
- Three windows.
Why the other options are wrong
- A. windowed slides by 1 by default.
- B. The window size is 2.
- D. windowed does not add.
Question 6Medium
What is the output?
println((1..6).chunked(4))
- [[1, 2, 3, 4]]
- [[1, 2, 3, 4], [5, 6]]
- [[1, 2], [3, 4], [5, 6]]
- [1, 2, 3, 4, 5, 6]
Answer: B. [[1, 2, 3, 4], [5, 6]]
Explanation
chunked(n) splits into lists of size n; the last may be smaller.
Step-by-step solution
- Size 4 chunk: 1..4.
- Remaining 5, 6.
- Two lists.
Why the other options are wrong
- A. The leftover forms a smaller chunk.
- C. Chunk size is 4, not 2.
- D. chunked groups the elements.
Question 7Medium
What is the output?
println(listOf("apple", "fig", "kiwi").sortedBy { it.length })
- [fig, kiwi, apple]
- [apple, fig, kiwi]
- [apple, kiwi, fig]
- [fig, apple, kiwi]
Answer: A. [fig, kiwi, apple]
Explanation
Lengths are 5, 3, 4, so ascending order gives fig, kiwi, apple.
Step-by-step solution
- Compute lengths.
- Sort ascending.
- [fig, kiwi, apple].
Why the other options are wrong
- B. Sorted by length, not alphabetically.
- C. Shortest comes first.
- D. kiwi (4) is shorter than apple (5).
Question 8Medium
What is the output?
println(listOf("a", "b").mapIndexed { i, s -> "$i$s" })
- [a0, b1]
- [a, b]
- [0, 1]
- [0a, 1b]
Answer: D. [0a, 1b]
Explanation
mapIndexed passes index then element; the template joins them as 0a and 1b.
Step-by-step solution
- i=0, s=a -> "0a".
- i=1, s=b -> "1b".
- List [0a, 1b].
Why the other options are wrong
- A. Index comes first in the template.
- B. The index is added.
- C. The letter is part of the string.
Question 9Hard
What is the output?
println(listOf("a", "b", "a").groupingBy { it }.eachCount())
- {a=1, b=1}
- [a, b, a]
- {a, b}
- {a=2, b=1}
Answer: D. {a=2, b=1}
Explanation
groupingBy{}.eachCount() counts how many items fall in each group.
Step-by-step solution
- Group by the string.
- a appears twice, b once.
- {a=2, b=1}.
Why the other options are wrong
- A. a appears twice.
- B. eachCount returns a map.
- C. Counts are included.
Question 10Hard
What is the output?
val r = listOf(1, 2, 3, 4).asSequence()
.map { print("m$it "); it * 2 }
.first { it > 2 }
println(r)
- m1 m2 m3 m4 4
- m1 m2 4
- m1 m2 m3 m4 8
- 4 only, with no map output
Answer: B. m1 m2 4
Explanation
Lazily, 1 maps to 2 (not >2), 2 maps to 4 (>2) so evaluation stops; prints "m1 m2 " then 4.
Step-by-step solution
- AsSequence is lazy, so each element goes through map one at a time, only when needed.
- 1 maps to 2 and prints m1; 2 maps to 4 and prints m2. 4 is greater than 2, so first returns 4 and stops.
- Output is m1 m2 4, the last 4 printed by println. Marked answer: option B.
Why the other options are wrong
- A. The sequence is lazy, so map does not run for 3 and 4 once first has found its match.
- C. First stops at the match, so 3 and 4 are never mapped and nothing prints for them.
- D. The map output is printed for elements 1 and 2 before the result appears.
Tip: Sequences are lazy: trace each element through the whole chain before moving on.
Practise all 27 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →