Skip to content
QuiznExam

Threads in Java Quiz

Test your knowledge of threads in Java, including thread creation, the Thread class, Runnable interface, thread lifecycle, synchronization, inter-thread communication, thread priorities, and concurrency.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is a thread in Java?

    • A. A type of database
    • B. A lightweight unit of execution within a process
    • C. A Java package
    • D. A method that always runs first
    Show answer

    B. A lightweight unit of execution within a process

    A thread is an independent path of execution within a process. Multiple threads can execute concurrently while sharing the process's resources.

  2. 2. Which class is commonly used to create and manage a thread directly in Java?

    • A. Thread
    • B. Scanner
    • C. System
    • D. String
    Show answer

    A. Thread

    The java.lang.Thread class provides methods for creating, starting, and managing threads.

  3. 3. Which interface can be implemented to define a task that a thread should execute?

    • A. Serializable
    • B. Comparable
    • C. Runnable
    • D. Cloneable
    Show answer

    C. Runnable

    The Runnable interface defines a run() method that contains the task to be executed by a thread.

  4. 4. Which method starts a new thread of execution in Java?

    • A. run()
    • B. executeNow()
    • C. init()
    • D. start()
    Show answer

    D. start()

    Calling start() schedules a new thread of execution, which subsequently invokes its run() method. Calling run() directly does not start a new thread.

  5. 5. What happens when the run() method of a Thread object is called directly?

    • A. A new thread always starts
    • B. The method executes in the current thread
    • C. The JVM immediately terminates
    • D. The thread automatically enters the BLOCKED state
    Show answer

    B. The method executes in the current thread

    Calling run() directly is an ordinary method call. A new thread is started by calling start().

  6. 6. Which method pauses the current thread for a specified period?

    • A. sleep()
    • B. stop()
    • C. yieldTime()
    • D. pauseThread()
    Show answer

    A. sleep()

    Thread.sleep() pauses the currently executing thread for a specified duration, subject to scheduling and possible interruption.

  7. 7. Which keyword is used to synchronize access to a critical section in Java?

    • A. volatile
    • B. transient
    • C. synchronized
    • D. native
    Show answer

    C. synchronized

    The synchronized keyword uses an object's monitor or a class monitor to coordinate access to protected code and establish memory visibility guarantees.

  8. 8. What is a race condition in multithreaded programming?

    • A. A thread that runs faster than every other thread
    • B. A thread that never calls run()
    • C. A situation in which all threads finish at exactly the same time
    • D. A situation in which the result depends on the timing or interleaving of concurrent operations
    Show answer

    D. A situation in which the result depends on the timing or interleaving of concurrent operations

    A race condition can occur when concurrent operations access shared state without sufficient coordination, causing results to depend on their execution order.

  9. 9. Which method can be used to wait for another thread to finish executing?

    • A. sleep()
    • B. join()
    • C. yield()
    • D. interrupt()
    Show answer

    B. join()

    Calling join() on a thread causes the current thread to wait until the target thread terminates, unless the wait is interrupted or a timed join expires.

  10. 10. Which method is used to request that a thread be interrupted?

    • A. interrupt()
    • B. terminate()
    • C. destroy()
    • D. kill()
    Show answer

    A. interrupt()

    The interrupt() method sets a thread's interrupt status and can cause certain blocking methods to throw InterruptedException. It does not forcibly terminate the thread.

  11. 11. Which statement about thread priorities in Java is correct?

    • A. Higher priority guarantees earlier execution
    • B. Thread priority determines the exact CPU time allocated
    • C. Thread priority is a scheduling hint and does not guarantee execution order
    • D. All Java threads must have the same priority
    Show answer

    C. Thread priority is a scheduling hint and does not guarantee execution order

    Java thread priorities can influence scheduling, but the JVM and operating system do not guarantee that a higher-priority thread will execute first.

  12. 12. What is the purpose of the wait() method in Java?

    • A. To permanently terminate a thread
    • B. To release every lock held by the current thread
    • C. To start a new thread automatically
    • D. To wait for notification while releasing the monitor associated with the object
    Show answer

    D. To wait for notification while releasing the monitor associated with the object

    Object.wait() requires the current thread to own the object's monitor. It releases that monitor while waiting and reacquires it before returning or throwing InterruptedException.

  13. 13. Which method is traditionally used to notify one thread waiting on an object's monitor?

    • A. notify()
    • B. notifyOneThreadNow()
    • C. signalAll()
    • D. resume()
    Show answer

    A. notify()

    Object.notify() wakes one thread waiting on that object's monitor, if any. The notifying thread must own the same monitor.

  14. 14. Which executor service method submits a Runnable task for execution without returning a Future?

    • A. start()
    • B. execute()
    • C. join()
    • D. wait()
    Show answer

    B. execute()

    Executor.execute(Runnable) submits a task for execution without returning a Future. ExecutorService.submit() can return a Future representing the task.

  15. 15. What does the BLOCKED state represent in Java's thread lifecycle?

    • A. The thread has completed execution
    • B. The thread has not yet been created
    • C. The thread is waiting to acquire a monitor lock
    • D. The thread is sleeping for a specified duration
    Show answer

    C. The thread is waiting to acquire a monitor lock

    A thread in the BLOCKED state is waiting to acquire an intrinsic monitor lock to enter a synchronized block or method.