MCQs on Advanced Functions | Haskell

Haskell programming is a powerful functional language that emphasizes immutability, pure functions, and lazy evaluation. It is known for its advanced features like fold, map, filter, monads, and others that help in building efficient and modular software systems. This collection of 30 multiple-choice questions (MCQs) will explore key concepts such as lazy evaluation, fold, map, filter, scan, and the use of various monads, including State, IO, Maybe, and Either.

1. Lazy evaluation in Haskell

  1. What does “lazy evaluation” mean in Haskell?
    a) Evaluating expressions when they are first used
    b) Evaluating all expressions upfront
    c) Storing results of all computations for later use
    d) Evaluating expressions sequentially
  2. Which of the following is a direct benefit of lazy evaluation in Haskell?
    a) Improved performance through memoization
    b) Automatic garbage collection
    c) Memory leak prevention
    d) Guaranteed termination of all programs
  3. What does the seq function in Haskell do in relation to lazy evaluation?
    a) Forces strict evaluation of an expression
    b) Converts lazy evaluation to eager evaluation
    c) Delays the evaluation of an expression
    d) Forces infinite recursion
  4. Which of the following scenarios is a good candidate for lazy evaluation?
    a) Processing an infinite data structure
    b) Sorting a fixed-size list
    c) Computing the factorial of a number
    d) Recursively summing a known finite list
  5. What is the primary reason for lazy evaluation being advantageous in Haskell?
    a) It allows for non-termination in programs
    b) It reduces memory usage by avoiding unnecessary computation
    c) It guarantees faster execution
    d) It helps with better type inference

2. Working with Fold, Map, Filter, and Scan

  1. The foldr function in Haskell is used to:
    a) Combine a list of elements from the right
    b) Filter elements of a list
    c) Map a function to each element
    d) Reverse a list of elements
  2. In Haskell, the map function is used for:
    a) Summing elements of a list
    b) Applying a function to each element of a list
    c) Filtering elements based on a condition
    d) Folding a list into a single value
  3. What is the primary difference between map and filter in Haskell?
    a) map transforms each element, filter removes elements based on a condition
    b) filter transforms elements, map removes elements
    c) map returns a boolean result, filter transforms elements
    d) map applies a function to lists, filter applies a function to numbers
  4. Which of the following Haskell functions is used to generate a list of cumulative results?
    a) foldl
    b) scanl
    c) map
    d) filter
  5. Which of the following is true about foldl and foldr in Haskell?
    a) foldl works from the left, while foldr works from the right
    b) foldr works from the left, while foldl works from the right
    c) foldl and foldr are identical
    d) foldr cannot be used with infinite lists

3. Monads and Their Applications

  1. What is a Monad in Haskell?
    a) A function that performs computations on values
    b) A data type with a specific structure used for sequencing operations
    c) A special kind of operator used for handling IO
    d) A programming language feature that prevents side effects
  2. The bind operation (>>=) in a Monad is used to:
    a) Combine two monadic values into one
    b) Map a function over a monadic value
    c) Sequence monadic operations
    d) Create a new monadic type
  3. Which of the following is a common use case for the Maybe Monad in Haskell?
    a) Handling input/output operations
    b) Handling optional values or computations that may fail
    c) Managing state in a computation
    d) Representing mutable variables
  4. The IO Monad in Haskell is used to:
    a) Handle side effects such as input/output operations
    b) Create immutable data
    c) Manage concurrency in programs
    d) Evaluate lazy expressions
  5. What is the primary purpose of the State Monad in Haskell?
    a) To handle side effects in functional programming
    b) To represent computations that produce side effects
    c) To handle mutable state in a purely functional way
    d) To manage input/output operations
  6. What does the Either Monad represent in Haskell?
    a) A computation that can return two types of results: success or failure
    b) A monadic operation that returns either a string or an integer
    c) A function that transforms one value into another
    d) A sequence of computations
  7. The Just constructor in the Maybe Monad represents:
    a) A failure result
    b) A computation that has successfully returned a value
    c) A null value
    d) An IO operation
  8. In which scenario would you use the IO Monad in Haskell?
    a) To perform mathematical calculations
    b) To generate random numbers
    c) To interact with files or console input/output
    d) To manage lists of data
  9. Which Monad is used to handle computations that might fail without causing errors in Haskell?
    a) Maybe
    b) State
    c) IO
    d) List
  10. What is the main benefit of using monads in Haskell?
    a) Simplifies handling side effects and managing state
    b) Improves the performance of all programs
    c) Reduces the need for recursion
    d) Guarantees faster execution of IO operations

