# Concurrent Programming in Java Quiz

Enroll Now

## Week- 1

### Module 1 Quiz

1.
Question 1
Which of the following primitive operations on threads were introduced in Lecture 1.1?

Please choose all options that are correct.

1 point

• C. Terminating (killing) a thread
• D. Joining or waiting on a thread

2.
Question 2
Which of the following statements are true, based on what you learned about threads in Lecture 1.1?

Please choose all options that are correct.

1 point

• A. It is possible to write a program with threads that deadlocks due to the use of JOIN operations.
• B. Threads start executing as soon as they are created.
• C. Threads can only be executed on multicore systems.
• D. Threads provide a low-level foundation (like an “assembly language”) for parallelism.

3.
Question 3
According to Lecture 1.2, which of the following statements are true?

Please choose all options that are correct.

1 point

• A. Parallel programs written using structured locks are guaranteed to be free from deadlocks.
• B. A structured lock construct is responsible for both acquiring and releasing the lock that it works with.
• C. Additional synchronization can be performed using wait and notify operations with structured locks.

4.
Question 4
Consider a parallel program with two structured locks, L1 and L2, (as introduced in Lecture 1.2), and 100 threads that are started in parallel such that each of them executes the following snippet of code:

12345
synchronized(L1) {
synchronized(L2) {
S1
}
}
The statement “This program is guaranteed to be free from deadlocks” is:

1 point

• A. True
• B. False

5.
Question 5
Which of the following are true regarding unstructured read-write locks introduced in Lecture 1.3?

Please choose all options that are correct.

1 point

• B. A read-write lock allows concurrent access for write operations, allowing multiple threads to write shared data in parallel.
• C. When a thread, T, has obtained a write lock, all other reader and writer threads will be blocked until thread T completes its write operation and releases its write lock.

6.
Question 6
Which of the following best describe the differences between structured and unstructured locks introduced in Lectures 1.2 and 1.3?

Please choose all options that are correct.

1 point

• A. Structured locking only supports nested locking paradigms, whereas unstructured locking also supports “hand-over-hand” locking.
• B. Structured locking always blocks a thread at a synchronization point if the lock is not available, whereas unstructured locking provides an API that allows that thread to perform other meaningful tasks if the lock is unavailable.
• C. Structured locking supports the distinction between reader and writer threads, and allows multiple reader threads to access the lock in parallel (so as to read shared data in parallel).

7.
Question 7
Please classify the following five scenarios, each employing two threads (T1 and T2) running concurrently, as possibly leading to deadlock, livelock or neither. Recall that deadlock and livelock were discussed in Lecture 1.4.

1 point

• B. deadlock, neither, livelock, neither, neither (for the 5 scenarios)
• C. livelock, deadlock, livelock, neither, livelock (for the 5 scenarios)

8.
Question 8
What is the difference between deadlock and livelock? (Recall that deadlock and livelock were discussed in Lecture 1.4.)

1 point

• A. Threads in deadlock will block indefinitely because each is waiting on the other, while threads in livelock can continue execution but make no meaningful progress.
• B. Threads in livelock will block indefinitely because each is waiting on the other, while threads in deadlock can continue execution but make no meaningful progress.

9.
Question 9
Many algorithms have been proposed to address the Dining Philosophers problem introduced in Lecture 1.5. In this problem, you will evaluate one such algorithm.

The following simple algorithm can deadlock with all philosophers holding their left chopsticks.

1
So, we develop a slight variant that attempts to avoid this problem — adjacent philosophers pick up the chopsticks in the opposite order. More precisely, we propose separate algorithms for the even-numbered and odd-numbered philosophers. Assume the philosophers are numbered 1, …, n clockwise around the table.

What liveness issues could arise if we have an even number of philosophers? What about if we had an odd number of philosophers?

1 point

• A: Deadlock if n is even, Livelock if n is odd
• B: No liveness issues, regardless of n
• C: Livelock, regardless of n
• D: Livelock if n is even, Deadlock if n is odd
• E: Deadlock if n is even, no liveness issues if n is odd

