MCQs on Error Handling and Maybe Type | Haskell

Error handling in Haskell is elegant and type-safe, relying on the Maybe and Either types. The Maybe type helps handle optional values without exceptions, while Either offers detailed error messages. Learn how to use Just, Nothing, function composition, and Either for effective error management in Haskell programming.


1. Handling Errors Using Maybe Type

  1. What is the Maybe type used for in Haskell?
    • a) To represent a list of values
    • b) To handle exceptions
    • c) To represent a value that may or may not exist
    • d) To define recursive functions
  2. What are the two constructors of the Maybe type?
    • a) Error and Value
    • b) Just and Nothing
    • c) Success and Failure
    • d) Left and Right
  3. How can you explicitly handle a Maybe value in Haskell?
    • a) Using pattern matching
    • b) Using if-else
    • c) Using type casting
    • d) Using loops
  4. Which of the following represents a Maybe value containing data?
    • a) Nothing
    • b) Just value
    • c) Error value
    • d) Value
  5. What does the Nothing constructor indicate in a Maybe type?
    • a) A value exists but is invalid
    • b) A value does not exist
    • c) An error occurred during computation
    • d) A computation was successful
  6. Which function is used to safely extract the value from a Maybe type with a default?
    • a) fromMaybe
    • b) maybeDefault
    • c) extractMaybe
    • d) unwrapMaybe
  7. What happens when you try to use a Maybe value without handling it?
    • a) It raises a runtime error.
    • b) It results in a compile-time error.
    • c) The program continues but ignores the value.
    • d) The program halts.
  8. How is the Maybe type related to error handling?
    • a) It captures and logs errors automatically.
    • b) It ensures errors are handled explicitly by the programmer.
    • c) It prevents errors from occurring.
    • d) It is unrelated to error handling.

2. Understanding Nothing and Just Values

  1. What is the meaning of Just 5 in Haskell?
    • a) An error occurred with the value 5
    • b) A valid value 5 wrapped in Maybe
    • c) A computation that failed but returned 5
    • d) A placeholder for a future value
  2. How can you pattern match on a Maybe type?
  • a) if-else statements
  • b) Case expressions with Just and Nothing
  • c) Using the Either type
  • d) Using guards exclusively
  1. Which of the following is true about Nothing?
  • a) It represents an error with a specific message.
  • b) It is equivalent to null in other languages.
  • c) It indicates the absence of a value.
  • d) It can only be used in monads.
  1. How can you check if a Maybe value is Nothing?
  • a) Using isNothing
  • b) Using checkNothing
  • c) Using nothingCheck
  • d) Using pattern matching only
  1. What happens if you apply a function to a Nothing value?
  • a) The function is ignored, and Nothing is returned.
  • b) The program crashes.
  • c) A runtime error is raised.
  • d) The function is applied as if Nothing were 0.
  1. How can you transform a Just value?
  • a) Using map
  • b) Using apply
  • c) Using transform
  • d) Using maybe
  1. What does the isJust function return?
  • a) True if the value is Just, False otherwise
  • b) True if the value is Nothing, False otherwise
  • c) The value inside Just
  • d) A transformed version of Just

3. Using Maybe in Function Composition

  1. What is a common use case for the Maybe type in function composition?
  • a) To handle values that may cause runtime errors
  • b) To improve performance
  • c) To create higher-order functions
  • d) To avoid recursion
  1. How do you chain multiple Maybe values?
  • a) Using the >>= operator (bind)
  • b) Using the >> operator
  • c) Using a loop
  • d) Using the if-else construct
  1. Which function allows you to handle a Nothing case in a function chain?
  • a) fromMaybe
  • b) maybe
  • c) defaultMaybe
  • d) mapMaybe
  1. What does the maybe function do?
  • a) Applies a function to a Maybe value, with a default for Nothing.
  • b) Combines multiple Maybe values into one.
  • c) Extracts the value from Just.
  • d) Converts Nothing to a default value.
  1. Why is Maybe preferred over exceptions for error handling in functional programming?
  • a) It makes error handling explicit and type-safe.
  • b) It eliminates the need for error messages.
  • c) It supports lazy evaluation.
  • d) It simplifies syntax.

4. Introduction to Either for Errors with a Message

  1. What does the Either type represent?
  • a) A value that may or may not exist
  • b) A value or an error message
  • c) A computation that never fails
  • d) A recursive data structure
  1. What are the constructors of the Either type?
  • a) Left and Right
  • b) Just and Nothing
  • c) True and False
  • d) Success and Failure
  1. Which side of the Either type conventionally holds the error message?
  • a) Left
  • b) Right
  • c) Just
  • d) Nothing
  1. How can you handle an Either value in Haskell?
  • a) By using case expressions
  • b) By directly unwrapping it
  • c) By treating it as a Maybe type
  • d) By using map only
  1. Which of the following represents a successful computation in Either?
  • a) Left value
  • b) Right value
  • c) Just value
  • d) Nothing
  1. What is the main advantage of Either over Maybe?
  • a) It supports both success and detailed error handling.
  • b) It is faster.
  • c) It can be used with recursion.
  • d) It eliminates the need for pattern matching.
  1. Which function converts a Maybe value to an Either value?
  • a) maybeToEither
  • b) fromMaybe
  • c) convertMaybe
  • d) eitherFromMaybe
  1. What does isLeft return when applied to an Either value?
  • a) True if the value is Left, otherwise False
  • b) False if the value is Right, otherwise True
  • c) The value contained in Left
  • d) The value contained in Right
  1. How can you transform the value inside a Right in Either?
  • a) Using map
  • b) Using transform
  • c) Using rightMap
  • d) Using bind
  1. Which of the following functions operates on Either values?
  • a) either
  • b) mapMaybe
  • c) eitherMap
  • d) fromEither

Here are the answers again in the table format:

QnoAnswer (Option with Text)
1c) To represent a value that may or may not exist
2b) Just and Nothing
3a) Using pattern matching
4b) Just value
5b) A value does not exist
6a) fromMaybe
7b) It results in a compile-time error.
8b) It ensures errors are handled explicitly by the programmer.
9b) A valid value 5 wrapped in Maybe
10b) Case expressions with Just and Nothing
11c) It indicates the absence of a value.
12a) Using isNothing
13a) The function is ignored, and Nothing is returned.
14a) Using map
15a) True if the value is Just, False otherwise
16a) To handle values that may cause runtime errors
17a) Using the >>= operator (bind)
18b) Using maybe
19a) Applies a function to a Maybe value, with a default for Nothing.
20a) It makes error handling explicit and type-safe.
21b) A value or an error message
22a) Left and Right
23a) Left
24a) By using case expressions
25b) Right value
26a) It supports both success and detailed error handling.
27a) maybeToEither
28a) True if the value is Left, otherwise False
29a) Using map
30a) either

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