MCQs on Advanced Collections | Scala

Dive into advanced Scala collections! This set of 30 MCQs covers higher-order functions like map, flatMap, filter, reduce, as well as working with Sequences, Streams, Views, Option, and Either.


Advanced Collections in Scala

1. Higher-order Functions: map, flatMap, filter, reduce

  1. What does the map function do in Scala collections?
    • A) It filters the elements based on a predicate
    • B) It transforms each element using a function
    • C) It reduces the elements to a single value
    • D) It flattens nested collections
  2. Which of the following functions is used to apply a transformation and flatten the result in Scala?
    • A) map
    • B) filter
    • C) flatMap
    • D) reduce
  3. The filter function in Scala collections is used to:
    • A) Map each element to a new value
    • B) Apply a predicate to retain elements
    • C) Reduce the collection to a single value
    • D) Flatten nested collections
  4. Which function is used in Scala to reduce a collection to a single value?
    • A) map
    • B) flatMap
    • C) reduce
    • D) filter
  5. What is the result of using map on a list of integers to square each element?
    • A) A list of squared integers
    • B) A single integer representing the sum
    • C) A filtered list with odd numbers
    • D) A list of tuples
  6. Which function combines each element of a collection into a single value by applying an accumulator in Scala?
    • A) map
    • B) flatMap
    • C) filter
    • D) reduce
  7. What is the primary difference between map and flatMap in Scala collections?
    • A) map does not flatten the result, but flatMap does
    • B) flatMap maps elements without transformation
    • C) map is used for filtering
    • D) flatMap reduces the collection
  8. What type of result does filter produce in Scala?
    • A) A new collection with only selected elements
    • B) A single element
    • C) A collection of transformed elements
    • D) A flattened collection
  9. Which of the following is an example of using flatMap in Scala?
    • A) list.map(x => x * 2)
    • B) list.filter(x => x > 5)
    • C) list.flatMap(x => List(x, x + 1))
    • D) list.reduce((x, y) => x + y)
  10. In the context of higher-order functions, which of the following statements is true?
    • A) map, filter, and reduce are higher-order functions
    • B) Only map is a higher-order function
    • C) flatMap and reduce are not higher-order functions
    • D) None of the above

2. Sequences, Streams, and Views

  1. What is a Sequence in Scala?
    • A) A collection with a predefined order of elements
    • B) A type of set
    • C) A random access collection
    • D) A collection that cannot be modified
  2. How does a Stream differ from a List in Scala?
    • A) A Stream is evaluated lazily, while a List is strict
    • B) A Stream is mutable, but a List is immutable
    • C) A Stream is faster than a List
    • D) A List supports lazy evaluation, but a Stream does not
  3. What does the view method in Scala collections do?
    • A) It forces immediate evaluation of the collection
    • B) It provides a lazy view of a collection without computing it entirely
    • C) It converts the collection into a set
    • D) It flattens the collection
  4. Which of the following statements is true about Stream in Scala?
    • A) It computes values only when they are accessed
    • B) It computes all elements eagerly
    • C) It is a mutable collection
    • D) It is an unordered collection
  5. What type of collection is a Vector in Scala?
    • A) Mutable sequence
    • B) Immutable sequence
    • C) Mutable set
    • D) Immutable set
  6. How can you convert a Stream into a strict collection in Scala?
    • A) By calling .toList or .toSeq
    • B) By calling .force
    • C) By calling .toSet
    • D) By using .lazyList
  7. What is a key advantage of using a View in Scala?
    • A) It provides fast random access to elements
    • B) It allows transformations to be applied lazily
    • C) It forces immediate evaluation of transformations
    • D) It allows updates to the original collection
  8. In which scenario is a Stream preferred over a List?
    • A) When you need to process large datasets lazily
    • B) When performance is not critical
    • C) When working with small, fixed-size collections
    • D) When you need to mutate data
  9. Which of the following is true about a View collection in Scala?
    • A) It is lazy and does not perform transformations until necessary
    • B) It eagerly computes the transformed collection
    • C) It is mutable
    • D) It is not suitable for large data sets
  10. What happens when you apply a transformation to a View?
    • A) It immediately computes the transformation
    • B) It stores the transformation and computes it only when accessed
    • C) It throws an exception
    • D) It forces a strict evaluation

3. Option and Either for Safe Operations

  1. What does the Option type represent in Scala?
    • A) A collection of optional values
    • B) A choice between two types
    • C) A type that can either hold a value or be empty
    • D) A sequence of possible values
  2. Which of the following are valid values for Option in Scala?
    • A) Some(value) and None
    • B) True and False
    • C) Null and NonNull
    • D) True and None
  3. What does the getOrElse method do in Scala’s Option?
    • A) Returns the value wrapped in Option or a default value if None
    • B) Returns the value only if it is Some
    • C) Returns an empty value when None
    • D) Forces the Option to be evaluated
  4. What is the use of Either in Scala?
    • A) To represent a value that can be of two types: success or failure
    • B) To represent a type that can be Some or None
    • C) To handle null values safely
    • D) To represent a collection with two possible values
  5. Which of the following is a valid instance of Either in Scala?
    • A) Left("error")
    • B) Right("success")
    • C) Left("error") or Right("success")
    • D) All of the above
  6. How does Option handle null values in Scala?
    • A) It wraps null values in Some
    • B) It can never contain null values
    • C) It wraps a null value in None
    • D) It throws an exception if null is encountered
  7. What does Either provide over Option in Scala?
    • A) It can handle only success cases
    • B) It provides more specific error handling with Left and Right
    • C) It forces immediate evaluation
    • D) It allows more flexible collection operations
  8. Which of the following functions can be used to transform values in Option in Scala?
    • A) map
    • B) flatMap
    • C) getOrElse
    • D) All of the above
  9. What is the best way to handle an empty value safely in Scala?
    • A) Use try-catch blocks
    • B) Use Option to represent the possibility of absence
    • C) Use Null values
    • D) Use assert() statements
  10. How do you combine two Option values in Scala?
    • A) Use map and flatMap methods
    • B) Use combine method
    • C) Use getOrElse
    • D) Use Either to combine

Answer Key

QnoAnswer (Option with the text)
1B) It transforms each element using a function
2C) flatMap
3B) It applies a predicate to retain elements
4C) reduce
5A) A list of squared integers
6D) reduce
7A) map does not flatten the result, but flatMap does
8A) A new collection with only selected elements
9C) list.flatMap(x => List(x, x + 1))
10A) map, filter, and reduce are higher-order functions
11A) A collection with a predefined order of elements
12A) A Stream is evaluated lazily, while a List is strict
13B) It provides a lazy view of a collection without computing it entirely
14A) It computes values only when they are accessed
15B) Immutable sequence
16B) By calling .force
17B) It allows transformations to be applied lazily
18A) When you need to process large datasets lazily
19A) It is lazy and does not perform transformations until necessary
20B) It provides a lazy view of a collection
21C) A type that can either hold a value or be empty
22A) Some(value) and None
23A) Returns the value wrapped in Option or a default value if None
24A) To represent a value that can be of two types: success or failure
25D) All of the above
26B) It can never contain null values
27B) It provides more specific error handling with Left and Right
28D) All of the above
29B) Use Option to represent the possibility of absence
30A) Use map and flatMap methods

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