Java Multithreading MCQ

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. As beginners embark on their Java journey, understanding multithreading becomes pivotal.

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?

a) java.util
b) java.lang
c) java.multi
d) java.thread

Answer:

b) java.lang

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?

a) run()
b) execute()
c) begin()
d) start()

Answer:

d) start()

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?

a) Only one
b) At least one
c) At least two
d) Multiple

Answer:

d) Multiple

Explanation:

A Java program can execute multiple threads concurrently.

4. Which of the following is not a thread state in Java?

a) Running
b) Ready
c) Sleeping
d) Deleted

Answer:

d) Deleted

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?

a) wait()
b) notify()
c) sleep()
d) yield()

Answer:

d) yield()

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?

a) Thread
b) Runnable
c) Object
d) Executor

Answer:

c) Object

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?

a) The current thread gets terminated.
b) The current thread waits for the other thread to finish.
c) The other thread waits for the current thread to finish.
d) The current thread immediately starts the other thread.

Answer:

b) The current thread waits for the other thread to finish.

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?

a) To speed up thread execution
b) To ensure methods run in parallel
c) To prevent method interruption
d) To prevent concurrent access to critical sections of code

Answer:

d) To prevent concurrent access to critical sections of code

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?

a) run()
b) start()
c) execute()
d) go()

Answer:

a) run()

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?

a) New
b) Runnable
c) Waiting
d) Terminated

Answer:

d) Terminated

Explanation:

The "Terminated" state indicates that a thread has completed its execution and has terminated its lifecycle.



Comments