MCQs on List and Tree Structures | Haskell

Haskell programming offers a powerful approach to working with list and tree structures, emphasizing immutability and recursion. With advanced operations like zipping and splitting on lists, and recursive functions to manage binary trees and tree traversals, Haskell provides an elegant and functional way to handle data. Mastering these concepts is essential for efficiently solving problems involving data structures. Here, we provide 30 multiple-choice questions (MCQs) to test your knowledge on Haskell’s list and tree structures, focusing on recursion, functional data structures, and tree traversal techniques.


List and Tree Structures in Haskell – MCQs

Part 1: More Advanced List Operations

  1. Which function in Haskell is used to combine two lists element by element?
    • A) zip
    • B) concat
    • C) splitAt
    • D) head
  2. What does the splitAt function in Haskell do?
    • A) Splits a list into two parts at a given index
    • B) Concatenates two lists
    • C) Zips two lists together
    • D) Returns the first element of a list
  3. What is the result of applying zip [1,2,3] ["a", "b", "c"] in Haskell?
    • A) [(1, "a"), (2, "b"), (3, "c")]
    • B) [1, 2, 3, "a", "b", "c"]
    • C) [("a", 1), ("b", 2), ("c", 3)]
    • D) [("a", "b"), ("c", "d"), ("e", "f")]
  4. Which Haskell function splits a list into a given number of elements and a remainder?
    • A) splitAt
    • B) partition
    • C) head
    • D) tails
  5. The Haskell function zipWith applies a function to corresponding elements of two lists. Which of the following is true?
    • A) It combines two lists element by element based on the function.
    • B) It checks if two lists are of the same length.
    • C) It finds the sum of the two lists.
    • D) It removes duplicates from the two lists.
  6. What is the result of applying zipWith (+) [1,2,3] [4,5,6] in Haskell?
    • A) [5,7,9]
    • B) [4,6,8]
    • C) [1,2,3]
    • D) [1,4,9]
  7. Which function is used to merge two sorted lists into one sorted list in Haskell?
    • A) merge
    • B) concat
    • C) intercalate
    • D) concatMap
  8. Which of the following is true about the foldl function in Haskell?
    • A) It iterates from the left, accumulating the result.
    • B) It iterates from the right, accumulating the result.
    • C) It only processes the first element.
    • D) It is used for sorting lists.
  9. What will the function foldr (+) 0 [1,2,3] return in Haskell?
    • A) 6
    • B) 0
    • C) 3
    • D) 9
  10. Which function can be used to reverse a list in Haskell?
    • A) reverse
    • B) rev
    • C) reverseList
    • D) flip

Part 2: Binary Trees and Recursion

  1. What is the primary use of recursion in binary trees in Haskell?
    • A) To manage memory efficiently
    • B) To implement tree traversal
    • C) To store data in sorted order
    • D) To modify tree nodes
  2. Which of the following is the correct structure of a binary tree in Haskell?
    • A) data Tree = Empty | Node Int Tree Tree
    • B) data Tree = Node Int Tree Tree
    • C) data Tree = Leaf Int
    • D) data Tree = Binary Node
  3. What is the base case for a recursive function that processes a binary tree in Haskell?
    • A) When the tree is empty (Empty)
    • B) When the tree has only one node
    • C) When the tree has more than one node
    • D) When the tree contains data
  4. In Haskell, how can you define a simple binary tree with a value and two children?
    • A) Node value left right
    • B) Binary value left right
    • C) Tree value left right
    • D) Branch value left right
  5. What is the result of recursively traversing a binary tree in Haskell?
    • A) A list of tree values in a specific order
    • B) A string representation of the tree
    • C) A reversed tree structure
    • D) A sorted list of elements
  6. Which of the following Haskell functions is typically used for tree traversal?
    • A) map
    • B) foldr
    • C) traverse
    • D) filter
  7. What is the purpose of the inorder traversal in a binary tree?
    • A) To visit the left subtree, then the root, then the right subtree
    • B) To visit the right subtree, then the root, then the left subtree
    • C) To visit only the leftmost node
    • D) To visit all nodes at the same level
  8. What is the result of the inorder traversal of the binary tree Node 1 (Node 2 Empty Empty) (Node 3 Empty Empty)?
    • A) [2, 1, 3]
    • B) [1, 2, 3]
    • C) [3, 2, 1]
    • D) [1, 3, 2]
  9. In a binary search tree, where are values less than the current node stored?
    • A) In the right subtree
    • B) In the left subtree
    • C) In the root node
    • D) In the parent node
  10. What does the function size return for a binary tree in Haskell?
    • A) The number of nodes in the tree
    • B) The depth of the tree
    • C) The maximum value in the tree
    • D) The height of the tree

