AlgoMaster Logo

Multithreading - Quiz

Last Updated: December 6, 2025

Multithreading Exercises

32 quizzes

1
Code Completion

Start a new thread that executes the given Runnable task

java
1
Thread worker = new Thread(task); worker.();

Click an option to fill the blank:

2
Code Completion

Ensure the current thread waits until another thread has finished before continuing

java
1
worker.();

Click an option to fill the blank:

3
Code Completion

Mark a shared boolean so that updates are immediately visible to other threads

java
1
private boolean running = true;

Click an option to fill the blank:

4
Multiple Choice

Which statement best describes a Java thread?

5
Multiple Choice

When implementing Runnable, where do you put the code that should run in the new thread?

6
Multiple Choice

Which state is a thread in immediately after new Thread(runnable) is created but before start() is called?

7
Multiple Choice

Which method should you use to pause the current thread for 100 milliseconds?

8
Multiple Choice

What is the default priority for a newly created Java thread?

9
Multiple Choice

Why is synchronization needed when multiple threads update a shared counter?

10
Multiple Choice

What happens if two threads synchronize on different lock objects when accessing the same field?

11
Multiple Choice

Which keyword ensures visibility of writes to a field across threads without using explicit locking?

12
Multiple Choice

Which situation most likely indicates a deadlock?

13
Multiple Choice

Which ExecutorService method stops accepting new tasks but lets submitted tasks finish?

14
Multiple Choice

What is a key advantage of using Callable over Runnable?

15
Multiple Choice

Which statement about CompletableFuture is true?

16
Sequencing

Order the steps to safely update a shared counter using ReentrantLock

Drag and drop to reorder, or use the arrows.

17
Sequencing

Order the steps to use a CountDownLatch so a main thread waits for workers to finish

Drag and drop to reorder, or use the arrows.

18
Output Prediction

What is the output of this code involving thread state?

1public class StateDemo {
2    public static void main(String[] args) throws Exception {
3        Thread t = new Thread(() -> {});
4        System.out.println(t.getState());
5    }
6}
19
Output Prediction

What is the output regarding thread priority?

1public class PriorityDemo {
2    public static void main(String[] args) {
3        Thread t = new Thread(() -> {});
4        System.out.println(t.getPriority());
5    }
6}
20
Output Prediction

What is the single line printed by this synchronized method example?

1public class SyncDemo {
2    private int value = 10;
3    public synchronized int getValue() { return value; }
4    public static void main(String[] args) {
5        SyncDemo d = new SyncDemo();
6        System.out.println(d.getValue());
7    }
8}
21
Output Prediction

What does this ExecutorService example print?

1import java.util.concurrent.*;
2public class ExecDemo {
3    public static void main(String[] args) throws Exception {
4        ExecutorService ex = Executors.newSingleThreadExecutor();
5        Future<Integer> f = ex.submit(() -> 7 * 3);
6        System.out.println(f.get());
7        ex.shutdown();
8    }
9}
22
Output Prediction

What single line does this CompletableFuture chain print?

1import java.util.concurrent.*;
2public class CFUDemo {
3    public static void main(String[] args) throws Exception {
4        CompletableFuture<String> f = CompletableFuture.supplyAsync(() -> "Hi")
5            .thenApply(s -> s + " there");
6        System.out.println(f.get());
7    }
8}
23
Bug Spotting

Find the concurrency bug in this shared counter code

Click on the line(s) that contain the bug.

java
1
public class Counter {
2
    private int value = 0;
3
    public void increment() {
4
        value = value + 1;
5
    }
6
    public int get() {
7
        return value;
8
    }
9
}
24
Bug Spotting

Find the bug in this code that uses wait/notify for thread communication

Click on the line(s) that contain the bug.

java
1
public class QueueDemo {
2
    private final Object lock = new Object();
3
    private boolean ready = false;
4
    public void awaitReady() throws InterruptedException {
5
        if (!ready) {
6
            lock.wait();
7
        }
8
    }
9
    public void signalReady() {
10
        synchronized (lock) {
11
            ready = true;
12
            lock.notifyAll();
13
        }
14
    }
15
}
25
Matching

Match each concurrency utility to its primary purpose

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Matching

Match each lock-related concept with its description

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

27
Fill in the Blanks

Complete the code to create a fixed-size thread pool and submit a task

java
1
import java.util.concurrent.*;
2
public class PoolExample {
3
public static void main(String[] args) {
4
ExecutorService executor = Executors.(4);
5
executor.(() -> System.out.println("Task"));
6
executor.shutdown();
7
}
8
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code so multiple readers can access data while writers are exclusive

java
1
import java.util.concurrent.locks.*;
2
public class Stats {
3
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
4
private int value;
5
public int read() {
6
lock.().lock();
7
try {
8
return value;
9
} finally {
10
lock.().unlock();
11
}
12
}
13
public void write(int v) {
14
lock.().lock();
15
try {
16
value = v;
17
} finally {
18
lock.().unlock();
19
}
20
}
21
}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the atomic counter code to increment safely without synchronization

java
1
import java.util.concurrent.atomic.*;
2
public class AtomicCounter {
3
private final AtomicInteger counter = new AtomicInteger(0);
4
public void inc() {
5
counter.();
6
}
7
public int get() {
8
return counter.();
9
}
10
}

Click an option to fill blank 1:

30
Fill in the Blanks

Complete the code to create and start a virtual thread that runs a task

java
1
public class VirtualDemo {
2
public static void main(String[] args) {
3
Thread vt = Thread.().(() -> System.out.println("virtual"));
4
}
5
}

Click an option to fill blank 1:

31
Hotspot Selection

Click the line where a deadlock risk is introduced due to inconsistent lock ordering

Click on the line to select.

java
1
public class DeadlockRisk {
2
    private final Object lockA = new Object();
3
    private final Object lockB = new Object();
4
    public void method1() {
5
        synchronized (lockA) {
6
            synchronized (lockB) {
7
                System.out.println("m1");
8
            }
9
        }
10
    }
11
    public void method2() {
12
        synchronized (lockB) {
13
            synchronized (lockA) {
14
                System.out.println("m2");
15
            }
16
        }
17
    }
18
}
32
Hotspot Selection

Click the line that can cause threads to spin without proper blocking in this busy-wait example

Click on the line to select.

java
1
public class VolatileFlag {
2
    private volatile boolean running = true;
3
    public void runLoop() {
4
        while (running) {
5
            // work
6
        }
7
    }
8
    public void stop() {
9
        running = false;
10
    }
11
}

Premium Content

Subscribe to unlock full access to this content and more premium articles.