MCQs on Understanding Laziness and Evaluation Strategies | Haskell

Understanding laziness and evaluation strategies in Haskell programming is crucial for writing efficient code. Concepts like strict vs lazy evaluation, call-by-need vs call-by-value, and creating efficient lazy data structures, such as infinite lists, are fundamental. Additionally, managing space leaks and avoiding them helps optimize memory usage. This collection of 30 MCQs covers these topics comprehensively.


Topics:

  1. Strict vs Lazy Evaluation
  2. Call-by-Need vs Call-by-Value
  3. Creating Efficient Lazy Data Structures
  4. Understanding and Avoiding Space Leaks

1. Strict vs Lazy Evaluation

  1. What is the main difference between strict and lazy evaluation in Haskell?
    a) Lazy evaluation is faster
    b) Strict evaluation computes values immediately
    c) Strict evaluation avoids memory leaks
    d) Lazy evaluation evaluates arguments before function execution
  2. In Haskell, what happens when an expression is evaluated lazily?
    a) It is evaluated at the point where it is first used
    b) It is evaluated at the start of the program
    c) It is evaluated by the compiler
    d) It is never evaluated
  3. Which of the following Haskell functions uses strict evaluation?
    a) head
    b) map
    c) foldl
    d) foldr
  4. What problem does lazy evaluation potentially introduce in Haskell programs?
    a) Memory overuse
    b) Stack overflow
    c) Infinite loops
    d) Code duplication
  5. How can strict evaluation be enforced in Haskell?
    a) By using the seq function
    b) By using the map function
    c) By using unsafePerformIO
    d) By using let bindings

2. Call-by-Need vs Call-by-Value

  1. In Haskell, which evaluation strategy is used for function arguments by default?
    a) Call-by-need
    b) Call-by-value
    c) Call-by-reference
    d) Call-by-name
  2. What does “call-by-need” mean in Haskell?
    a) Arguments are evaluated when they are needed
    b) Arguments are evaluated before the function call
    c) The function is evaluated once
    d) The argument is passed by reference
  3. Which of the following is true about call-by-value evaluation?
    a) Arguments are evaluated before the function is called
    b) Arguments are only evaluated when needed
    c) It is only used in lazy languages
    d) It is slower than call-by-need
  4. How does call-by-need help in terms of performance?
    a) It reduces the number of computations by evaluating only when necessary
    b) It makes all expressions execute immediately
    c) It prevents infinite loops
    d) It eliminates the need for recursion
  5. When using call-by-need in Haskell, what happens if an argument is not used in a function?
    a) It will never be evaluated
    b) It will be evaluated when the function is called
    c) It will cause an error
    d) It will be evaluated once and cached for later use

3. Creating Efficient Lazy Data Structures

  1. Which of the following is an example of a lazy data structure in Haskell?
    a) Linked lists
    b) Arrays
    c) Trees with precomputed values
    d) Hash tables
  2. How can you create an infinite list in Haskell?
    a) Using the repeat function
    b) Using the foldr function
    c) Using a for loop
    d) Using recursion without base cases
  3. What is the advantage of using infinite lists in Haskell?
    a) They allow handling unbounded data sequences without consuming memory all at once
    b) They avoid recursion
    c) They are easier to debug
    d) They increase execution speed
  4. What type of evaluation is primarily used for infinite lists in Haskell?
    a) Lazy evaluation
    b) Strict evaluation
    c) Parallel evaluation
    d) Immediate evaluation
  5. Which function in Haskell allows you to take the first n elements of an infinite list?
    a) take
    b) head
    c) drop
    d) length
  6. What is the output of the following Haskell code? take 5 (repeat 42)
    a) [42, 42, 42, 42, 42]
    b) [42, 42, 42, 42]
    c) [42]
    d) An infinite list
  7. What does the function cycle do in Haskell? a) It repeats a finite list infinitely
    b) It creates an infinite list of increasing integers
    c) It sums the elements of a list
    d) It returns an infinite list of zeros
  8. In Haskell, how can you avoid memory consumption issues when using lazy lists?
    a) By applying seq to force evaluation
    b) By using foldl instead of foldr
    c) By limiting recursion depth
    d) By using infinite recursion

