Haskell, a functional programming language, provides powerful tools for working with lists, making it essential for handling data effectively. This set of 30 multiple-choice questions explores list operations, manipulation functions like map and filter, zip and unzip functions, and Haskell’s unique lazy evaluation capabilities, including infinite lists.
MCQs on Working with Lists in Haskell
Basic List Operations
What does the head function do in Haskell?
A) Returns the last element of a list
B) Returns the first element of a list
C) Removes the first element of a list
D) Returns the length of a list
What is the purpose of the tail function?
A) Returns the last element of a list
B) Returns the list without its first element
C) Reverses the list
D) Returns the number of elements in the list
Which function is used to find the length of a list?
A) size
B) len
C) length
D) count
What happens if you call head on an empty list?
A) Returns Nothing
B) Returns an empty list
C) Throws an error
D) Returns 0
How can you create a list with numbers from 1 to 10 in Haskell?
A) [1, 10]
B) range(1, 10)
C) [1..10]
D) {1 to 10}
List Manipulation Functions
What does the concat function do in Haskell?
A) Combines two lists into one
B) Concatenates a string to a list
C) Flattens a list of lists into a single list
D) Joins two elements
Which function applies a given function to each element of a list?
A) filter
B) map
C) foldl
D) zip
What does the filter function do?
A) Removes elements from a list
B) Selects elements from a list based on a condition
C) Maps elements to a new list
D) Combines two lists
Which function combines all elements of a list into a single value by applying a function from left to right?
A) foldr
B) foldl
C) map
D) concat
How does foldr differ from foldl?
A) foldr processes elements from right to left, and foldl from left to right
B) foldr processes only odd elements
C) foldr works with strings only
D) They are identical
What does map (+1) [1, 2, 3] return?
A) [1, 2, 3]
B) [2, 3, 4]
C) [0, 1, 2]
D) [2, 4, 6]
How would you double all even numbers in a list [1, 2, 3, 4]?
A) filter even [1, 2, 3, 4]
B) map (*2) (filter even [1, 2, 3, 4])
C) foldl (+) 0 [1, 2, 3, 4]
D) concat [1, 2, 3, 4]
Which function removes duplicate elements from a list?
A) filter
B) nub
C) concat
D) map
What does foldl (+) 0 [1, 2, 3] evaluate to?
A) 6
B) [1, 2, 3]
C) 0
D) 3
Which function checks if all elements of a list satisfy a condition?
A) any
B) all
C) filter
D) map
Zip and Unzip Functions
What does the zip function do?
A) Joins two lists into a list of tuples
B) Combines two lists into a single list
C) Filters elements from a list
D) Reverses a list
What is the result of zip [1, 2, 3] ['a', 'b', 'c']?
A) [(1, 'a'), (2, 'b'), (3, 'c')]
B) [(1, 2, 3), ('a', 'b', 'c')]
C) [1, 2, 3, 'a', 'b', 'c']
D) ['a', 'b', 'c', 1, 2, 3]
What does the unzip function do?
A) Reverses the result of zip
B) Creates a tuple from two lists
C) Separates a list of tuples into two lists
D) Combines elements of two lists
Which of the following is true for zipWith?
A) It combines two lists using a custom function
B) It merges two lists into tuples
C) It filters elements of a list
D) It performs folding
How does zip handle lists of different lengths?
A) Pads the shorter list with Nothing
B) Throws an error
C) Truncates to the length of the shorter list
D) Repeats the shorter list
Understanding Laziness in Lists (Infinite Lists)
What is a key feature of lists in Haskell?
A) Strict evaluation
B) Lazy evaluation
C) Mutability
D) Fixed size
How would you create an infinite list of numbers starting from 1?
A) [1, 2, 3, ...]
B) repeat 1
C) [1..]
D) infinity 1
Which function can be used to take the first 10 elements of an infinite list?
A) head
B) take
C) filter
D) map
What does take 5 [1..] return?
A) [1, 2, 3, 4, 5]
B) [1..]
C) [5, 4, 3, 2, 1]
D) [1, 1, 1, 1, 1]
What is the result of evaluating sum (take 5 [1..])?
A) 15
B) 5
C) 10
D) 1
How does Haskell handle infinite lists?
A) Evaluates the entire list in memory
B) Evaluates only the needed elements
C) Converts the list to a finite size automatically
D) Throws an error
What is the purpose of the cycle function?
A) Creates an infinite repetition of a list
B) Reverses a list
C) Filters elements of a list
D) Merges two lists
How would you create an infinite list of alternating 1 and 0?
A) repeat [1, 0]
B) cycle [1, 0]
C) [1..]
D) concat [1, 0]
What does the repeat function do?
A) Repeats a value infinitely
B) Repeats a list once
C) Filters duplicate values
D) Creates tuples
Which of the following can be used to generate Fibonacci numbers lazily?
A) zipWith (+) fibs (tail fibs)
B) [1, 1..]
C) repeat 1
D) foldl (+) 0 fibs
**Answer Key
4o
ChatGPT said:
ChatGPT
**
Qno
Answer (Option with the text)
1
B) Returns the first element of a list
2
B) Returns the list without its first element
3
C) length
4
C) Throws an error
5
C) [1..10]
6
C) Flattens a list of lists into a single list
7
B) map
8
B) Selects elements from a list based on a condition
9
B) foldl
10
A) foldr processes elements from right to left, and foldl from left to right