Sample questions
Question 1Easy
The section of code in which a process accesses shared data is called the:
- Entry section
- Remainder section
- Exit section
- Critical section
Answer: D. Critical section
Explanation
Only one process at a time should execute its critical section for the same shared data.
Step-by-step solution
- Code that touches shared data is the critical section.
- Entry and exit sections surround it.
Why the other options are wrong
- A. Entry section requests permission.
- B. Remainder section is other code.
- C. Exit section releases.
Question 2Easy
A race condition can be prevented by:
- Adding more threads
- Making the accesses to shared data mutually exclusive
- Using a faster CPU
- Increasing the time quantum of the scheduler
Answer: B. Making the accesses to shared data mutually exclusive
Explanation
Races arise from unsynchronised interleavings; locks, semaphores or atomic operations serialise the critical accesses.
Step-by-step solution
- A race condition happens when threads access shared data at the same time and the result depends on timing.
- Making those accesses mutually exclusive means only one thread touches the data at a time.
- So option B prevents the race.
Why the other options are wrong
- A. More threads add more competing accesses, not fewer.
- C. A faster CPU changes timing but does not stop the race.
- D. A longer time quantum does not stop two threads touching the same data.
Tip: Race condition fix = mutual exclusion on shared data.
Question 3Easy
- Prevent context switches
- Allocate memory to threads
- Protect a single shared variable from concurrent writes
- Make a group of threads wait until all of them reach the same point
Answer: D. Make a group of threads wait until all of them reach the same point
Explanation
No thread passes the barrier until every participating thread has arrived.
Step-by-step solution
- Barrier ensures all threads reach a point before proceeding.
- Threads pause at barrier until everyone arrives.
- This synchronizes group of threads together.
- It does not protect variables or allocate memory.
- Final answer: Make a group of threads wait until all of them reach the same point.
Why the other options are wrong
- A. A barrier does not prevent context switches; threads still switch and simply wait at the barrier point.
- B. Memory allocation is unrelated to a barrier, which only makes threads wait until all of them reach the point.
- C. Protecting one variable from concurrent writes is the job of a lock, not a barrier.
Tip: Remember barriers are for thread synchronization, not memory protection.
Question 4Easy
A counting semaphore guarding a pool of 5 printers is initialised to:
- 5
- 0
- 4
- 1
Answer: A. 5
Explanation
The initial value equals the number of available instances; each wait takes one printer and each signal returns one.
Step-by-step solution
- A counting semaphore counts the free units of a resource pool.
- With 5 printers, all 5 are free when the system starts.
- So the semaphore is initialised to 5, option A.
Why the other options are wrong
- B. 0 would block every process before it could use a printer.
- C. 4 would wrongly hide one free printer.
- D. 1 is the initial value of a binary semaphore or mutex, not a pool of 5.
Tip: Counting semaphore starts at the number of free units.
Question 5Medium
Which of the three critical-section requirements says a process wanting to enter must not wait forever while others enter repeatedly?
- Bounded waiting
- Progress
- Mutual exclusion
- Atomicity
Answer: A. Bounded waiting
Explanation
Bounded waiting limits how many times others can enter ahead of a waiting process, preventing starvation.
Step-by-step solution
- The three critical-section requirements are mutual exclusion, progress and bounded waiting.
- Bounded waiting says a process wanting to enter must not wait forever while others enter repeatedly.
- That is bounded waiting, option A.
Why the other options are wrong
- B. Progress is about choosing the next entrant when the section is empty.
- C. Mutual exclusion says at most one process is inside.
- D. Atomicity is not one of the three critical-section requirements.
Tip: Progress = someone gets in; bounded waiting = nobody waits forever.
Question 6Medium
In the readers-writers problem (first variant), which statement is correct?
- Only one reader may read at a time
- Writers have priority, and new readers may start reading only when no writer is waiting for access
- Many readers may read at the same time, but a writer needs exclusive access
- Readers and writers may always run together
Answer: C. Many readers may read at the same time, but a writer needs exclusive access
Explanation
The first readers-writers problem favours readers, so writers may starve; the second variant favours writers.
Step-by-step solution
- First variant allows multiple readers to read simultaneously.
- A writer needs exclusive access when it is their turn.
- Readers can read even if a writer is waiting, as long as no writer is active.
- Writers are not given priority in the first variant.
- Only one writer can write at a time, but many readers may read.
- Correct answer: Many readers may read at the same time, but a writer needs exclusive access.
Why the other options are wrong
- A. Only one reader at a time is wrong; the first variant lets many readers read simultaneously.
- B. The first variant does not give writers priority; that is the second variant, where waiting writers block new readers.
- D. Readers and writers cannot run together while a writer is active, because a writer needs exclusive access.
Tip: Remember: First variant favours readers, allowing many to read at once but giving writers exclusive access when they need it.
Question 7Medium
A counting semaphore starts at 3. Five processes call wait() on it, one after another, without any signal(). How many processes are blocked?
- 5
- 0
- 2
- 3
Answer: C. 2
Explanation
The first three waits decrement the value to 0 and proceed; the remaining two block (the value becomes -2 in the classic implementation).
Step-by-step solution
- Semaphore starts at 3.
- First wait reduces it to 2.
- Second wait reduces it to 1.
- Third wait reduces it to 0.
- Fourth wait blocks as value is -1.
- Fifth wait blocks as value is -2.
Why the other options are wrong
- A. Only waits that find the value below zero block; with 3 units the first three calls succeed without blocking.
- B. 0 is wrong; the value drops below zero on the 4th wait, so the 4th and 5th calls block.
- D. 3 is the starting count; the value reaches 0 after three waits, and then the 4th and 5th calls block.
Tip: Count down from initial value; any wait beyond that blocks.
Question 8Medium
A spinlock (busy-waiting lock) is most appropriate when:
- There is only one thread in the program
- Locks are held for a long time on a single-core system
- The critical section performs disk I/O
- Locks are held for very short times on a multiprocessor system
Answer: D. Locks are held for very short times on a multiprocessor system
Explanation
Spinning wastes CPU, but avoids the cost of a context switch. That is a good trade only when the wait is shorter than a context switch and another core is releasing the lock.
Step-by-step solution
- Spinlock uses busy waiting, which wastes CPU time.
- Context switches are costly but avoided with spinlocks.
- Spinlocks work best when locks are held for very short times.
- Multiprocessor systems have multiple cores to release locks.
- Locks held for very short times on a multiprocessor system is the best fit.
Why the other options are wrong
- A. With only one thread there is no contention, so a lock is unneeded and spinning gives no benefit.
- B. On a single core, spinning wastes the only CPU while the lock holder cannot run, so spinlocks suit short holds only.
- C. Disk I/O holds the lock for a long time, so spinning would waste CPU; a blocking lock is the better choice.
Tip: Use spinlocks for short lock holds on multi-core systems to save context switch costs.
Question 9Hard
Two threads each run "for i in 1..5: x = x + 1" on a shared x (initially 0) with no locking; each increment is a separate load, add and store. What is the smallest possible final value of x?
- 2
- 5
- 1
- 0
Answer: A. 2
Explanation
Thread A loads 0, B does 4 full increments, A stores 1; B loads 1, A does its last 4 increments, B stores 2. The value can never be below 2.
Step-by-step solution
- Each thread stores its loaded value plus 1, so every store writes at least 1 and 0 is impossible.
- Interleaving can let one thread overwrite the other's work with a stale, low value.
- Checking every interleaving of the two 5-step threads by program gives a smallest final value of 2.
- So the smallest possible final value is 2, option A.
Why the other options are wrong
- B. 5 is reachable but it is not the smallest possible value.
- C. 1 cannot be reached in any interleaving; the minimum is 2.
- D. 0 is impossible because every store writes at least 1.
Tip: With lost updates, count the smallest value by letting one thread overwrite the other with stale values.
Question 10Hard
A strict-alternation solution uses a shared variable turn, and process i enters only when turn == i. Which requirement does it violate?
- Mutual exclusion
- None of them
- Bounded waiting
- Progress
Answer: D. Progress
Explanation
If it is P0's turn but P0 is in its remainder section, P1 cannot enter even though the critical section is free.
Step-by-step solution
- Strict alternation forces the turn to pass back and forth.
- If P0 is in its remainder section, P1 cannot enter even if free.
- This violates progress.
Why the other options are wrong
- A. Mutual exclusion is preserved.
- B. It does violate one.
- C. Bounded waiting is not the main failure here.
Practise all 45 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →