MCQs on Threads and Concurrency | Java Multithreading

Mastering Java Threads and Concurrency is essential for developing efficient, responsive, and scalable applications. These MCQs cover topics such as thread creation, lifecycle, synchronization, and thread pool management.


Creating Threads in Java

  1. Which of the following methods is used to start a thread in Java?
    • a) run()
    • b) start()
    • c) execute()
    • d) begin()
  2. How can you create a thread in Java?
    • a) By implementing the Runnable interface
    • b) By extending the Thread class
    • c) By creating an instance of the Callable interface
    • d) Both a and b
  3. What is the primary purpose of the Runnable interface in Java?
    • a) To start threads
    • b) To define a method for asynchronous execution
    • c) To provide a single method, run(), for thread execution
    • d) To create multithreaded applications
  4. Which of these is NOT a correct way to create a thread?
    • a) Extending the Thread class
    • b) Implementing the Runnable interface
    • c) Implementing the ExecutorService interface
    • d) Using lambda expressions
  5. How do you make a thread sleep in Java?
    • a) thread.wait()
    • b) Thread.sleep()
    • c) thread.pause()
    • d) Thread.yield()

Thread Lifecycle and States

  1. Which method can cause a thread to go to the “waiting” state?
    • a) sleep()
    • b) wait()
    • c) notify()
    • d) yield()
  2. Which state does a thread enter after it has been created but before it starts running?
    • a) Blocked
    • b) Runnable
    • c) New
    • d) Terminated
  3. Which of the following states can a thread be in after calling the start() method?
    • a) New
    • b) Runnable
    • c) Blocked
    • d) Waiting
  4. What does the join() method do in Java?
    • a) It pauses the current thread for a specified time
    • b) It allows one thread to wait until another thread completes
    • c) It terminates the specified thread
    • d) It combines two threads into one
  5. In which thread state is a thread unable to progress because it’s waiting to acquire a lock?
    • a) Waiting
    • b) Blocked
    • c) Timed waiting
    • d) Terminated

Synchronization in Java

  1. What is the purpose of synchronization in Java?
    • a) To allow multiple threads to access resources simultaneously
    • b) To make threads execute sequentially
    • c) To prevent thread interference and data inconsistency
    • d) To increase thread execution speed
  2. Which keyword is used to make a method synchronized?
    • a) volatile
    • b) static
    • c) synchronized
    • d) final
  3. What is a synchronized block in Java?
    • a) A block that only one thread can execute at a time
    • b) A block that prevents thread creation
    • c) A block that synchronizes multiple classes
    • d) A block that ensures all methods execute concurrently
  4. What can lead to deadlock in Java?
    • a) Two threads competing for the same resources without release
    • b) A single thread executing too quickly
    • c) Two synchronized methods running simultaneously
    • d) Both threads having different priorities
  5. Which of the following can help avoid deadlocks?
    • a) Using fewer synchronized blocks
    • b) Using notify() and notifyAll() methods
    • c) Ensuring threads acquire locks in the same order
    • d) Increasing thread priority
  6. Which method can release a lock on an object and let other threads acquire it?
    • a) wait()
    • b) notify()
    • c) yield()
    • d) start()
  7. What is the purpose of the volatile keyword in Java?
    • a) To allow only one thread to access a variable
    • b) To make variable updates visible to all threads
    • c) To stop thread execution
    • d) To pause the current thread
  8. Which of the following statements about synchronized is true?
    • a) It prevents all threads from accessing an object
    • b) It ensures that only one thread can access a resource at a time
    • c) It allows multiple threads to execute a method simultaneously
    • d) It only works for Runnable objects

Executors and Thread Pools

  1. What is the purpose of the Executor framework in Java?
    • a) To manage thread lifecycle
    • b) To stop and start threads automatically
    • c) To manage a pool of worker threads
    • d) To synchronize thread execution
  2. How do you create a fixed-size thread pool in Java?
    • a) new ThreadPool(5)
    • b) Executors.newFixedThreadPool(5)
    • c) ThreadPool.create(5)
    • d) ExecutorService.createFixedPool(5)
  3. What does shutdown() do in an ExecutorService?
    • a) Immediately stops all running tasks
    • b) Waits for tasks to complete before stopping
    • c) Restarts all tasks in the pool
    • d) Blocks new tasks but allows existing tasks to finish
  4. Which method in the Executor framework submits a Callable task?
    • a) execute()
    • b) submit()
    • c) run()
    • d) invoke()
  5. How can you retrieve a result from a Callable task?
    • a) By calling task.getResult()
    • b) Using the Future.get() method
    • c) With Thread.get()
    • d) Using ThreadPool.getResult()
  6. Which of the following is an advantage of using thread pools?
    • a) Reduces memory usage
    • b) Minimizes thread creation overhead
    • c) Increases CPU speed
    • d) Improves memory cleanup
  7. Which class provides a cached thread pool in Java?
    • a) ThreadPool
    • b) Executors
    • c) ThreadManager
    • d) RunnablePool
  8. How does a cached thread pool work?
    • a) It reuses threads and creates new ones if necessary
    • b) It maintains a fixed number of threads
    • c) It blocks tasks when all threads are busy
    • d) It terminates all threads after each task
  9. Which method is used to wait for the completion of all tasks in an ExecutorService?
    • a) await()
    • b) waitAll()
    • c) awaitTermination()
    • d) finishAll()
  10. What is the purpose of invokeAll() in ExecutorService?
    • a) To submit multiple Callable tasks
    • b) To shutdown the service
    • c) To cancel all running tasks
    • d) To monitor the state of tasks
  11. Which method is used to execute a Runnable task with an Executor?
    • a) run()
    • b) execute()
    • c) submit()
    • d) call()
  12. How do you gracefully shutdown an ExecutorService?
    • a) By calling stop()
    • b) By using shutdown() or shutdownNow()
    • c) By using terminate()
    • d) By using close()

Answer Key

QnoAnswer
1b) start()
2d) Both a and b
3c) To provide a single method, run(), for thread execution
4c) Implementing the ExecutorService interface
5b) Thread.sleep()
6b) wait()
7c) New
8b) Runnable
9b) It allows one thread to wait until another thread completes
10b) Blocked
11c) To prevent thread interference and data inconsistency
12c) synchronized
13a) A block that only one thread can execute at a time
14a) Two threads competing for the same resources without release
15c) Ensuring threads acquire locks in the same order
16a) wait()
17b) To make variable updates visible to all threads
18b) It ensures that only one thread can access a resource at a time
19c) To manage a pool of worker threads
20b) Executors.newFixedThreadPool(5)
21d) Blocks new tasks but allows existing tasks to finish
22b) submit()
23b) Using the Future.get() method
24b) Minimizes thread creation overhead
25b) Executors
26a) It reuses threads and creates new ones if necessary
27c) awaitTermination()
28a) To submit multiple Callable tasks
29b) execute()
30b) By using shutdown() or shutdownNow()

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top