MCQs on Functions in Haskell | Haskell

Haskell Programming: Understanding Functions

Haskell is a functional programming language that relies heavily on functions for computation. To excel in Haskell, understanding how to define functions, use function application and currying, compose functions, and work with higher-order functions like map, filter, and fold is crucial. These concepts power Haskell’s expressive and efficient style.


30 Multiple-Choice Questions on Functions in Haskell

Defining Functions

  1. What is the syntax to define a function in Haskell?
    • A) functionName args = expression
    • B) def functionName(args):
    • C) function functionName(args)
    • D) let functionName = args => expression
  2. Which keyword is used to define a local function within another function in Haskell?
    • A) let
    • B) where
    • C) do
    • D) in
  3. How can you define a function with multiple cases in Haskell?
    • A) Using the if-else construct only
    • B) By writing multiple patterns with |
    • C) Using case or pattern matching
    • D) Using a switch statement
  4. What happens if a function in Haskell does not cover all possible input patterns?
    • A) The function compiles but produces runtime errors for unhandled patterns.
    • B) The compiler fills the missing patterns automatically.
    • C) It will generate warnings but continue execution.
    • D) It will refuse to compile.
  5. How do you declare a type signature for a function in Haskell?
    • A) functionName :: Type
    • B) def functionName : Type
    • C) let functionName = Type
    • D) type functionName = Type

Function Application and Currying

  1. In Haskell, how is a function applied to its arguments?
    • A) By enclosing arguments in parentheses
    • B) By using whitespace between the function name and arguments
    • C) By using commas to separate arguments
    • D) By using a : separator
  2. What is currying in Haskell?
    • A) A process of combining multiple functions into one
    • B) Transforming a function into a sequence of functions, each taking one argument
    • C) Making functions operate only on lists
    • D) Declaring multiple type signatures for a single function
  3. What is the result of partially applying a function in Haskell?
    • A) A new function with fewer arguments
    • B) An error, since all arguments must be provided
    • C) The function is discarded
    • D) The partially applied function is automatically executed
  4. What is the difference between f x y and f (x y) in Haskell?
    • A) f x y applies f to x and then to y, while f (x y) applies x to y first.
    • B) There is no difference; both are valid and equivalent.
    • C) f (x y) is invalid syntax.
    • D) f (x y) indicates that f is partially applied.
  5. Which of the following is true about function application in Haskell?
  • A) It is right-associative.
  • B) It requires explicit parentheses for all arguments.
  • C) It is left-associative.
  • D) It requires type annotations every time.

Function Composition

  1. What does the (.) operator do in Haskell?
  • A) Multiplies two numbers
  • B) Composes two functions such that (f . g) x = f (g x)
  • C) Creates a tuple of two functions
  • D) Concatenates two functions
  1. How can you write the composition of f and g applied to x?
  • A) f g x
  • B) f (g x)
  • C) (f x) . (g x)
  • D) (f g) x
  1. What is the primary benefit of using function composition in Haskell?
  • A) It makes the code faster.
  • B) It reduces the need for recursion.
  • C) It creates cleaner and more readable code by avoiding intermediate variables.
  • D) It allows functions to operate on lists directly.
  1. How does function composition work with higher-order functions?
  • A) Only one higher-order function can be composed at a time.
  • B) Higher-order functions cannot be composed.
  • C) Higher-order functions can be composed just like any other functions.
  • D) Composition must involve intermediate variables for higher-order functions.
  1. What is the type of the (.) operator in Haskell?
  • A) (a -> b) -> (b -> c) -> (a -> c)
  • B) (b -> a) -> (a -> c) -> (c -> b)
  • C) (a -> b) -> (c -> a) -> (c -> b)
  • D) (a -> b) -> (b -> a) -> (a -> b)

