MCQs on Pattern Matching and Guards | Haskell

attern matching and guards are powerful features in Haskell that allow for concise and expressive code. Pattern matching is often used with data types like lists, tuples, and custom types to simplify condition checking. Guards and where clauses help in defining more readable conditions for function definitions, while recursive pattern matching allows for cleaner and more efficient recursion. “As patterns” are used for enhancing readability when dealing with complex structures. These topics are fundamental for mastering Haskell’s functional programming style.


MCQs on Pattern Matching and Guards in Haskell Programming

Pattern Matching on Data Types

  1. What is the output of the following Haskell function for the input [1,2,3]?lessCopy codef (x:xs) = x f [] = 0
    A) 1
    B) 2
    C) 3
    D) 0
  2. Which of the following is true about pattern matching on a tuple in Haskell?
    A) Only the first element of the tuple can be matched
    B) Pattern matching can be used to extract multiple elements
    C) Tuples are not supported in pattern matching
    D) Tuples can only be matched using guards
  3. How does Haskell pattern match on an empty list []?
    A) It is considered a base case
    B) It causes a runtime error
    C) It matches as any other element
    D) It matches only when the list has a single element
  4. In Haskell, what is the output of the following function call f (1, 2, 3)?javaCopy codef (x, y, z) = x + y + z f _ = 0
    A) 0
    B) 3
    C) 6
    D) 2
  5. Which of the following is correct regarding pattern matching on custom data types in Haskell?
    A) Pattern matching can only be done on primitive types
    B) It cannot be used for data types with multiple constructors
    C) Pattern matching is a natural way to handle custom types with multiple data constructors
    D) Custom data types cannot be pattern matched

Guards and Where Clauses

  1. What is the purpose of a guard in Haskell? A) To ensure that a pattern is matched
    B) To check conditions when defining functions
    C) To bind variables to specific values
    D) To prevent errors during pattern matching
  2. What does the following Haskell code do?Copy codeabsValue x | x >= 0 = x | otherwise = -x
    A) Returns the absolute value of x
    B) Returns the negative of x
    C) Returns the square of x
    D) Returns x unchanged
  3. How does the where clause help in Haskell?
    A) It defines default values for patterns
    B) It can define local variables to be used in the function
    C) It acts as a guard for specific conditions
    D) It forces the use of recursion in functions
  4. Which of the following is an example of using a guard in Haskell?
    A) f x = x + 2
    B) f x | x > 0 = "Positive" | otherwise = "Non-positive"
    C) f (x:xs) = x
    D) f = x
  5. What is the result of calling f 3 in the following function?
f x | x > 0 = "Positive"
| x == 0 = "Zero"
| otherwise = "Negative"

A) “Positive”
B) “Zero”
C) “Negative”
D) Error

Recursive Pattern Matching and Optimization

  1. What is the output of the recursive function call f [1, 2, 3] where f is defined as:
f (x:xs) = x + f xs
f [] = 0

A) 6
B) 3
C) 1
D) 0

  1. What happens if you omit a recursive base case in a Haskell function?
    A) The function will be optimized
    B) It will cause an infinite recursion leading to a stack overflow
    C) It will always return Nothing
    D) The program will terminate successfully
  2. How can recursion be optimized in Haskell for better performance?
    A) By avoiding pattern matching
    B) By using tail recursion and accumulating results
    C) By using where clauses in recursion
    D) By calling the function only once
  3. Which of the following best describes a tail-recursive function in Haskell?
    A) A function where the recursive call is the first operation in the function body
    B) A function that never calls itself recursively
    C) A function that uses pattern matching exclusively
    D) A function that calls other functions instead of itself
  4. What is the result of evaluating f 4 if the function f is defined recursively as follows?
f 0 = 1
f n = n * f (n-1)

A) 4
B) 24
C) 1
D) 120

