MCQs on Advanced Functional Programming | Scala

Master advanced functional programming concepts in Scala, including Monads, Applicatives, Functors, and libraries like Cats and Scalaz. These 30 MCQs will deepen your understanding of functional programming in Scala.


Advanced Functional Programming in Scala

1. Monads and Their Use Cases (Option, Either, Future)

  1. Which of the following is a Monad in Scala?
    • A) Option
    • B) Future
    • C) Either
    • D) All of the above
  2. What is the main advantage of using a Monad in Scala?
    • A) To handle side-effects
    • B) To encapsulate computations that may fail
    • C) To chain computations together
    • D) All of the above
  3. In Scala, which Monad can represent a computation that may fail with an error?
    • A) Option
    • B) Either
    • C) Future
    • D) List
  4. What does the map method do in a Monad?
    • A) Applies a function to the value inside the Monad
    • B) Creates a new Monad with the same value
    • C) Unwraps the value from the Monad
    • D) None of the above
  5. How do you handle errors in the Either Monad in Scala?
    • A) By using the map method
    • B) By using the flatMap method
    • C) By checking the Left value
    • D) By using recover()
  6. What type of computation does a Future Monad represent in Scala?
    • A) A computation that will complete in the future
    • B) A computation that may throw an exception
    • C) A computation that runs synchronously
    • D) A computation that is already complete
  7. What is the result of using Option.empty in Scala?
    • A) A Some value
    • B) An empty Option
    • C) A None value
    • D) An error
  8. Which Monad is used to represent a computation that could fail with one of several possible errors?
    • A) Option
    • B) Either
    • C) Future
    • D) Try
  9. How do you chain computations in a Future Monad?
    • A) Using flatMap
    • B) Using map
    • C) Using for-comprehensions
    • D) Both A and C
  10. Which of the following best describes the Option Monad in Scala?
    • A) A wrapper around a value that could be missing
    • B) A wrapper around a value that could be an error
    • C) A container for multiple values
    • D) A wrapper for a computation that will return in the future

2. Applicatives and Functors

  1. What does a Functor in functional programming allow you to do?
    • A) Map over values contained within a context
    • B) Apply multiple functions in parallel
    • C) Chain multiple computations together
    • D) All of the above
  2. Which of the following is true about Functors?
    • A) They must implement a map method
    • B) They must implement a flatMap method
    • C) They must have a filter method
    • D) None of the above
  3. What is the key difference between a Functor and an Applicative?
    • A) Applicatives support operations that combine contexts, Functors do not
    • B) Functors support operations that combine contexts, Applicatives do not
    • C) Applicatives can only work with monads
    • D) There is no difference between them
  4. In Scala, which of the following is an example of an Applicative?
    • A) Option
    • B) Future
    • C) Either
    • D) All of the above
  5. Which method is typically used by Applicatives to apply a function wrapped inside a context to a value inside a context?
    • A) map
    • B) apply
    • C) flatMap
    • D) combine
  6. Which of the following is a key feature of an Applicative functor in functional programming?
    • A) It allows for combining multiple values inside contexts
    • B) It allows for monadic chaining
    • C) It supports higher-order functions
    • D) It is only used for error handling
  7. Which of the following libraries in Scala provides support for Applicatives and Functors?
    • A) Cats
    • B) Scalaz
    • C) Both A and B
    • D) Akka
  8. What does the ap method do in an Applicative?
    • A) It applies a function inside an Applicative to a value inside another Applicative
    • B) It combines two values inside an Applicative
    • C) It converts a value inside an Applicative to a flat value
    • D) It maps a value inside an Applicative
  9. Which of the following is a key property of an Applicative?
    • A) It must support function application within a context
    • B) It must support both map and flatMap
    • C) It must support only synchronous operations
    • D) It can only handle errors
  10. What does the pure method in an Applicative do?
    • A) It lifts a value into a context
    • B) It applies a function to a value inside a context
    • C) It chains operations together
    • D) It combines two contexts

3. Understanding Cats/Scalaz Libraries

  1. What is the main purpose of the Cats library in Scala?
    • A) To provide abstractions for functional programming
    • B) To handle concurrency
    • C) To manage HTTP requests
    • D) To interact with databases
  2. In which of the following does the Cats library provide support for functional programming?
    • A) Functors
    • B) Monads
    • C) Applicatives
    • D) All of the above
  3. Which of the following is a feature of the Scalaz library in Scala?
    • A) Advanced functional programming abstractions
    • B) Concurrency utilities
    • C) HTTP client and server support
    • D) File I/O operations
  4. How does the Cats library differ from Scalaz?
    • A) Cats is simpler and more modular
    • B) Scalaz is faster
    • C) Cats is not used for functional programming
    • D) Scalaz supports only monads
  5. What type of data structure does Cats use to represent immutable collections?
    • A) List
    • B) Queue
    • C) Vector
    • D) Set
  6. Which of the following is a key feature of Cats’s Functor?
    • A) It implements map for transforming values inside contexts
    • B) It supports both map and flatMap
    • C) It allows applying multiple functions inside a context
    • D) None of the above
  7. What is the role of Scalaz’s Validation data structure?
    • A) To collect errors in a context-aware way
    • B) To validate inputs and handle errors
    • C) To chain computations with error handling
    • D) All of the above
  8. In Cats, which method is used to lift a value into a Functor?
    • A) pure
    • B) map
    • C) flatMap
    • D) apply
  9. Which of the following is true about Cats‘s Monoid?
    • A) It provides a combination function for two values of the same type
    • B) It provides a method for serializing values
    • C) It handles exception handling
    • D) It handles parallel computations
  10. Which of the following is a feature of Scalaz’s Either type?
    • A) It can represent two possibilities: a value or an error
    • B) It supports flatMap and map for chaining operations
    • C) It is commonly used for error handling
    • D) All of the above

Answer Key

QnoAnswer (Option with the text)
1D) All of the above
2D) All of the above
3B) Either
4A) Applies a function to the value inside the Monad
5C) By checking the Left value
6A) A computation that will complete in the future
7C) A None value
8B) Either
9D) Both A and C
10A) A wrapper around a value that could be missing
11A) Map over values contained within a context
12A) They must implement a map method
13A) Applicatives support operations that combine contexts, Functors do not
14D) All of the above
15B) apply
16A) It allows for combining multiple values inside contexts
17C) Both A and B
18A) It applies a function inside an Applicative to a value inside another Applicative
19A) It must support function application within a context
20A) It lifts a value into a context
21A) To provide abstractions for functional programming
22D) All of the above
23A) Advanced functional programming abstractions
24A) Cats is simpler and more modular
25C) Vector
26A) It implements map for transforming values inside contexts
27D) All of the above
28A) pure
29A) It provides a combination function for two values of the same type
30D) All of the above

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