Concurrency & Parallelism MCQs for the NSCT
Concurrency and parallelism cover how a program runs several tasks at once: creating threads, protecting shared data with locks and semaphores, avoiding deadlock, and estimating speed-up from extra cores. It is topic 14 of the 16 in the Programming (C++/Java/Python) area, which carries 10% of the NSCT according to HEC's Areas of Competencies. Candidates averaged 47.8% accuracy in this area in 2026, per the HEC NSCT report.
The topic is introductory here. Operating Systems covers scheduling and synchronisation in more depth, and both follow Silberschatz's terms, so learning them once pays off twice.
Threads
A thread is a single sequence of execution inside a process. Threads of the same process share its code, heap, global variables and open files. Each thread has its own stack, program counter and registers.
| Process | Thread | |
|---|---|---|
| Memory | Own address space | Shares the process's address space |
| Creation cost | Higher | Lower |
| Communication | Pipes, sockets, shared memory set up explicitly | Directly through shared variables |
| A crash affects | That process | The whole process |
import threading
def task():
print("child")
t = threading.Thread(target=task)
t.start() # begins a new thread
t.join() # waits for it to finish
print("main") # always printed after "child"
In Java, t.start() creates the thread, while calling t.run() directly just runs the method in the current thread.
Exam angle: without join(), the order of output from different threads is not guaranteed. Questions that ask for "the output" usually contain a join() or a lock that fixes the order.
Locks
A race condition happens when threads access shared data, at least one writes, and the result depends on timing. count += 1 is three steps (read, add, write), so two threads can read the same value and lose an update. The code that touches shared data is the critical section, and a lock (mutex) makes it mutually exclusive:
lock = threading.Lock()
def work():
global count
for _ in range(1000):
with lock: # only one thread at a time
count += 1
Other tools you should recognise:
| Tool | What it does |
|---|---|
Mutex / Lock |
One owner at a time |
RLock (reentrant lock) |
The owner may acquire it again without blocking |
| Counting semaphore | Lets up to N threads in; wait/P decrements, signal/V increments |
Java synchronized |
Locks the object's monitor around a method or block |
Java volatile |
Visibility of writes only; count++ is still not atomic |
AtomicInteger |
Atomic read-modify-write operations |
Deadlock needs all four Coffman conditions at once: mutual exclusion, hold and wait, no preemption and circular wait. The usual fix in code is a fixed lock order, which breaks circular wait. Compare deadlock with livelock, where threads stay active but make no progress, and starvation, where one thread waits indefinitely while others proceed.
Exam angle: "two threads lock A and B in opposite orders" is the standard deadlock question. The answer is to lock in the same order everywhere.
Parallel vs concurrent
Concurrency means several tasks are in progress over the same period. One core can do this by interleaving them. Parallelism means tasks execute at the same instant on several cores. A concurrent program may or may not run in parallel.
Three examples show the difference:
- Two threads on a single core are concurrent but not parallel.
- Python's asyncio is concurrency on one thread. While one coroutine waits on I/O, another runs.
- In standard CPython, the Global Interpreter Lock (GIL) lets one thread run Python bytecode at a time. CPU-bound threads therefore do not run in parallel; use multiprocessing instead. Python 3.13 and later offer an optional free-threaded build.
Amdahl's law limits the speed-up from N processors when only a fraction p of the work can be parallelised:
Speed-up = 1 / ((1 − p) + p / N)
| p (parallel) | N = 2 | N = 4 | N → ∞ |
|---|---|---|---|
| 50% | 1.33 | 1.6 | 2 |
| 70% | 1.54 | 2.11 | 3.33 |
| 90% | 1.82 | 3.08 | 10 |
Exam angle: the limit as N grows is 1 / (1 − p). If 10% of a program is serial, it can never run more than 10 times faster.
Common traps
- Treating concurrency and parallelism as the same thing. One core can be concurrent but never parallel.
- Assuming
volatilemakescount++safe. It gives visibility, not atomicity. - Locking only some of the accesses to shared data. A lock protects data only if every access uses it.
- Calling
run()instead ofstart()in Java, which creates no new thread. - Expecting CPU-bound Python threads to use all cores under the GIL.
- Applying Amdahl's law to the whole program when only part of it is parallel, or adding speed-ups instead of using the formula.
Worked examples
1. Understanding. Why is creating a thread usually cheaper than creating a process? Answer: a new thread shares its process's address space, code and open files, so the OS only allocates a stack and a register context. A new process needs its own address space and resource tables.
2. Application. 95% of a program is parallelisable. What is the speed-up on 8 cores, and what is the maximum on any number of cores? Answer: 1 / (0.05 + 0.95/8) = 1 / 0.16875 ≈ 5.93. The maximum is 1 / 0.05 = 20.
3. Analysis. Thread A updates count inside with lock:, but Thread B updates count without taking the lock. Is the counter safe?
Answer: no. A lock only excludes threads that also try to acquire it, so B can still interleave with A's read-add-write and updates can be lost. Every access to the shared variable must use the same lock.
Practise
The quiz below has 100 questions on concurrency and parallelism, filterable by tier and difficulty.
Practise: 100 questions
FAQs
What is the difference between a mutex and a binary semaphore?
Both allow one thread at a time. A mutex has an owner: the thread that locked it should be the one to unlock it, and reentrant variants let the owner lock it again. A binary semaphore has no owner, so any thread may signal it, which makes it suitable for signalling between threads as well as for mutual exclusion.
Is Python good for parallel programming?
For I/O-bound work, threads and asyncio work well, because waiting releases the GIL. For CPU-bound work in standard CPython, use multiprocessing or concurrent.futures.ProcessPoolExecutor. Each process has its own interpreter, so they can use several cores. Libraries such as NumPy also release the GIL inside their compiled code.
How can deadlock be prevented in code?
Break at least one of the four Coffman conditions. In practice, programmers usually do one of three things. They acquire locks in a fixed global order, which removes circular wait. They acquire all needed locks at once, or give up and retry with a timeout, which removes hold and wait. Or they avoid holding a lock while calling unknown code.
Next step
Continue to Debugging, Testing & Optimization MCQs, where race conditions return as hard-to-find bugs. Revisit the Programming MCQs hub or test yourself in an NSCT mock test.