Java Multithreading
Test your knowledge of threads, lifecycle, creating threads, daemon threads, thread priorities, and the basics of concurrent execution.
Questions in this quiz
20 questions on Java Multithreading. Work through them here, then start the quiz to answer them and see your score.
1.What is a thread in Java?
- AA separate JVM process with its own memory
- BA connection to an external service
- CThe lightest unit of execution — a path of execution within a process, sharing the process's memory
- DA queued task waiting in a thread pool
2.What are the two ways to create a thread in Java?
- AExtend Thread or implement Callable
- BUse new Thread() or use CompletableFuture
- CImplement Runnable or implement Executor
- DExtend Thread or implement Runnable
3.What does this code print (approximately)?
public class Main { public static void main(String[] args) throws InterruptedException { Thread t = new Thread(() -> { for (int i = 0; i < 3; i++) System.out.println("Thread: " + i); }); t.start(); for (int i = 0; i < 3; i++) System.out.println("Main: " + i); t.join(); } }- AAlways: Main:0, Main:1, Main:2, Thread:0, Thread:1, Thread:2
- BMain and Thread lines interleaved in non-deterministic order
- CCompilation error — lambda cannot be passed to Thread
- DAlways: Thread:0, Thread:1, Thread:2, Main:0, Main:1, Main:2
4.What is the difference between start() and run()?
- Astart() creates a new OS thread and eventually calls run() in it; calling run() directly executes on the current thread
- Brun() creates the thread; start() begins execution
- Cstart() is for Runnable; run() is for Thread subclasses
- DThey are identical — start() just calls run()
5.What are the states in the Java thread lifecycle?
- AINIT, ACTIVE, IDLE, STOPPED
- BCreated, Running, Sleeping, Dead
- CREADY, EXECUTING, PAUSED, FINISHED
- DNEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED
6.What does Thread.sleep() do?
- AMoves the thread to the back of the ready queue
- BPauses the current thread for at least the specified milliseconds, entering TIMED_WAITING state
- CReleases all locks held by the thread
- DPermanently suspends the thread
7.What does this code print?
public class Main { public static void main(String[] args) throws InterruptedException { Thread t = new Thread(() -> { System.out.println("Thread state during run: " + Thread.currentThread().getState()); }); System.out.println("Before start: " + t.getState()); t.start(); t.join(); System.out.println("After join: " + t.getState()); } }- ANEW BLOCKED TERMINATED
- BNEW RUNNABLE RUNNABLE
- CNEW RUNNABLE TERMINATED
- DRUNNABLE RUNNABLE TERMINATED
8.What is a daemon thread?
- AA thread that runs system-level operations
- BA thread that cannot be interrupted
- CA thread with the highest priority
- DA background thread that the JVM terminates automatically when all non-daemon threads have finished
9.What is a race condition?
- AA deadlock between two threads competing for resources
- BWhen the outcome of a program depends on the unpredictable timing of multiple threads accessing shared mutable state
- CA thread running faster than another thread
- DWhen two threads finish at exactly the same time
10.What does this code risk?
public class Counter { private int count = 0; public void increment() { count++; } public int getCount() { return count; } } // Two threads both call increment() 1000 times each- AStackOverflowError from concurrent calls
- BA race condition — count++ is three operations (read, add, write) and threads can interleave, giving a final count less than 2000
- CA deadlock between the two threads
- DNothing — count++ is atomic
11.What does Thread.currentThread() return?
- AThe parent thread that started the current thread
- BThe thread with the highest priority
- CA reference to the currently executing thread
- DThe main thread always
12.What is thread priority in Java?
- AThe order in which threads were started
- BThe amount of CPU time allocated per thread
- CA hint to the OS scheduler (1–10 scale) that may influence scheduling — but not guaranteed
- DA guarantee that higher-priority threads always run first
13.What does Thread.yield() do?
- AMoves the thread to WAITING state indefinitely
- BHints that the current thread is willing to pause and let other threads of equal priority run
- CTerminates the current thread
- DReleases all monitors held by the current thread
14.What does this code print?
public class Main { public static void main(String[] args) { Runnable task = () -> System.out.println( Thread.currentThread().getName()); Thread t1 = new Thread(task, "Worker-1"); Thread t2 = new Thread(task, "Worker-2"); t1.start(); t2.start(); } }- AWorker-1 Worker-2 (in that order always)
- BCompilation error
- CWorker-1 and Worker-2 in non-deterministic order
- Dmain main
15.What is thread interruption?
- AForcefully killing a thread using thread.stop()
- BA cooperative mechanism where one thread sets an interrupt flag on another; the interrupted thread checks the flag and decides to stop
- CAn OS-level signal that terminates the thread immediately
- DAn exception automatically thrown when a thread is no longer needed
16.What does this code print?
public class Main { public static void main(String[] args) throws InterruptedException { Thread t = new Thread(() -> { try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("Interrupted!"); return; } System.out.println("Done sleeping"); }); t.start(); Thread.sleep(100); t.interrupt(); t.join(); } }- ANothing — the thread is killed silently
- BDone sleeping
- CInterrupted!
- DInterruptedException propagates to main
17.What is a virtual thread (introduced in Java 21)?
- AA thread pool with dynamic sizing
- BA thread that runs in a virtual machine only
- CA lightweight JVM-managed thread that enables millions of concurrent threads without OS thread overhead
- DA simulated thread for testing without real concurrency
18.What is the purpose of ThreadLocal?
- AA local variable inside a thread's run() method
- BA lock that is local to one thread
- CA variable that provides a separate copy for each thread — each thread sees only its own value
- DA thread-safe collection shared between threads
19.What does this code risk?
public class Main { static ThreadLocal<Integer> tl = new ThreadLocal<>(); public static void main(String[] args) { // Used in a thread pool where threads are reused // tl.set(42); // ... task runs ... // Missing: tl.remove(); } }- ANullPointerException when get() is called
- BRace condition between threads
- CMemory leak — the ThreadLocal value from a previous task persists when the thread is reused for a new task
- DClassCastException on get()
20.What is the difference between concurrency and parallelism?
- AThey are the same concept with different names
- BParallelism is software-based; concurrency is hardware-based
- CConcurrency is about managing multiple tasks at once (may interleave on one CPU); parallelism is executing multiple tasks simultaneously on multiple CPUs
- DConcurrency requires multiple threads; parallelism requires multiple processes