Multithreading is one of the core concepts in Java that enables concurrent execution of two or more parts of a program, maximizing the CPU’s utilization.
This blog post provides a set of multiple-choice questions (MCQs) focused on Java multithreading to test and reinforce your knowledge.
1. Which Java package contains classes and interfaces for multithreading?
Answer:
Explanation:
The java.lang package provides classes and interfaces for multithreading, including Thread and Runnable.
2. Which method is used to start the execution of a thread?
Answer:
Explanation:
The start() method of the Thread class is used to start the execution of a thread.
3. How many threads can be executed at a time in a Java program?
Answer:
Explanation:
A Java program can execute multiple threads concurrently.
4. Which of the following is not a thread state in Java?
Answer:
Explanation:
“Deleted” is not a state of a thread in Java.
5. Which method moves a thread from the running state to the runnable state?
Answer:
Explanation:
The yield() method moves the currently running thread back to the runnable state, allowing other threads to execute.
6. Which class or interface defines the wait(), notify(), and notifyAll() methods in Java?
Answer:
Explanation:
The methods wait(), notify(), and notifyAll() are defined in the Object class, and every object in Java inherits them.
7. When a thread calls the join() method on another thread, what happens?
Answer:
Explanation:
The join() method lets one thread wait for the completion of another thread.
8. What is the primary purpose of the synchronized keyword in Java?
Answer:
Explanation:
The synchronized keyword ensures that only one thread can access the synchronized method or block at a given point in time, providing a mechanism to prevent race conditions.
9. Which method must be provided when a class implements the Runnable interface for threads?
Answer:
Explanation:
The Runnable interface mandates the definition of the run() method. This method contains the code that constitutes the new thread.
10. In the lifecycle of a thread, which state represents a thread that has terminated its lifecycle?
Answer:
Explanation:
The “Terminated” state indicates that a thread has completed its execution and has terminated its lifecycle.
11. What is multithreading in Java?
Answer:
Explanation:
Multithreading refers to the ability of a program to execute multiple threads concurrently within a single process.
12. What is a thread in Java?
Answer:
Explanation:
A thread in Java is a lightweight, independent unit of execution that runs a sequence of instructions.
13. Which class is used to create a thread in Java?
Answer:
Explanation:
The Thread class is used to create and control threads in Java.
14. How can synchronization be achieved in Java threads?
Answer:
Explanation:
Synchronization in Java threads can be achieved by using the synchronized keyword to protect critical sections of code from concurrent access.
15. What is the main advantage of multithreading in Java?
Answer:
Explanation:
One of the main advantages of Multithreading in Java is improved program performance by utilizing available CPU cores and concurrently executing tasks.