MCQs on Collections and Basics of Immutability | Scala

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

  1. 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
  2. How is an empty List defined in Scala?
    • A) List()
    • B) new List
    • C) List.empty
    • D) Both A and C
  3. 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
  4. 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)
  5. 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
  6. 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
  7. What is the maximum number of elements allowed in a Tuple in Scala?
    • A) 10
    • B) 15
    • C) 22
    • D) 25
  8. Which Scala collection is ordered and immutable by default?
    • A) List
    • B) Set
    • C) Map
    • D) Tuple
  9. 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
  10. Which method checks if a key exists in a Map?
    • A) containsKey
    • B) exists
    • C) keyExists
    • D) contains

2. Immutability and Immutables

  1. 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
  2. Which of these is an immutable collection in Scala?
    • A) Array
    • B) MutableList
    • C) List
    • D) Buffer
  3. 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)
  4. 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
  5. What is the advantage of immutability in collections?
    • A) Easier debugging
    • B) Thread safety
    • C) Predictable behavior
    • D) All of the above
  6. Which of the following operations is not allowed on an immutable List?
    • A) Adding elements
    • B) Accessing elements
    • C) Traversing elements
    • D) Mapping elements
  7. How do you convert a mutable collection to immutable in Scala?
    • A) collection.toImmutable
    • B) collection.toSeq
    • C) collection.toList
    • D) collection.to[immutable]
  8. 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
  9. 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
  10. 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

  1. 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
  2. Which method is used to combine two collections in Scala?
    • A) merge
    • B) concat
    • C) ++
    • D) combine
  3. How do you filter elements in a collection?
    • A) filter(predicate)
    • B) select(predicate)
    • C) reduce(predicate)
    • D) extract(predicate)
  4. 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
  5. Which method is used to find the first element of a collection?
    • A) first()
    • B) get(0)
    • C) head
    • D) start
  6. How do you check if a collection is empty?
    • A) isEmpty
    • B) size == 0
    • C) length == 0
    • D) All of the above
  7. 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
  8. Which operation is used to transform a collection into a single result?
    • A) map
    • B) reduce
    • C) transform
    • D) convert
  9. How do you reverse a collection?
    • A) reverse()
    • B) collection.reverse
    • C) collection.invert
    • D) collection.flip
  10. Which method sorts a collection in ascending order?
    • A) sort()
    • B) sorted
    • C) sortAsc
    • D) arrange

Answers

QnoAnswer
1B) It maintains insertion order
2D) Both A and C
3C) It contains unique elements
4A) Map(key -> value)
5B) A sequence of immutable elements
6C) Using .index notation (e.g., .1, .2)
7C) 22
8A) List
9B) Returns None
10D) contains
11B) Elements cannot be added, modified, or removed
12C) List
13A) Set(1, 2, 3)
14C) Yes, but it creates a new instance
15D) All of the above
16A) Adding elements
17C) collection.toList
18A) They ensure side effects are minimized
19B) A new collection instance
20A) Yes, they are inherently thread-safe
21B) Applies a function to each element and returns a new collection
22C) ++
23A) filter(predicate)
24A) Combines elements using an associative operation
25C) head
26D) All of the above
27B) Combines elements into a single value
28B) reduce
29B) collection.reverse
30B) sorted

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