4. Understanding and Avoiding Space Leaks

  1. What is a space leak in Haskell?
    a) When memory is unnecessarily retained due to lazy evaluation
    b) When the program encounters an infinite loop
    c) When a function is evaluated too early
    d) When functions return incorrect results
  2. How can you detect a space leak in a Haskell program?
    a) By observing memory usage during execution
    b) By using the unsafePerformIO function
    c) By applying strict evaluation to all functions
    d) By printing the result of all expressions
  3. What is the main cause of space leaks in lazy evaluation?
    a) Unevaluated thunks accumulating in memory
    b) Stack overflow errors
    c) Excessive recursion
    d) Infinite data structures
  4. Which of the following can be used to avoid space leaks in Haskell?
    a) Using seq to force evaluation
    b) Avoiding infinite recursion
    c) Using call-by-value evaluation
    d) Always using foldr instead of foldl
  5. Which Haskell function can help prevent space leaks by evaluating arguments strictly?
    a) seq
    b) map
    c) foldr
    d) take
  6. What is the primary advantage of using foldl over foldr in terms of space efficiency?
    a) It reduces space leaks by evaluating from left to right
    b) It avoids recursion
    c) It uses lazy evaluation
    d) It creates a more compact code
  7. What happens when a large thunk is not evaluated in a lazy program?
    a) It accumulates and causes memory usage to increase
    b) It results in faster computation
    c) It prevents infinite recursion
    d) It reduces memory usage
  8. How can you optimize memory usage in Haskell when working with large data?
    a) By using strict evaluation techniques like seq
    b) By avoiding the use of lazy lists
    c) By using foldr instead of foldl
    d) By increasing the heap size
  9. Which of the following Haskell constructs can be problematic in terms of space leaks?
    a) Infinite lists
    b) Lists with a fixed length
    c) Arrays
    d) Tuples
  10. What happens when a program with a space leak runs for a long time?
    a) The program’s memory usage grows significantly
    b) The program speeds up
    c) The program will eventually crash with an error
    d) The program stops consuming memory
  11. Which Haskell data structure requires special attention to avoid space leaks?
    a) Lazy lists
    b) Arrays
    c) Tuples
    d) Maps
  12. How can the use of the strict function help in avoiding space leaks?
    a) It forces evaluation of arguments, preventing excessive memory retention
    b) It simplifies code logic
    c) It ensures functions always return values
    d) It reduces the program’s runtime

Answer Key

QnoAnswer
1b) Strict evaluation computes values immediately
2a) It is evaluated at the point where it is first used
3c) foldl
4a) Memory overuse
5a) By using the seq function
6a) Call-by-need
7a) Arguments are evaluated when they are needed
8a) Arguments are evaluated before the function is called
9a) It reduces the number of computations by evaluating only when necessary
10a) It will never be evaluated
11a) Linked lists
12a) Using the repeat function
13a) They allow handling unbounded data sequences without consuming memory all at once
14a) Lazy evaluation
15a) take
16a) [42, 42, 42, 42, 42]
17a) It repeats a finite list infinitely
18a) By applying seq to force evaluation
19a) When memory is unnecessarily retained due to lazy evaluation
20a) By observing memory usage during execution
21a) Unevaluated thunks accumulating in memory
22a) Using seq to force evaluation
23a) seq
24a) It reduces space leaks by evaluating from left to right
25a) It accumulates and causes memory usage to increase
26a) By using strict evaluation techniques like seq
27a) Infinite lists
28a) The program’s memory usage grows significantly
29a) Lazy lists
30a) It forces evaluation of arguments, preventing excessive memory retention

4o mini

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