Higher-Order Functions

  1. What is a higher-order function in Haskell?
  • A) A function that only operates on numbers
  • B) A function that takes another function as input or returns a function as output
  • C) A function that cannot be partially applied
  • D) A function that operates only on lists
  1. Which of the following is an example of a higher-order function?
  • A) map
  • B) if-else
  • C) let
  • D) case
  1. What does the map function do in Haskell?
  • A) Transforms each element of a list using a given function
  • B) Filters elements from a list based on a condition
  • C) Reduces a list to a single value
  • D) Creates a new list from the intersection of two lists
  1. How does filter function work in Haskell?
  • A) It applies a function to all elements of a list.
  • B) It returns all elements of a list that satisfy a given predicate.
  • C) It concatenates two lists.
  • D) It creates a new list with unique elements only.
  1. What is the purpose of foldl in Haskell?
  • A) To transform a list into a single value by iterating from left to right
  • B) To transform a list into a single value by iterating from right to left
  • C) To concatenate two lists
  • D) To map a function over a list
  1. What is the key difference between foldl and foldr?
  • A) foldl starts from the rightmost element, while foldr starts from the leftmost element.
  • B) foldl processes the list left to right, while foldr processes it right to left.
  • C) foldl works only on numbers, while foldr works on strings.
  • D) There is no difference between the two.
  1. How is map different from foldl?
  • A) map creates a new list, while foldl reduces the list to a single value.
  • B) map requires a predicate, while foldl does not.
  • C) foldl works only with numbers, while map works with any type.
  • D) foldl operates only on the last element of the list.
  1. What is the type of the map function in Haskell?
  • A) (a -> b) -> [a] -> b
  • B) (a -> b) -> [a] -> [b]
  • C) (a -> b) -> a -> [b]
  • D) (a -> b) -> b -> [a]
  1. How does the filter function handle an empty list?
  • A) It throws an error.
  • B) It returns Nothing.
  • C) It returns an empty list.
  • D) It concatenates two empty lists.
  1. What does foldr (+) 0 [1, 2, 3] return in Haskell?
  • A) 6
  • B) 5
  • C) 3
  • D) 1
  1. Which function can be used to check if all elements of a list satisfy a condition?
  • A) map
  • B) foldr
  • C) all
  • D) filter
  1. Which higher-order function applies a function to elements of two lists simultaneously?
  • A) map
  • B) zipWith
  • C) filter
  • D) foldl
  1. What does foldl (*) 1 [1, 2, 3] return?
  • A) 6
  • B) 5
  • C) 1
  • D) 0
  1. How does foldl differ from scanl?
  • A) foldl produces intermediate results, while scanl does not.
  • B) scanl

4o

ChatGPT said:

ChatGPT

produces a list of accumulated results, while foldl only returns the final result.

  • C) There is no difference; they are equivalent.
  • D) scanl is used for numerical operations only.
  1. What is the result of applying map (+1) to the list [1,2,3] in Haskell?
  • A) [1, 2, 3]
  • B) [2, 3, 4]
  • C) [1, 3, 6]
  • D) [3, 2, 1]

Answers:

QnoAnswer (Option with the text)
1A) functionName args = expression
2A) let
3B) By writing multiple patterns with `
4A) The function compiles but produces runtime errors for unhandled patterns
5A) functionName :: Type
6B) By using whitespace between the function name and arguments
7B) Transforming a function into a sequence of functions, each taking one argument
8A) A new function with fewer arguments
9A) f x y applies f to x and then to y, while f (x y) applies x to y first
10A) It is right-associative.
11B) Composes two functions such that (f . g) x = f (g x)
12B) f (g x)
13C) It creates cleaner and more readable code by avoiding intermediate variables
14C) Higher-order functions can be composed just like any other functions
15A) (a -> b) -> (b -> c) -> (a -> c)
16B) A function that takes another function as input or returns a function as output
17A) map
18A) Transforms each element of a list using a given function
19B) It returns all elements of a list that satisfy a given predicate
20A) To transform a list into a single value by iterating from left to right
21B) foldl processes the list left to right, while foldr processes it right to left
22A) map creates a new list, while foldl reduces the list to a single value
23B) (a -> b) -> [a] -> [b]
24C) It returns an empty list
25A) 6
26C) all
27B) zipWith
28A) 6
29B) scanl produces a list of accumulated results, while foldl only returns the final result
30B) [2, 3, 4]

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