MCQs on Working with Lists | Haskell

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

  1. 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
  2. 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
  3. Which function is used to find the length of a list?
    • A) size
    • B) len
    • C) length
    • D) count
  4. 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
  5. 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

  1. 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
  2. Which function applies a given function to each element of a list?
    • A) filter
    • B) map
    • C) foldl
    • D) zip
  3. 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
  4. 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
  5. 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
  6. What does map (+1) [1, 2, 3] return?
    • A) [1, 2, 3]
    • B) [2, 3, 4]
    • C) [0, 1, 2]
    • D) [2, 4, 6]
  7. 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]
  8. Which function removes duplicate elements from a list?
    • A) filter
    • B) nub
    • C) concat
    • D) map
  9. What does foldl (+) 0 [1, 2, 3] evaluate to?
    • A) 6
    • B) [1, 2, 3]
    • C) 0
    • D) 3
  10. Which function checks if all elements of a list satisfy a condition?
    • A) any
    • B) all
    • C) filter
    • D) map

Zip and Unzip Functions

  1. 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
  2. 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]
  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
  4. 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
  5. 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)

  1. What is a key feature of lists in Haskell?
    • A) Strict evaluation
    • B) Lazy evaluation
    • C) Mutability
    • D) Fixed size
  2. How would you create an infinite list of numbers starting from 1?
    • A) [1, 2, 3, ...]
    • B) repeat 1
    • C) [1..]
    • D) infinity 1
  3. Which function can be used to take the first 10 elements of an infinite list?
    • A) head
    • B) take
    • C) filter
    • D) map
  4. 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]
  5. What is the result of evaluating sum (take 5 [1..])?
    • A) 15
    • B) 5
    • C) 10
    • D) 1
  6. 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
  7. 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
  8. 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]
  9. What does the repeat function do?
    • A) Repeats a value infinitely
    • B) Repeats a list once
    • C) Filters duplicate values
    • D) Creates tuples
  10. 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

**

QnoAnswer (Option with the text)
1B) Returns the first element of a list
2B) Returns the list without its first element
3C) length
4C) Throws an error
5C) [1..10]
6C) Flattens a list of lists into a single list
7B) map
8B) Selects elements from a list based on a condition
9B) foldl
10A) foldr processes elements from right to left, and foldl from left to right
11B) [2, 3, 4]
12B) map (*2) (filter even [1, 2, 3, 4])
13B) nub
14A) 6
15B) all
16A) Joins two lists into a list of tuples
17A) [(1, 'a'), (2, 'b'), (3, 'c')]
18C) Separates a list of tuples into two lists
19A) It combines two lists using a custom function
20C) Truncates to the length of the shorter list
21B) Lazy evaluation
22C) [1..]
23B) take
24A) [1, 2, 3, 4, 5]
25A) 15
26B) Evaluates only the needed elements
27A) Creates an infinite repetition of a list
28B) cycle [1, 0]
29A) Repeats a value infinitely
30A) zipWith (+) fibs (tail fibs)

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