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:
Strict vs Lazy Evaluation
Call-by-Need vs Call-by-Value
Creating Efficient Lazy Data Structures
Understanding and Avoiding Space Leaks
1. Strict vs Lazy Evaluation
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
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
Which of the following Haskell functions uses strict evaluation? a) head b) map c) foldl d) foldr
What problem does lazy evaluation potentially introduce in Haskell programs? a) Memory overuse b) Stack overflow c) Infinite loops d) Code duplication
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
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
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
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
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
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
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
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
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
What type of evaluation is primarily used for infinite lists in Haskell? a) Lazy evaluation b) Strict evaluation c) Parallel evaluation d) Immediate evaluation
Which function in Haskell allows you to take the first n elements of an infinite list? a) take b) head c) drop d) length
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
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
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
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
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
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
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
Which Haskell function can help prevent space leaks by evaluating arguments strictly? a) seq b) map c) foldr d) take
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
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
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
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
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
Which Haskell data structure requires special attention to avoid space leaks? a) Lazy lists b) Arrays c) Tuples d) Maps
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
Qno
Answer
1
b) Strict evaluation computes values immediately
2
a) It is evaluated at the point where it is first used
3
c) foldl
4
a) Memory overuse
5
a) By using the seq function
6
a) Call-by-need
7
a) Arguments are evaluated when they are needed
8
a) Arguments are evaluated before the function is called
9
a) It reduces the number of computations by evaluating only when necessary
10
a) It will never be evaluated
11
a) Linked lists
12
a) Using the repeat function
13
a) They allow handling unbounded data sequences without consuming memory all at once
14
a) Lazy evaluation
15
a) take
16
a) [42, 42, 42, 42, 42]
17
a) It repeats a finite list infinitely
18
a) By applying seq to force evaluation
19
a) When memory is unnecessarily retained due to lazy evaluation
20
a) By observing memory usage during execution
21
a) Unevaluated thunks accumulating in memory
22
a) Using seq to force evaluation
23
a) seq
24
a) It reduces space leaks by evaluating from left to right
25
a) It accumulates and causes memory usage to increase
26
a) By using strict evaluation techniques like seq
27
a) Infinite lists
28
a) The program’s memory usage grows significantly
29
a) Lazy lists
30
a) It forces evaluation of arguments, preventing excessive memory retention