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
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
What method is used to access an element in a List? a) get() b) put() c) add() d) find()
In Kotlin, which type of Set ensures elements are stored in insertion order? a) HashSet b) LinkedHashSet c) TreeSet d) MutableSet
What distinguishes a Map from other Kotlin collections? a) Ordered elements b) Key-value pairs c) Unique elements d) Mutable elements
Which function retrieves all the keys from a Kotlin Map? a) mapKeys() b) keys c) getKeys() d) keySet()
What is the default implementation of Map in Kotlin? a) HashMap b) TreeMap c) LinkedHashMap d) MutableMap
2. Mutable vs Immutable Collections
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.
Which keyword is used to declare immutable collections in Kotlin? a) mutableOf b) listOf c) setOf d) varOf
How can you convert a mutable List into an immutable one? a) toImmutable() b) toList() c) asImmutable() d) asList()
Which of the following is not a mutable collection in Kotlin? a) ArrayList b) HashSet c) listOf d) HashMap
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.
Which Kotlin collection supports both mutable and immutable versions? a) List b) Map c) Set d) All of the above
3. Common Collection Operations
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
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
Which function checks if all elements in a collection satisfy a condition? a) any() b) all() c) contains() d) none()
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.
Which operation would you use to merge two collections into a single list? a) filter() b) union() c) plus() d) combine()
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.
Which function removes duplicate elements from a collection? a) distinct() b) filter() c) unique() d) deduplicate()
How do you find the largest element in a numeric List? a) maxBy() b) maxOf() c) max() d) largest()
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.
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.
Which operation sorts a collection by natural order? a) sort() b) sorted() c) order() d) arrange()
What function would you use to check if a collection is empty? a) isNotEmpty() b) count() == 0 c) isEmpty() d) none()
Which function converts a collection to a Set? a) toSet() b) asSet() c) convertToSet() d) setOf()
The groupBy function organizes elements into: a) Nested collections based on a key. b) Sorted collections. c) Immutable collections. d) Unique groups without duplicates.
To retrieve the last element of a List, you can use: a) last() b) getLast() c) tail() d) findLast()
Which method combines filtering and mapping in one step? a) filter() b) filterMap() c) map() d) mapNotNull()
How do you reverse the order of a collection? a) reverseOrder() b) reversed() c) invert() d) swap()
Which function randomly shuffles elements in a collection? a) shuffle() b) randomize() c) randomOrder() d) mixed()
Answers Table
QNo
Answer (Option with Text)
1
b) A collection with ordered elements
2
a) get()
3
b) LinkedHashSet
4
b) Key-value pairs
5
b) keys
6
c) LinkedHashMap
7
b) It throws an error
8
c) setOf
9
b) toList()
10
c) listOf
11
b) No, it throws an error
12
d) All of the above
13
b) A new collection with elements matching the predicate
14
b) Transform each element in a collection
15
b) all()
16
b) It flattens nested collections after mapping
17
c) plus()
18
c) Performs an action on each element
19
a) distinct()
20
b) maxOf()
21
b) Combines elements into a single value
22
a) Two parts, matching and non-matching a predicate