MCQs on Collections | Kotlin

Kotlin Collections, including Lists, Sets, and Maps, simplify data manipulation. Understanding mutable vs. immutable collections and mastering common operations like filter and map is crucial for efficient Kotlin programming.


Topics and MCQs

1. Lists, Sets, and Maps

  1. Which of the following defines a Kotlin List?
    a) A collection with unique elements
    b) A collection with ordered elements
    c) A key-value paired collection
    d) A mutable collection
  2. What method is used to access an element in a List?
    a) get()
    b) put()
    c) add()
    d) find()
  3. In Kotlin, which type of Set ensures elements are stored in insertion order?
    a) HashSet
    b) LinkedHashSet
    c) TreeSet
    d) MutableSet
  4. What distinguishes a Map from other Kotlin collections?
    a) Ordered elements
    b) Key-value pairs
    c) Unique elements
    d) Mutable elements
  5. Which function retrieves all the keys from a Kotlin Map?
    a) mapKeys()
    b) keys
    c) getKeys()
    d) keySet()
  6. What is the default implementation of Map in Kotlin?
    a) HashMap
    b) TreeMap
    c) LinkedHashMap
    d) MutableMap

2. Mutable vs Immutable Collections

  1. What happens when you attempt to modify an immutable List?
    a) It is updated successfully.
    b) It throws an error.
    c) A new list is created with the modification.
    d) The modification is ignored silently.
  2. Which keyword is used to declare immutable collections in Kotlin?
    a) mutableOf
    b) listOf
    c) setOf
    d) varOf
  3. How can you convert a mutable List into an immutable one?
    a) toImmutable()
    b) toList()
    c) asImmutable()
    d) asList()
  4. Which of the following is not a mutable collection in Kotlin?
    a) ArrayList
    b) HashSet
    c) listOf
    d) HashMap
  5. Can an immutable Map be modified?
    a) Yes, directly.
    b) No, it throws an error.
    c) Yes, by using a function like add().
    d) Only if converted to mutable.
  6. Which Kotlin collection supports both mutable and immutable versions?
    a) List
    b) Map
    c) Set
    d) All of the above

3. Common Collection Operations

  1. What does the filter function return?
    a) A filtered view of the original collection
    b) A new collection with elements matching the predicate
    c) The same collection without modification
    d) An immutable version of the collection
  2. The map function is used to:
    a) Filter elements from a collection
    b) Transform each element in a collection
    c) Retrieve key-value pairs
    d) Remove elements from a collection
  3. Which function checks if all elements in a collection satisfy a condition?
    a) any()
    b) all()
    c) contains()
    d) none()
  4. How does the flatMap function differ from map?
    a) It combines map and filter.
    b) It flattens nested collections after mapping.
    c) It filters and maps elements.
    d) It does not differ.
  5. Which operation would you use to merge two collections into a single list?
    a) filter()
    b) union()
    c) plus()
    d) combine()
  6. What is the purpose of the forEach function in Kotlin?
    a) Iterates and transforms elements.
    b) Filters elements based on a condition.
    c) Performs an action on each element.
    d) Combines multiple collections.
  7. Which function removes duplicate elements from a collection?
    a) distinct()
    b) filter()
    c) unique()
    d) deduplicate()
  8. How do you find the largest element in a numeric List?
    a) maxBy()
    b) maxOf()
    c) max()
    d) largest()
  9. What does the reduce function do in Kotlin?
    a) Filters elements based on a condition.
    b) Combines elements into a single value.
    c) Sorts the collection.
    d) Converts a list to a set.
  10. The partition function divides a collection into:
    a) Two parts, matching and non-matching a predicate.
    b) Equal-sized collections.
    c) Immutable and mutable collections.
    d) Keys and values.
  11. Which operation sorts a collection by natural order?
    a) sort()
    b) sorted()
    c) order()
    d) arrange()
  12. What function would you use to check if a collection is empty?
    a) isNotEmpty()
    b) count() == 0
    c) isEmpty()
    d) none()
  13. Which function converts a collection to a Set?
    a) toSet()
    b) asSet()
    c) convertToSet()
    d) setOf()
  14. The groupBy function organizes elements into:
    a) Nested collections based on a key.
    b) Sorted collections.
    c) Immutable collections.
    d) Unique groups without duplicates.
  15. To retrieve the last element of a List, you can use:
    a) last()
    b) getLast()
    c) tail()
    d) findLast()
  16. Which method combines filtering and mapping in one step?
    a) filter()
    b) filterMap()
    c) map()
    d) mapNotNull()
  17. How do you reverse the order of a collection?
    a) reverseOrder()
    b) reversed()
    c) invert()
    d) swap()
  18. Which function randomly shuffles elements in a collection?
    a) shuffle()
    b) randomize()
    c) randomOrder()
    d) mixed()

Answers Table

QNoAnswer (Option with Text)
1b) A collection with ordered elements
2a) get()
3b) LinkedHashSet
4b) Key-value pairs
5b) keys
6c) LinkedHashMap
7b) It throws an error
8c) setOf
9b) toList()
10c) listOf
11b) No, it throws an error
12d) All of the above
13b) A new collection with elements matching the predicate
14b) Transform each element in a collection
15b) all()
16b) It flattens nested collections after mapping
17c) plus()
18c) Performs an action on each element
19a) distinct()
20b) maxOf()
21b) Combines elements into a single value
22a) Two parts, matching and non-matching a predicate
23b) sorted()
24c) isEmpty()
25a) toSet()
26a) Nested collections based on a key
27a) last()
28d) mapNotNull()
29b) reversed()
30a) shuffle()

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top