10.
Question 10
Towards the end of Lecture 1.5, Dr. Sarkar mentioned that deadlock and livelock can be avoided in the Dining Philosophers problem by modifying the “all acquire left fork first” algorithm such that n-1 philosophers attempt to acquire their left fork first, and 1 philosopher attempts to acquire its right fork first. However, nothing was mentioned about the impact of this modification on another important liveness issue: starvation. How could starvation occur, or not occur, with this modification presented at the end of Lecture 1.5?

1 point

• A. Starvation can occur because, although it is guaranteed that some philosophers can eat at any given time, the modification does not ensure that every philosopher at the table gets a chance to eat.
• B. Starvation can occur because, with this modification, all philosophers may be forced to wait idly for chopsticks currently held by other philosophers.
• C. Starvation doesn’t occur because the modification ensures an upper bound on the time a philosopher at the dining table must wait before getting a turn to eat.

## Week- 2

### Module 2 Quiz

1.
Question 1
Consider the following example algorithm, taken from the lecture video:

123456789
MYBAL = MYBAL – 100
FAMILY(W1) = FAMILY(R1) + 100
}

FAMILY(W2) = FAMILY(R2) – 100
DBAL = DBAL + 100
}
We denote the read and write for FAMILY in thread 1 as R1 and W1, and the read and write for FAMILY in thread 2 as R2 and W2. Which of the following is not a possible ordering of reads and writes for the family’s account balance (FAMILY)?

1 point

A. R1, W1, R2, W2

B. R1, R2, W2, W1

C. R2, W1, R1, W2

D. R1, R2, W1 W2

E. R2, W2, R1, W1

2.
Question 2
Consider the following example algorithm, taken from the lecture video:

123456789
MYBAL = MYBAL – 100
FAMILY(W1) = FAMILY(R1) + 100
}

FAMILY(W2) = FAMILY(R2) – 100
DBAL = DBAL + 100
}
The beginning balance of the FAMILY account is 1000. We denote the read and write for FAMILY in thread 1 as R1 and W1, and the read and write for FAMILY in thread 2 as R2 and W2. Which of the following is not a possible value that the family account can end up with?

1 point

A.800

B. 900

C. 1000

D. 1100

3.
Question 3
Assume we have the doubly-linked list shown below. Using object-based isolation, which sets of object deletions can occur simultaneously?

Please choose all options that are correct.

1 point

A. (A, D)

B. (A, C, F)

C. (B, C, F)

D. (B, E)

4.
Question 4
Assume we are using the provided bank transaction code. We have six accounts: A, B, C, D, E, F We make eight transfers, all asynchronously:

1. $100 from A to B 2.$50 from B to C

3. $30 from B to C 4.$20 from B to A

5. $70 from D to A 6.$40 from B to D

7. $30 from E to D 8.$20 from D to F

Each transaction requires one computation step worth of time, but multiple transactions can run in parallel in the same computation step. Given enough processors, what is the minimum number of computation steps needed to run these eight transactions without causing a data race?

1 point

 67

5.
Question 5
Consider the following pseudocode snippet for computing the spanning tree in parallel. You may assume that for every vertex in the graph, parent is initialized to null.

123456789101112131415161718192021222324
compute(v) {
for each neighbor n of v {
if makeParent(v, n) {
async compute(n)
}
}
}