Part 3: Working with Tree Traversals

  1. Which traversal method processes the left child first in Haskell?
    • A) Preorder
    • B) Inorder
    • C) Postorder
    • D) Level order
  2. What is the purpose of the postorder traversal of a binary tree?
    • A) Visit the left and right children before the node itself
    • B) Visit the node before the children
    • C) Visit the children before the leftmost node
    • D) Visit the node and then both children
  3. What is a key property of a “balanced” binary tree?
    • A) The left and right subtrees of every node differ in height by at most one
    • B) The left and right subtrees are always the same size
    • C) The tree has only one child node at each level
    • D) The tree is a perfect binary tree
  4. How is the preorder traversal of a tree implemented in Haskell?
    • A) Process the root, then the left subtree, then the right subtree
    • B) Process the left subtree, then the root, then the right subtree
    • C) Process the left subtree, then the right subtree, then the root
    • D) Process the right subtree, then the left subtree
  5. Which of the following is true for a tree with the following structure: Node 5 (Node 3 Empty Empty) (Node 8 Empty Empty)?
    • A) It is a balanced tree.
    • B) It is a left-skewed tree.
    • C) It is a right-skewed tree.
    • D) It is an unbalanced tree.
  6. What does the function height return for a binary tree in Haskell?
    • A) The number of edges on the longest path from the root to a leaf
    • B) The number of nodes in the tree
    • C) The maximum depth of the tree
    • D) The value of the root node
  7. Which traversal method would you use to process all nodes at the same depth in a tree in Haskell?
    • A) Preorder
    • B) Inorder
    • C) Level order
    • D) Postorder
  8. In Haskell, what does the function flatten do for a tree?
    • A) Converts the tree into a list of values
    • B) Flattens the tree structure into a single node
    • C) Removes duplicate nodes
    • D) Balances the tree
  9. How would you define a function in Haskell to find the maximum value in a binary tree?
    • A) Use recursion to compare the current node’s value with its children’s values
    • B) Simply return the root node’s value
    • C) Only consider the left child’s value
    • D) Traverse the tree in preorder and sum the values
  10. In Haskell, the foldTree function is useful for what purpose?
    • A) To reduce a tree’s values using a function
    • B) To balance an unbalanced tree
    • C) To traverse the tree in level order
    • D) To convert the tree into a list

Answer Key

QnoAnswer
1A) zip
2A) Splits a list into two parts at a given index
3A) [(1, "a"), (2, "b"), (3, "c")]
4A) splitAt
5A) It combines two lists element by element based on the function.
6A) [5,7,9]
7A) merge
8A) It iterates from the left, accumulating the result.
9A) 6
10A) reverse
11B) To implement tree traversal
12A) `data Tree = Empty
13A) When the tree is empty (Empty)
14A) Node value left right
15A) A list of tree values in a specific order
16C) traverse
17A) To visit the left subtree, then the root, then the right subtree
18A) [2, 1, 3]
19B) In the left subtree
20A) The number of nodes in the tree
21A) Preorder
22A) Visit the left and right children before the node itself
23A) The left and right subtrees of every node differ in height by at most one
24A) Process the root, then the left subtree, then the right subtree
25A) It is a balanced tree.
26A) The number of edges on the longest path from the root to a leaf
27C) Level order
28A) Converts the tree into a list of values
29A) Use recursion to compare the current node’s value with its children’s values
30A) To reduce a tree’s values using a function

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