Collections Questions with Answers

27 practice questions on Collections (Kotlin) with answers and step-by-step solutions. Try 10 samples below, then practise all 27 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

println(listOf(1, 2, 3).map { it * 2 })
  1. [1, 2, 3]
  2. [2, 4, 6]
  3. [2, 4]
  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

  1. Each element doubled.
  2. Collected in a new list.
  3. [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?

  1. Throws NoSuchElementException
  2. Returns null
  3. Returns 0
  4. Returns an empty list

Answer: A. Throws NoSuchElementException

Explanation

first() throws on an empty collection. Use firstOrNull() for null instead.

Step-by-step solution

  1. List is empty.
  2. first() needs an element.
  3. 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?

  1. listOf(1, 2, 3)
  2. mutableListOf(1, 2, 3)
  3. arrayListOf(1, 2, 3)
  4. 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

  1. ListOf creates a read-only list, so it cannot be changed after it is made.
  2. MutableListOf and arrayListOf give mutable lists, and List.new is not Kotlin syntax.
  3. 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)
  1. 3
  2. 4
  3. 2
  4. Error

Answer: A. 3

Explanation

A Set stores unique elements, so {1, 2, 3} has size 3.

Step-by-step solution

  1. Duplicates collapse.
  2. Elements 1, 2, 3.
  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. [[1, 2], [3, 4]]
  2. [[1, 2, 3], [2, 3, 4]]
  3. [[1, 2], [2, 3], [3, 4]]
  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

  1. Window size 2.
  2. Step defaults to 1.
  3. 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. [[1, 2, 3, 4]]
  2. [[1, 2, 3, 4], [5, 6]]
  3. [[1, 2], [3, 4], [5, 6]]
  4. [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

  1. Size 4 chunk: 1..4.
  2. Remaining 5, 6.
  3. 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 })
  1. [fig, kiwi, apple]
  2. [apple, fig, kiwi]
  3. [apple, kiwi, fig]
  4. [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

  1. Compute lengths.
  2. Sort ascending.
  3. [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" })
  1. [a0, b1]
  2. [a, b]
  3. [0, 1]
  4. [0a, 1b]

Answer: D. [0a, 1b]

Explanation

mapIndexed passes index then element; the template joins them as 0a and 1b.

Step-by-step solution

  1. i=0, s=a -> "0a".
  2. i=1, s=b -> "1b".
  3. 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())
  1. {a=1, b=1}
  2. [a, b, a]
  3. {a, b}
  4. {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

  1. Group by the string.
  2. a appears twice, b once.
  3. {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)
  1. m1 m2 m3 m4 4
  2. m1 m2 4
  3. m1 m2 m3 m4 8
  4. 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

  1. AsSequence is lazy, so each element goes through map one at a time, only when needed.
  2. 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.
  3. 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 →

Related topics

See all Kotlin topics · All topics