Multithreading and Concurrency MCQs

  1. Which of the following is true about threads in Java?
    a) Thread is a lightweight process
    b) Created using Thread class or Runnable interface
    c) Managed by JVM scheduler
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  2. What will be printed?
    1 class Eduinq extends Thread {
    2 public void run() {
    3 System.out.println("Eduinq Thread Running");
    4 }
    5 }
    6 public class Test {
    7 public static void main(String[] args) {
    8 new Eduinq().start();
    9 }
    10 }
    a) Eduinq Thread Running
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Thread Running

  3. Which of the following is true about Runnable?
    a) Defines run() method
    b) Used to create threads without extending Thread
    c) Allows multiple inheritance flexibility
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  4. What will be printed?
    1 class Eduinq implements Runnable {
    2 public void run() {
    3 System.out.println("Eduinq Runnable Running");
    4 }
    5 }
    6 public class Test {
    7 public static void main(String[] args) {
    8 Thread t = new Thread(new Eduinq());
    9 t.start();
    10 }
    11 }
    a) Eduinq Runnable Running
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Runnable Running

  5. Which of the following is true about thread states?
    a) NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED
    b) Managed by JVM
    c) Can be checked using getState()
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  6. What will be printed?
    1 public class Test {
    2 public static void main(String[] args) throws InterruptedException {
    3 Thread t = new Thread(() -> System.out.println("Eduinq Thread"));
    4 t.start();
    5 t.join();
    6 System.out.println("Main Done");
    7 }
    8 }
    a) Eduinq Thread followed by Main Done
    b) Main Done followed by Eduinq Thread
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Thread followed by Main Done

  7. Which of the following is true about synchronization?
    a) Ensures thread safety
    b) Prevents race conditions
    c) Achieved using synchronized keyword
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  8. What will be printed?
    1 class Eduinq {
    2 synchronized void printEduinq() {
    3 System.out.println("Eduinq Synchronized");
    4 }
    5 }
    6 public class Test {
    7 public static void main(String[] args) {
    8 Eduinq e = new Eduinq();
    9 new Thread(() -> e.printEduinq()).start();
    10 }
    11 }
    a) Eduinq Synchronized
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Synchronized

  9. Which of the following is true about ReentrantLock?
    a) Provides explicit lock mechanism
    b) Supports fairness policy
    c) Must be unlocked explicitly
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  10. What will be printed?
    1 import java.util.concurrent.locks.*;
    2 class Eduinq {
    3 private final Lock lock = new ReentrantLock();
    4 void show() {
    5 lock.lock();
    6 try {
    7 System.out.println("Eduinq Lock");
    8 } finally {
    9 lock.unlock();
    10 }
    11 }
    12 }
    13 public class Test {
    14 public static void main(String[] args) {
    15 new Eduinq().show();
    16 }
    17 }
    a) Eduinq Lock
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Lock

  11. Which of the following is true about ExecutorService?
    a) Manages thread pools
    b) Provides submit() and execute() methods
    c) Must be shut down explicitly
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  12. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 ExecutorService service = Executors.newFixedThreadPool(1);
    5 service.execute(() -> System.out.println("Eduinq Executor"));
    6 service.shutdown();
    7 }
    8 }
    a) Eduinq Executor
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Executor

  13. Which of the following is true about Future?
    a) Represents result of async computation
    b) Provides get() to retrieve result
    c) Can be cancelled
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  14. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 ExecutorService service = Executors.newSingleThreadExecutor();
    5 Future<String> f = service.submit(() -> "Eduinq Future");
    6 System.out.println(f.get());
    7 service.shutdown();
    8 }
    9 }
    a) Eduinq Future
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Future

  15. Which of the following is true about AtomicInteger?
    a) Provides atomic operations on int
    b) Uses CAS internally
    c) Thread safe
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  16. What will be printed?
    1 import java.util.concurrent.atomic.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 AtomicInteger ai = new AtomicInteger(5);
    5 ai.incrementAndGet();
    6 System.out.println(ai.get());
    7 }
    8 }
    a) 5
    b) 6
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — 6

  17. Which of the following is true about CountDownLatch?
    a) Allows threads to wait until latch counts down to zero
    b) Initialized with count
    c) Provides await() and countDown()
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  18. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 CountDownLatch latch = new CountDownLatch(1);
    5 new Thread(() -> {
    6 System.out.println("Eduinq Worker");
    7 latch.countDown();
    8 }).start();
    9 latch.await();
    10 System.out.println("Main Done");
    11 }
    12 }
    a) Eduinq Worker followed by Main Done
    b) Main Done followed by Eduinq Worker
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Worker followed by Main Done

  19. Which of the following is true about CyclicBarrier?
    a) Allows threads to wait until all reach barrier
    b) Resets automatically
    c) Useful for parallel tasks
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  20. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 CyclicBarrier barrier = new CyclicBarrier(2, () -> System.out.println("Eduinq Barrier Action"));
    5 new Thread(() -> {
    6 try { barrier.await(); } catch(Exception e) {}
    7 }).start();
    8 barrier.await();
    9 }
    10 }
    a) Eduinq Barrier Action
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Barrier Action

  21. Which of the following is true about Semaphore?
    a) Controls access to resources
    b) Initialized with permits
    c) Provides acquire() and release()
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  22. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 Semaphore sem = new Semaphore(1);
    5 sem.acquire();
    6 System.out.println("Eduinq Semaphore");
    7 sem.release();
    8 }
    9 }
    a) Eduinq Semaphore
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Semaphore

  23. Which of the following is true about ThreadLocal?
    a) Provides thread specific variables
    b) Each thread has its own copy
    c) Useful for user sessions
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  24. What will be printed?
    1 public class Test {
    2 public static void main(String[] args) {
    3 ThreadLocal<String> tl = new ThreadLocal<>();
    4 tl.set("Eduinq Local");
    5 System.out.println(tl.get());
    6 }
    7 }
    a) Eduinq Local
    b) null
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Local

  25. Which of the following is true about volatile keyword?
    a) Ensures visibility of changes across threads
    b) Does not guarantee atomicity
    c) Used for variables shared between threads
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  26. What will be printed?
    1 class Eduinq {
    2 volatile int count = 0;
    3 }
    4 public class Test {
    5 public static void main(String[] args) {
    6 Eduinq e = new Eduinq();
    7 e.count++;
    8 System.out.println(e.count);
    9 }
    10 }
    a) 1
    b) 0
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — 1

  27. Which of the following is true about synchronized blocks?
    a) Can synchronize specific code blocks
    b) Require object reference for lock
    c) Improve performance compared to synchronizing entire method
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  28. What will be printed?
    1 class Eduinq {
    2 void show() {
    3 synchronized(this) {
    4 System.out.println("Eduinq Block Sync");
    5 }
    6 }
    7 }
    8 public class Test {
    9 public static void main(String[] args) {
    10 new Eduinq().show();
    11 }
    12 }
    a) Eduinq Block Sync
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Block Sync

  29. Which of the following is true about ThreadPoolExecutor?
    a) Provides flexible thread pool management
    b) Allows core and max pool size
    c) Supports queueing tasks
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  30. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
    5 executor.execute(() -> System.out.println("Eduinq Task"));
    6 executor.shutdown();
    7 }
    8 }
    a) Eduinq Task
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Task

  31. Which of the following is true about ScheduledExecutorService?
    a) Executes tasks after delay
    b) Executes tasks periodically
    c) Provides schedule() and scheduleAtFixedRate()
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  32. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
    5 service.schedule(() -> System.out.println("Eduinq Scheduled"), 1, TimeUnit.SECONDS);
    6 service.shutdown();
    7 }
    8 }
    a) Eduinq Scheduled (after 1 second)
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Scheduled (after 1 second)

  33. Which of the following is true about Phaser?
    a) Used for synchronizing threads in phases
    b) More flexible than CyclicBarrier
    c) Supports dynamic registration
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  34. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 Phaser phaser = new Phaser(1);
    5 phaser.register();
    6 new Thread(() -> {
    7 System.out.println("Eduinq Phase");
    8 phaser.arriveAndDeregister();
    9 }).start();
    10 phaser.arriveAndAwaitAdvance();
    11 System.out.println("Main Done");
    12 }
    13 }
    a) Eduinq Phase followed by Main Done
    b) Main Done followed by Eduinq Phase
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Phase followed by Main Done

  35. Which of the following is true about ReadWriteLock?
    a) Provides separate read and write locks
    b) Multiple readers allowed simultaneously
    c) Only one writer allowed
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  36. What will be printed?
    1 import java.util.concurrent.locks.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 ReadWriteLock rw = new ReentrantReadWriteLock();
    5 rw.readLock().lock();
    6 System.out.println("Eduinq Read Lock");
    7 rw.readLock().unlock();
    8 }
    9 }
    a) Eduinq Read Lock
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Read Lock

  37. Which of the following is true about ConcurrentHashMap concurrency level?
    a) Defines number of segments (pre Java 8)
    b) Controls parallelism
    c) Default is 16
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  38. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 ConcurrentHashMap<Integer,String> map = new ConcurrentHashMap<>();
    5 map.put(1,"Eduinq");
    6 System.out.println(map.get(1));
    7 }
    8 }
    a) Eduinq
    b) null
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq

  39. Which of the following is true about BlockingQueue?
    a) Blocks when queue is full or empty
    b) Implemented by ArrayBlockingQueue, LinkedBlockingQueue
    c) Provides put() and take() methods
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  40. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 BlockingQueue<String> q = new ArrayBlockingQueue<>(2);
    5 q.put("Eduinq");
    6 System.out.println(q.take());
    7 }
    8 }
    a) Eduinq
    b) Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq

  41. Which of the following is true about Exchanger?
    a) Allows two threads to exchange objects
    b) Provides exchange() method
    c) Useful for producer consumer handoff
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  42. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 Exchanger<String> ex = new Exchanger<>();
    5 new Thread(() -> {
    6 try {
    7 System.out.println("Thread got: " + ex.exchange("Eduinq"));
    8 } catch(Exception e) {}
    9 }).start();
    10 System.out.println("Main got: " + ex.exchange("Portal"));
    11 }
    12 }
    a) Thread got: Portal, Main got: Eduinq
    b) Thread got: Eduinq, Main got: Portal
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Thread got: Portal, Main got: Eduinq

  43. Which of the following is true about ForkJoinPool?
    a) Implements work stealing algorithm
    b) Used for parallel tasks
    c) Supports RecursiveTask and RecursiveAction
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  44. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) {
    4 ForkJoinPool pool = new ForkJoinPool();
    5 pool.submit(() -> System.out.println("Eduinq ForkJoin"));
    6 pool.shutdown();
    7 }
    8 }
    a) Eduinq ForkJoin
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq ForkJoin

  45. Which of the following is true about CompletableFuture?
    a) Provides async computation handling
    b) Supports chaining with thenApply, thenAccept
    c) Can combine multiple futures
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  46. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> "Eduinq Async");
    5 System.out.println(cf.get());
    6 }
    7 }
    a) Eduinq Async
    b) Compilation error
    c) Runtime exception
    d) None
    View Answer

    Correct answer: A — Eduinq Async

  47. Which of the following is true about CompletableFuture.thenApply()?
    a) Transforms result of future
    b) Returns new CompletableFuture
    c) Executes asynchronously if chained
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  48. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> "Eduinq")
    5 .thenApply(s -> s + " Portal");
    6 System.out.println(cf.get());
    7 }
    8 }
    a) Eduinq Portal
    b) Eduinq
    c) Portal
    d) Compilation error
    View Answer

    Correct answer: A — Eduinq Portal

  49. Which of the following is true about CompletableFuture.thenCombine()?
    a) Combines results of two futures
    b) Executes when both complete
    c) Returns new CompletableFuture
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  50. What will be printed?
    1 import java.util.concurrent.*;
    2 public class Test {
    3 public static void main(String[] args) throws Exception {
    4 CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> "Eduinq");
    5 CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> "Portal");
    6 CompletableFuture<String> combined = cf1.thenCombine(cf2, (a,b) -> a + " " + b);
    7 System.out.println(combined.get());
    8 }
    9 }
    a) Eduinq Portal
    b) Portal Eduinq
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Eduinq Portal

Quick Links to Explore