Using As Patterns for More Readable Code

  1. What does the @ symbol in Haskell represent in pattern matching?
    A) It is used to denote a list
    B) It binds a variable to the entire matched value for further use
    C) It defines a guard condition
    D) It defines a type constraint
  2. What is the benefit of using “as patterns” in Haskell?
    A) It helps avoid unnecessary recursion
    B) It increases the readability of the code
    C) It restricts the function to certain patterns
    D) It prevents the use of tuples
  3. In the following Haskell function, what does the @ in xs@(x:xs) do?
f xs@(x:xs) = x
f [] = 0

A) It matches the value of x and xs separately
B) It binds xs to the entire list for future use
C) It matches only when the list has more than one element
D) It checks if the list contains exactly two elements

  1. What would be the result of calling f [1,2,3] with the following function?
f xs@(x:xs) = length xs
f [] = 0

A) 2
B) 3
C) 1
D) 0

  1. How does “as patterns” improve code readability?
    A) By avoiding pattern matching entirely
    B) By allowing you to refer to the entire matched value while also destructuring it
    C) By limiting the use of recursion
    D) By simplifying guards

Combining Pattern Matching and Guards

  1. What will be the output of f 10 if the following function is used?
f x | x > 5 = "Greater"
| x <= 5 = "Lesser"

A) “Greater”
B) “Lesser”
C) “Equal”
D) Error

  1. How does Haskell decide between different patterns when multiple match clauses are present?
    A) It uses a first-match rule
    B) It always chooses the most complex pattern
    C) It follows a depth-first matching strategy
    D) It chooses the pattern with the highest complexity
  2. Which of the following is an advantage of using guards in Haskell?
    A) Guards allow for more concise and readable code
    B) Guards prevent recursion
    C) Guards automatically optimize pattern matching
    D) Guards only work with custom data types
  3. Which of the following expressions is an example of using a “where” clause for optimization in Haskell?
    A) f x = g x + 1 where g x = x * 2
    B) f x = g x | x > 0 = True | otherwise = False
    C) f x = x + 1 | x < 10 = True
    D) f x = x + 1
  4. What is the result of f 5 if f is defined as follows?
f x | x < 0 = "Negative"
| x == 0 = "Zero"
| x > 0 = "Positive"

A) “Negative”
B) “Zero”
C) “Positive”
D) Error

  1. What does the following pattern match return when called with f [3, 2, 1]?
f (x:y:xs) = x + y
f [] = 0

A) 6
B) 5
C) 3
D) 2

  1. Which of the following is the most efficient way to match on a list in Haskell?
    A) Using recursion with a base case and pattern matching
    B) Using guards only
    C) Using “as patterns”
    D) Using infinite lists
  2. How would you match on a list where you want both the first element and the rest of the list in Haskell?
    A) Use x:xs
    B) Use x|xs
    C) Use head xs
    D) Use first:rest
  3. What does this code return for input [3, 2, 1]?
f xs@(x:xs) = length xs
f [] = 0

A) 3
B) 2
C) 1
D) 0

  1. What will the following Haskell function return for f [1,2,3]?
f (x:xs) = "First element: " ++ show x
f [] = "Empty list"

A) “First element: 1”
B) “First element: 2”
C) “Empty list”
D) Error


Answers

QnoAnswer
1A) 1
2B) Pattern matching can be used to extract multiple elements
3A) It is considered a base case
4C) 6
5C) Pattern matching is a natural way to handle custom types with multiple data constructors
6B) To check conditions when defining functions
7A) Returns the absolute value of x
8B) It can define local variables to be used in the function
9B) `f x
10A) “Positive”
11A) 6
12B) It will cause an infinite recursion leading to a stack overflow
13B) By using tail recursion and accumulating results
14A) A function where the recursive call is the first operation in the function body
15B) 24
16B) It binds a variable to the entire matched value for further use
17B) It increases the readability of the code
18B) It binds xs to the entire list for future use
19A) 2
20B) By allowing you to refer to the entire matched value while also destructuring it
21A) “Greater”
22A) It uses a first-match rule
23A) Guards allow for more concise and readable code
24A) f x = g x + 1 where g x = x * 2
25C) “Positive”
26B) 5
27A) Using recursion with a base case and pattern matching
28A) Use x:xs
29B) 2
30A) “First element: 1”

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