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
Which function in Haskell is used to combine two lists element by element?
A) zip
B) concat
C) splitAt
D) head
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
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")]
Which Haskell function splits a list into a given number of elements and a remainder?
A) splitAt
B) partition
C) head
D) tails
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.
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]
Which function is used to merge two sorted lists into one sorted list in Haskell?
A) merge
B) concat
C) intercalate
D) concatMap
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.
What will the function foldr (+) 0 [1,2,3] return in Haskell?
A) 6
B) 0
C) 3
D) 9
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
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
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
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
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
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
Which of the following Haskell functions is typically used for tree traversal?
A) map
B) foldr
C) traverse
D) filter
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
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]
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
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
Which traversal method processes the left child first in Haskell?
A) Preorder
B) Inorder
C) Postorder
D) Level order
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
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
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
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.
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
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
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
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
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
Qno
Answer
1
A) zip
2
A) Splits a list into two parts at a given index
3
A) [(1, "a"), (2, "b"), (3, "c")]
4
A) splitAt
5
A) It combines two lists element by element based on the function.
6
A) [5,7,9]
7
A) merge
8
A) It iterates from the left, accumulating the result.
9
A) 6
10
A) reverse
11
B) To implement tree traversal
12
A) `data Tree = Empty
13
A) When the tree is empty (Empty)
14
A) Node value left right
15
A) A list of tree values in a specific order
16
C) traverse
17
A) To visit the left subtree, then the root, then the right subtree
18
A) [2, 1, 3]
19
B) In the left subtree
20
A) The number of nodes in the tree
21
A) Preorder
22
A) Visit the left and right children before the node itself
23
A) The left and right subtrees of every node differ in height by at most one
24
A) Process the root, then the left subtree, then the right subtree
25
A) It is a balanced tree.
26
A) The number of edges on the longest path from the root to a leaf
27
C) Level order
28
A) Converts the tree into a list of values
29
A) Use recursion to compare the current node’s value with its children’s values