Concurrency and parallelism are essential for achieving high performance in modern computing, especially when working with Haskell. This set of 30 multiple-choice questions (MCQs) covers advanced topics like Software Transactional Memory (STM), parallel Haskell using par and pseq, the Actor model, and distributed systems. These concepts are crucial for building efficient, scalable applications that can handle complex computations.
Topics:
Software Transactional Memory (STM)
Which of the following is the key feature of Software Transactional Memory (STM)? a) Lock-based synchronization b) Transactional consistency c) Multithreaded computation d) Data immutability
In Haskell’s STM, which type represents a transaction? a) MVar b) TVar c) IORef d) STRef
Which STM operation is used to start a new transaction in Haskell? a) atomically b) forkIO c) newTVarIO d) retry
In STM, the retry function causes the transaction to: a) Commit immediately b) Rollback and retry c) Exit the transaction d) Suspend the transaction
What is the default behavior of Haskell’s STM when a transaction cannot immediately complete? a) Immediate rollback b) Retry at a later time c) Block indefinitely d) Force a deadlock
Which of the following functions is used to commit a transaction in STM? a) commit b) atomic c) atomically d) commitSTM
How does STM in Haskell differ from traditional lock-based concurrency mechanisms? a) STM uses locks b) STM provides transactional guarantees without locks c) STM avoids multi-threading d) STM restricts the number of threads
What happens if two STM transactions attempt to modify the same TVar at the same time? a) Both transactions succeed b) One transaction succeeds, the other is aborted c) Both transactions block indefinitely d) A deadlock occurs
Which Haskell package provides the STM module for concurrency? a) Control.Concurrent b) Control.Monad c) Control.Concurrent.STM d) Control.Applicative
What is the primary advantage of STM over traditional locks in concurrent programming? a) Better performance b) Simpler code c) Avoidance of deadlock d) Easier debugging
Parallel Haskell and par/pseq for Parallel Computation
What is the purpose of par in Haskell? a) Sequential computation b) Parallelizing pure computations c) Managing stateful computations d) Handling IO operations
In Haskell, what is the function pseq used for? a) For parallel computation b) For sequential evaluation c) For creating threads d) For managing mutable variables
Which of the following is true about the relationship between par and pseq? a) par ensures order of execution b) pseq guarantees parallel execution c) par creates parallel threads, pseq enforces evaluation order d) par is for IO operations, pseq is for STM
What does the par combinator achieve in Haskell? a) It forces strict evaluation b) It signals that the computation can be performed in parallel c) It handles side-effects d) It suspends computation until needed
Which type of computation does pseq help with in a parallel setting? a) Lazy evaluation b) Strict evaluation c) Deadlock prevention d) IO operations
What is the result of using par without pseq in a parallel computation? a) A deadlock b) Undefined behavior c) Non-deterministic evaluation order d) Immediate computation
In which kind of computation is the combination of par and pseq most useful? a) Parallel recursive computation b) Sequential data transformation c) Single-threaded execution d) IO-bound tasks
How does parallel Haskell differ from traditional parallel programming models? a) Haskell does not support parallelism b) Haskell uses functional constructs for parallelism c) Haskell relies solely on multi-core processors d) Haskell only supports parallel loops
What does the Control.Parallel module provide in Haskell? a) Transactional memory support b) Parallel execution functions like par and pseq c) IO and mutable state d) Actor-based concurrency models
Which of the following describes the evaluation strategy for parallel Haskell code? a) Eager evaluation b) Lazy evaluation c) Strict evaluation d) Dynamic evaluation
Actors and Message-Passing Concurrency Models
Which of the following is a key concept in the Actor model of concurrency? a) Shared memory b) Message-passing between independent agents c) Locking mechanisms d) Centralized control
In the Actor model, each actor is responsible for: a) Sending and receiving messages b) Managing shared state c) Coordinating threads d) Synchronizing memory access
Which library provides actor-based concurrency in Haskell? a) Control.Concurrent b) ActorModel c) Control.Concurrent.Actor d) HAppS
What is the fundamental communication mechanism in the Actor model? a) Shared variables b) Direct function calls c) Message-passing d) Memory-mapped files
In the Actor model, what happens when an actor receives a message? a) It updates its own state and sends a response b) It immediately terminates c) It blocks all other actors d) It suspends other actors until completion
What does the Actor model avoid in comparison to traditional multi-threaded programming? a) Deadlock b) Race conditions c) Data-sharing between threads d) Thread synchronization
How do actors in the Actor model manage their state? a) Through mutable global variables b) Through message queues c) By synchronizing with other actors d) By having local private states
In Haskell, which function is typically used to send messages to an actor? a) sendMessage b) actorSend c) tell d) postMessage
Which of the following best describes the Actor model’s suitability for concurrent applications? a) Ideal for applications with shared memory b) Useful for applications with isolated processes communicating via messages c) Best suited for sequential computation d) Designed for applications with minimal communication between processes
What is a potential disadvantage of the Actor model in Haskell? a) Limited scalability b) High overhead in managing message queues c) Lack of support for parallel execution d) Complex state management
Answer Key:
Qno
Answer
1
b) Transactional consistency
2
b) TVar
3
a) atomically
4
b) Rollback and retry
5
b) Retry at a later time
6
c) atomically
7
b) STM provides transactional guarantees without locks
8
b) One transaction succeeds, the other is aborted
9
c) Control.Concurrent.STM
10
c) Avoidance of deadlock
11
b) Parallelizing pure computations
12
b) For sequential evaluation
13
c) par creates parallel threads, pseq enforces evaluation order
14
b) It signals that the computation can be performed in parallel
15
b) Strict evaluation
16
c) Non-deterministic evaluation order
17
a) Parallel recursive computation
18
b) Haskell uses functional constructs for parallelism
19
b) Parallel execution functions like par and pseq
20
b) Lazy evaluation
21
b) Message-passing between independent agents
22
a) Sending and receiving messages
23
c) Control.Concurrent.Actor
24
c) Message-passing
25
a) It updates its own state and sends a response
26
c) Data-sharing between threads
27
d) By having local private states
28
c) tell
29
b) Useful for applications with isolated processes communicating via messages