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