boolean makeParent(v, c) {
isolated(c) {

For a complete graph with 4 vertices, what is the WORK of this algorithm?

1 point

A. 3

B. 4

C. It depends on the order the node neighbors are selected.

D. It depends on what gets executed in parallel.

6.
Question 6
If we re-run the algorithm from Question 5 on the spanning tree (which is also a graph) that is produced in Question 5, what will be the total WORK of the algorithm?

1 point

A. 3

B. 4

C. It depends on which vertex is selected to start the tree.

D. It depends on what gets executed in parallel.

7.
Question 7
Consider the following pseudocode snippet:

123456789
finish {
AtomicReference r = new AtomicReference()
async {
println(r.get())
}
forall i in 0..1000 {
r.compareAndSet(null, i)
}
}
What is most accurate statement regarding the value of r.get() printed by the async?

1 point

A. The string is null

B. Any value between 0 and 1000.

C. The string is null or any value between 0 and 1000.

8.
Question 8
Now, consider the pseudocode snippet below. Recall that a “forall” loop creates a separate async for every iteration of the loop.

What is most accurate statement regarding the value of r.get() printed by the async?

1 point

A. The string null

B. Any value between 0 and 1000.

C. The string is null or any value between 0 and 1000.

9.
Question 9
Assume we have defined objects x and y. The following pseudo-code uses object isolation on three blocks of code, A, B, and C.

123456789
async {
isolated (x) A
}
async {
isolated (x) B
}
async {
isolated (y) C
}
Which code blocks can run simultaneously?

Please choose all options that are correct.

1 point

A. A and B, but not also C

B. A and C, but not also B

C. B and C, but not also A

D. A, B, and C

10.
Question 10
Assume we have defined object y. The following pseudo-code uses object isolation on three blocks of code, D, E, and F.

}
Which code blocks can run simultaneously?

1 point

A. D and E, but not also F

B. D and F, but not also E

C. E and F, but not also D

D. D, E, and F

11.
Question 11
Let’s say that you have some additional information: blocks D and E actually write y and block F only reads y. However, we are leaving the isolated declarations as is. How does this change the previous answer?

1 point

A. No change.

B. It reverses all the True/False answers.

C. The code is erroneous and won’t run.

## Week- 3

### Module 3 Quiz

1.
Question 1
What advantage do actors have over object-based isolation?

1 point

A. Avoids data races

C. Increased CPL

D. Reduces programmer error by making a variable isolated by default

2.
Question 2
How does an actor interact with the local state?

1 point

A. Using parallel threads to access the local state variable

B. Using predefined message methods in that subclass of actor

C. Using only the methods INCREMENT, DECREMENT, EXIT

D. Using message methods the programmer defines for that subclass of actor

3.
Question 3
In the Actor model, message ordering can be preserved in which of the following cases?

1 point

A. Never

4.
Question 4
How many actors do you need in an actor-based pipeline?

1 point

A. One

B. One actor per pipeline stage

C. One actor per actor sublcass

5.
Question 5
For generating the primes less than n, how many actors will the Sieve of Eratosthenes use?

1 point

A. 1

B. \sqrt{n}
n

C. n – 1n−1

D. nn

E. Number of primes less than nn

6.
Question 6
Which of the following statements is/are true regarding the sieve pipeline introduced in the video?

Please choose all options that are correct.

1 point

A. The pipeline grows dynamically

B. Each next actor in the pipeline is created and started by the previous actor

C. What numbers get filtered in the next stage of the pipeline is arbitrarily determined

7.
Question 7
Which of the following would be good objects to use to implement an unbounded buffer in Java?

Please choose all options that are correct.

1 point

A. PriorityBlockingQueue

B. SynchronousQueue

D. ArrayBlockingQueue

8.
Question 8
Why is it beneficial to model producers, consumers and their unbounded buffer as actors?

Please choose all options that are correct.

1 point

A. Actors are more efficient than alternatives like waiting on a while loop condition to evaluate to true.

B. There is no need for producers to check whether the buffer is full, or for consumers to check whether the buffer is empty.

C. The consumer can remove items from the buffer at will.

D. The producer must coordinate with the master actor when it’s ready to insert items in the buffer.

9.
Question 9
P is the number of producer actors and C is the number of consumer actors. What can we assume about the size of the buffer (B)?

1 point

A. B < PB<P and B < CB<C

B. B < PB<P or B < CB<C

C. P \leq BP≤B

D. C \leq BC≤B

10.
Question 10
The bounded buffer master actor coordinates with the producer actors and consumer actors by…

Please choose all options that are correct.

1 point

A. Requesting data from a producer actor

B. Waiting for a producer actor to send it data

C. Requesting removal of data by consumer actors

D. Waiting for a consumer actor to tell the buffer that it is ready

## Week- 4

### Module 4 Quiz

1.
Question 1
What is a potential downside of the shown getAndAdd() function, which employs optimistic concurrency in the following code block?

1234567
while (true) {
cur = this.get();
next = cur + delta;
if (this.compareAndSet(cur, next) return cur;
}
}
1 point

A. It may perform many unused computations.

B. It requires the use of expensive locks.

D. It may livelock.

2.
Question 2
Under what circumstances might optimistic concurrency be a good strategy when designing a concurrent algorithm?

1 point

A. Computation on the shared object is very expensive compared to the overhead of locks.

B. You expect very low contention.

C. The optimistically computed operation has side effects.

3.
Question 3
Which variables in an implementation of a sequential queue would need to be handled differently in a concurrent implementation?

1 point

A. Since not all variables would be part of a data race, only those that would be part of a data race.

B. Since all variables would be part of a data race, all variables.

C. All variables that are used in an enqueue operation.

D. All variables that are used in a dequeue operation.

E. None of the above.

4.
Question 4
What’s the best way to modify TAIL.NEXT in a concurrent implementation of ENQ(X)?

1 point

A. LOCK(X) { TAIL.NEXT = X }

B. ISOLATED(X) {TAIL.NEXT = X }

C. TAIL.NEXT.COMPAREANDSET(TAIL, X)

D. TAIL.NEXT.COMPAREANDSET(NULL, X)

E. Both A and B

5.
Question 5
Consider the following timeline. Assuming the enqueue operations are linearizable, which of the below are possible results from a sequence of dequeue operations?

Please choose all options that are correct.

1 point

A. a, m, x, b, n, y

B. m, x, a, n, b, y

C. m, x, y, a, b, n

D. x, a, b, m, n, y

E. x, a, y, m, n, b

F. x, a, y, b, m, n

6.
Question 6
Consider the scenario where threads T1 and T2 (and no other threads) are attempting to obtain a lock on L1. Which of the following are linearizable orderings of statements executed?

Please choose all options that are correct.

1 point

A. 1) T1 calls L1.lock(). 2) T2 calls L1.lock(). 3) T2 is unable to obtain lock. 4) T1 successfully obtains lock.

