Any2Quiz
Quizzes / Computer Science / Languages / Java

Threads and Concurrency Quiz - Languages Java

Free practice quiz: Threads and Concurrency (Languages Java). 55 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

This Languages Java practice quiz has 55 multiple-choice questions on Threads and Concurrency, and 55 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.

Start quiz: 55 questions, free

Sample questions

  1. What is the primary mechanism by which Java achieves concurrency?

    Show answer

    Answer: B. Threads

    Java's concurrency model is built around the concept of threads, which are lightweight units of execution that can run concurrently within a single process.

  2. Which exception is thrown by `Thread.sleep()`?

    Show answer

    Answer: B. InterruptedException

    `Thread.sleep()` can be interrupted by another thread calling the `interrupt()` method on the sleeping thread. If this happens, `Thread.sleep()` throws an `InterruptedException`.

  3. What is `ReentrantLock` in Java?

    Show answer

    Answer: B. A more flexible and powerful locking mechanism than `synchronized` keywords, allowing reentrant locking, fairness, and timed waits.

    `ReentrantLock` implements the `Lock` interface and provides explicit locking and unlocking. It offers features like reentrancy (a thread can acquire the same lock multiple times), fairness policies (optional), and timed lock acquisition, which are not available with the basic `synchronized` keyword.

  4. What is the purpose of the `synchronized` block `synchronized(object)`?

    Show answer

    Answer: B. To synchronize on a specific instance of an object.

    When you use `synchronized(object)`, the thread acquires the intrinsic lock associated with the `object` instance. This allows for fine-grained synchronization, where different blocks of code might synchronize on different objects, enabling more concurrent access than synchronizing an entire method or static method.

  5. What is a `Phaser` in Java's concurrency utilities?

    Show answer

    Answer: A. A synchronization mechanism for a variable number of parties that may arrive and depart dynamically at barrier points.

    `Phaser` is a more flexible synchronization barrier than `CyclicBarrier` because the number of parties (threads) can change dynamically. Parties register with the `Phaser`, wait at a barrier point, and then can optionally deregister. It's useful for coordinating threads in complex, dynamic workflows.

Take the full quiz

More questions in this quiz

Answers and explanations are in the full quiz.

  1. Which method is used to start a thread in Java?
  2. What is the difference between `Thread.sleep(milliseconds)` and `Object.wait()`?
  3. What is the purpose of the `synchronized` keyword in Java?
  4. What is a deadlock in multithreaded programming?
  5. Which interface defines the contract for executing a command on some underlying thread?
  6. What is the difference between `wait()`, `notify()`, and `notifyAll()`?
  7. What is a race condition?
  8. Which of the following is NOT a state of a Java Thread?
  9. What is the role of the `volatile` keyword in Java?
  10. What is the `ExecutorService` interface primarily used for?
  11. What is the purpose of `Thread.yield()`?
  12. What is the difference between `start()` and `run()` methods of a `Thread` class?
  13. What is the `daemon` property of a thread?
  14. What is the purpose of the `join()` method in Java threads?
  15. What does the `interrupt()` method do to a thread?
  16. Which class provides a framework for managing threads and scheduling tasks in a more flexible way than direct thread manipulation?
  17. What is the purpose of a thread-safe class?
  18. What is a `Callable` interface in Java concurrency?
  19. What is the purpose of a `Future` object in Java concurrency?
  20. Which of the following is an example of a thread-safe collection in Java's `java.util.concurrent` package?
  21. What is the purpose of `Thread.currentThread().interrupt()`?
  22. What is the primary advantage of using an `ExecutorService` over manually creating `Thread` objects?
  23. What is the purpose of `synchronized(this)` in Java?
  24. What is a `CountDownLatch`?
  25. What is `synchronized` on a static method equivalent to?
  26. What is a `Semaphore` in Java's concurrency utilities?
  27. What is the purpose of the `CompletableFuture` class introduced in Java 8?
  28. What is the `yield()` method guaranteed to do?
  29. What is an `Atomic` operation in Java concurrency?
  30. Which of the following is NOT a way to create a thread in Java?
  31. What is the primary purpose of the `java.util.concurrent.locks` package?
  32. What is a `CyclicBarrier` used for?
  33. What happens if a thread calls `wait()` on an object, but no other thread calls `notify()` or `notifyAll()` on that same object?
  34. What is the purpose of the `Thread.yield()` method in the context of thread scheduling?
  35. Which method is used to check if a thread has been interrupted?
  36. What is the purpose of the `synchronized` keyword when applied to a method?
  37. What is a thread pool in the context of `ExecutorService`?
  38. What does `notifyAll()` do?
  39. What is the purpose of the `Thread.activeCount()` method?
  40. What is the key difference between `sleep()` and `wait()` regarding locks?
  41. What is the purpose of the `ReentrantReadWriteLock`?
  42. What is the `ThreadLocal` class primarily used for?
  43. What does `notify()` do?
  44. What is the main purpose of `Executors.newFixedThreadPool(int nThreads)`?
  45. What is the meaning of reentrancy in the context of `ReentrantLock` or `synchronized` methods?
  46. What is the default behavior of a `Thread` when it's created and started in Java?
  47. What is the purpose of `Thread.join(long millis)`?
  48. What is an `Executor` framework in Java?
  49. What is the role of a `ThreadFactory`?
  50. What is the significance of the `finally` block in relation to `synchronized` blocks?

More quizzes

Browse all Java quizzes

More to play

Daily ChallengeA new quiz every day. Keep your streak.HangmanGuess the word, letter by letter.Story QuestLearn through short interactive stories.All quizzesCBSE, ICSE, competitive exams and more.