4. Working with State, IO, Maybe, Either, and other Monads

  1. Which monad is often used in Haskell to simulate a computation with a global state?
    a) Maybe
    b) State
    c) IO
    d) List
  2. What does the return function do in a Monad in Haskell?
    a) Encapsulates a value into a monadic context
    b) Returns the final value of a computation
    c) Forces a strict evaluation
    d) Binds two monadic values together
  3. In the context of the State Monad, what does the state refer to?
    a) A value that is shared across computations
    b) A value that remains constant throughout the computation
    c) An input/output operation
    d) A list of computations
  4. In the Maybe Monad, the value Nothing represents:
    a) A computation that has failed
    b) A valid value
    c) An IO operation
    d) A stateful computation
  5. Which monad in Haskell is primarily used to handle errors or exceptional cases?
    a) Maybe
    b) State
    c) Either
    d) IO
  6. What is the purpose of the mapM function in Haskell?
    a) To map a monadic function over a list
    b) To fold a list of monadic values
    c) To apply a function over a list of values
    d) To handle exception-based computations
  7. In Haskell, the >> operator is used in monads for:
    a) Binding a function to a monadic value
    b) Sequencing monadic operations while ignoring the result
    c) Mapping a function over a list
    d) Returning a value from a monad
  8. What is the result of applying the foldl function to a list of values?
    a) A list of intermediate results
    b) A single value obtained by applying a function to the list
    c) A reversed version of the list
    d) A list of the elements sorted
  9. What is the main difference between foldl and foldr?
    a) foldl processes the list from left to right, while foldr processes it from right to left
    b) foldr works with infinite lists, while foldl does not
    c) foldl is used with lazy evaluation, while foldr is not
    d) foldr reduces a list to a single value, while foldl returns a new list
  10. How do monads like Maybe and Either enhance functional programming in Haskell?
    a) They allow for handling side effects in a pure functional style
    b) They improve the performance of programs
    c) They eliminate the need for recursion
    d) They guarantee deterministic behavior in programs

Answer Key (Tabular Form)

QnoAnswer
1a) Evaluating expressions when they are first used
2a) Improved performance through memoization
3a) Forces strict evaluation of an expression
4a) Processing an infinite data structure
5b) It reduces memory usage by avoiding unnecessary computation
6a) Combine a list of elements from the right
7b) Applying a function to each element of a list
8a) map transforms each element, filter removes elements based on a condition
9b) scanl
10a) foldl works from the left, while foldr works from the right
11b) A data type with a specific structure used for sequencing operations
12c) Sequence monadic operations
13b) Handling optional values or computations that may fail
14a) Handle side effects such as input/output operations
15c) To handle mutable state in a purely functional way
16a) A computation that can return two types of results: success or failure
17b) A computation that has successfully returned a value
18c) To interact with files or console input/output
19a) Maybe
20a) Simplifies handling side effects and managing state
21b) State
22a) Encapsulates a value into a monadic context
23a) A value that is shared across computations
24a) A computation that has failed
25c) Either
26a) To map a monadic function over a list
27b) Sequencing monadic operations while ignoring the result
28b) A single value obtained by applying a function to the list
29a) foldl processes the list from left to right, while foldr processes it from right to left
30a) They allow for handling side effects in a pure functional style

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