Java Concurrency: Understanding Thread Pools and Executors
A beginner's quiz on Java concurrency focusing on thread pools and executors.
Questions in this quiz
15 questions on Java Concurrency: Understanding Thread Pools and Executors. Work through them here, then start the quiz to answer them and see your score.
1.What is the primary purpose of using a thread pool in Java?
- ATo ensure tasks are executed sequentially
- BTo decrease the CPU usage
- CTo increase the number of threads indefinitely
- DTo manage a pool of threads for executing tasks efficiently
2.Which Java class provides a simple way to create a thread pool?
- AThread
- BExecutorService
- CRunnable
- DCallable
3.What method is used to submit a task to an ExecutorService?
- Aexecute()
- Bsubmit()
- Cstart()
- Drun()
4.Which method is used to shut down an ExecutorService immediately?
- AshutdownNow()
- Bshutdown()
- Cstop()
- Dterminate()
5.What is the difference between execute() and submit() methods in ExecutorService?
- ANeither return a Future
- BBoth return a Future
- Csubmit() returns a Future, execute() does not
- Dexecute() returns a Future, submit() does not
6.Which of the following is NOT a type of thread pool provided by Executors?
- AScheduledThreadPool
- BFixedThreadPool
- CCachedThreadPool
- DDynamicThreadPool
7.What does the newFixedThreadPool(int nThreads) method do?
- ACreates a single-threaded executor
- BCreates a thread pool that executes tasks sequentially
- CCreates a thread pool with a fixed number of threads
- DCreates a thread pool that can grow and shrink dynamically
8.Which method in ExecutorService allows periodic task execution?
- Asubmit()
- BscheduleAtFixedRate()
- Crun()
- Dexecute()
9.What is the role of the Callable interface in Java concurrency?
- AIt allows tasks to be executed without returning a result
- BIt is used to create threads directly
- CIt is used to stop threads
- DIt allows tasks to return a result and throw exceptions
10.What does the Future interface represent in Java concurrency?
- AA result of an asynchronous computation
- BA task that has been cancelled
- CA thread that has been terminated
- DA completed task
11.Which method in the Future interface checks if the task is completed?
- AisCancelled()
- Bcancel()
- CisDone()
- Dget()
12.What happens if you submit a task to a shutdown ExecutorService?
- AThe task is executed immediately
- BThe task is queued for later execution
- CThe task is rejected
- DThe task is executed after a delay
13.What is the purpose of the ScheduledExecutorService interface?
- ATo execute tasks sequentially
- BTo manage a fixed number of threads
- CTo schedule commands to run after a delay or periodically
- DTo execute tasks without delay
14.Which method in ExecutorService is used to wait for the termination of all tasks?
- Aterminate()
- Bwait()
- Cshutdown()
- DawaitTermination()
15.What is a key advantage of using thread pools?
- AThey reduce the overhead of thread creation
- BThey allow tasks to run without any delay
- CThey increase the number of threads indefinitely
- DThey ensure tasks are executed in order