B. 1) T1 calls L1.lock(). 2) T2 calls L1.lock(). 3) T1 is unable to obtain lock. 4) T2 successfully obtains lock.

C. 1) T1 calls L1.lock(). 2) T2 calls L1.lock(). 3) T1 successfully obtains lock. 4) T2 is unable to obtain lock.

D. 1) T1 calls L1.lock(). 2) T2 calls L1.lock(). 3) T2 successfully obtains lock. 4) T1 is unable to obtain lock.

E) 1) T1 calls L1.lock(). 2) T2 calls L1.lock(). 3) T2 successfully obtains lock. 4) T1 successfully obtains lock.

F. 1) T1 calls L1.lock(). 2) T2 calls L1.lock(). 3) T1 is unable to obtain lock. 4) T2 is unable to obtain lock.

7.
Question 7
If multiple threads are trying to write a value in the map for the same key using PUTIFABSENT(key, value), only one of the threads will succeed.

1 point

True

False

8.
Question 8
Which of the following operations are not linearizable?

Please choose all options that are correct.

1 point

A. PUT(key, value)

B. PUTIFABSENT(key, value)

C. PUTALL()

D. CLEAR()

E. GET (key)

9.
Question 9
What is a possible downside of using locks to transform a sequential MST algorithm into a concurrent algorithm?

1 point

A. As our merged tree gets smaller, we have more collisions when using trylock, thus reducing performance.

B. This method may not account for all data races.

C. It results in a deadlock.

D. The merging step could result in two processes attempting to merge the same nodes and thus result in a bug.

E. There would be more code, which would hurt our brains.

10.
Question 10
Is it possible for a minimum spanning tree to have the same total weight as its original graph?

1 point

A. Yes

B. No