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