Dive deep into Scala collections such as Lists, Sets, Maps, and Tuples. Understand the essence of immutability and learn essential operations on collections with this comprehensive MCQ set.
Collections and Basics of Immutability in Scala
1. Lists, Sets, Maps, and Tuples
What is the key characteristic of a List in Scala?
A) It is mutable
B) It maintains insertion order
C) It does not allow duplicate elements
D) It is unordered
How is an empty List defined in Scala?
A) List()
B) new List
C) List.empty
D) Both A and C
What distinguishes a Set in Scala?
A) It allows duplicate elements
B) It automatically sorts elements
C) It contains unique elements
D) It has a fixed size
How do you create a Map in Scala?
A) Map(key -> value)
B) new Map(key = value)
C) Map{key, value}
D) Map(key = value)
What is a Tuple in Scala?
A) A mutable collection
B) A sequence of immutable elements
C) A special kind of List
D) A key-value pair
How do you access elements in a Tuple?
A) Using square brackets
B) Using tupleName(index)
C) Using .index notation (e.g., .1, .2)
D) Using the get() method
What is the maximum number of elements allowed in a Tuple in Scala?
A) 10
B) 15
C) 22
D) 25
Which Scala collection is ordered and immutable by default?
A) List
B) Set
C) Map
D) Tuple
What happens when you try to access a non-existent key in a Map?
A) Throws a NullPointerException
B) Returns None
C) Returns null
D) Throws a NoSuchElementException
Which method checks if a key exists in a Map?
A) containsKey
B) exists
C) keyExists
D) contains
2. Immutability and Immutables
What does immutability mean in Scala collections?
A) Collections cannot grow or shrink
B) Elements cannot be added, modified, or removed
C) Collections cannot be sorted
D) Collections cannot hold duplicate elements
Which of these is an immutable collection in Scala?
A) Array
B) MutableList
C) List
D) Buffer
How do you create an immutable Set?
A) Set(1, 2, 3)
B) new immutable.Set(1, 2, 3)
C) ImmutableSet(1, 2, 3)
D) Set.immutable(1, 2, 3)
Can immutable collections in Scala be updated?
A) Yes, by modifying them directly
B) No, they are permanently fixed
C) Yes, but it creates a new instance
D) No, unless they are converted to mutable
What is the advantage of immutability in collections?
A) Easier debugging
B) Thread safety
C) Predictable behavior
D) All of the above
Which of the following operations is not allowed on an immutable List?
A) Adding elements
B) Accessing elements
C) Traversing elements
D) Mapping elements
How do you convert a mutable collection to immutable in Scala?
A) collection.toImmutable
B) collection.toSeq
C) collection.toList
D) collection.to[immutable]
Why are immutable collections preferred in functional programming?
A) They ensure side effects are minimized
B) They are faster than mutable collections
C) They consume less memory
D) They allow in-place modifications
What is the return type when updating an immutable collection?
A) The same collection instance
B) A new collection instance
C) A Boolean indicating success
D) A null object
Can immutable collections be used with concurrent programming?
A) Yes, they are inherently thread-safe
B) No, they are not supported in concurrency
C) Only when synchronized manually
D) Yes, but only with shared locks
3. Basic Operations on Collections
What does the map function do in Scala collections?
A) Modifies the collection in place
B) Applies a function to each element and returns a new collection
C) Maps keys to values in a Map
D) Rearranges elements
Which method is used to combine two collections in Scala?
A) merge
B) concat
C) ++
D) combine
How do you filter elements in a collection?
A) filter(predicate)
B) select(predicate)
C) reduce(predicate)
D) extract(predicate)
What does the fold function do?
A) Combines elements using an associative operation
B) Filters elements based on a condition
C) Converts a collection to a string
D) Reverses the collection
Which method is used to find the first element of a collection?
A) first()
B) get(0)
C) head
D) start
How do you check if a collection is empty?
A) isEmpty
B) size == 0
C) length == 0
D) All of the above
What does the reduce function do in a collection?
A) Finds the smallest element
B) Combines elements into a single value
C) Filters elements based on a condition
D) Converts the collection to another type
Which operation is used to transform a collection into a single result?
A) map
B) reduce
C) transform
D) convert
How do you reverse a collection?
A) reverse()
B) collection.reverse
C) collection.invert
D) collection.flip
Which method sorts a collection in ascending order?
A) sort()
B) sorted
C) sortAsc
D) arrange
Answers
Qno
Answer
1
B) It maintains insertion order
2
D) Both A and C
3
C) It contains unique elements
4
A) Map(key -> value)
5
B) A sequence of immutable elements
6
C) Using .index notation (e.g., .1, .2)
7
C) 22
8
A) List
9
B) Returns None
10
D) contains
11
B) Elements cannot be added, modified, or removed
12
C) List
13
A) Set(1, 2, 3)
14
C) Yes, but it creates a new instance
15
D) All of the above
16
A) Adding elements
17
C) collection.toList
18
A) They ensure side effects are minimized
19
B) A new collection instance
20
A) Yes, they are inherently thread-safe
21
B) Applies a function to each element and returns a new collection
22
C) ++
23
A) filter(predicate)
24
A) Combines elements using an associative operation