learn2code

40 MCQ on Multithreading in Java: Interview Questions

Java for Each Loop. Call by Value in Java and MCQ on Multithreading in Java and Design Patterns in Java and Strong Numbers in Java

MCQ on multithreading in Java can help you cover a wide range of topics and concepts. Here’s a comprehensive set of MCQs, each followed by a detailed explanation:

40 MCQ on Multithreading in Java:

Q1. Which of the following is the correct way to create a new thread in Java?

A) By implementing the Thread class
B) By extending the Runnable interface
C) By extending the Thread class
D) By implementing the Threadable interface

A1: C) By extending the Thread class

Explanation: You can create a new thread in Java by either extending the Thread class or implementing the Runnable interface. Extending the Thread class is one of the direct ways to create a thread.


Q2. Which of the following methods is used to start a thread execution?

A) run()
B) execute()
C) begin()
D) start()

A2: D) start()

Explanation: The start() method is used to initiate the execution of a thread. This method calls the run() method internally to execute the thread.


Q3. What will be the output if the run() method is called directly instead of using the start() method?

A) A new thread will be created and started.
B) The run() method will be executed in the context of the current thread.
C) A compilation error will occur.
D) A runtime exception will be thrown.

A3: B) The run() method will be executed in the context of the current thread.

Explanation: If the run() method is called directly, it will execute in the same thread, not in a new one. The start() method is required to create a new thread.


Q4. Which of the following is NOT a state in the thread lifecycle?

A) New
B) Runnable
C) Terminated
D) Paused

A4: D) Paused

Explanation: There is no state called “Paused” in the Java thread lifecycle. The valid states are New, Runnable, Blocked, Waiting, Timed Waiting, and Terminated.


Q5. What does the sleep() method in Java do?

A) Pauses the thread execution indefinitely.
B) Pauses the thread execution for a specified time.
C) Terminates the thread.
D) Moves the thread to the Waiting state until notified.

A5: B) Pauses the thread execution for a specified time.

Explanation: The sleep() method pauses the execution of the current thread for a specified duration, allowing other threads to execute during this time.


Q6. Which of the following is true about thread priorities in Java?

A) Threads always execute in the order of their priorities.
B) Thread priorities range from 0 to 10.
C) The setPriority() method is used to set a thread’s priority.
D) Threads with the same priority are executed simultaneously.

A6: C) The setPriority() method is used to set a thread’s priority.

Explanation: Thread priorities in Java range from 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY), and the setPriority() method is used to assign a priority to a thread. However, thread execution order based on priority is not guaranteed by the JVM.


Q7. What happens when a thread with a lower priority calls the yield() method?

A) It immediately terminates.
B) It moves to the Terminated state.
C) It pauses and allows threads with the same or higher priority to execute.
D) It continues executing until manually stopped.

A7: C) It pauses and allows threads with the same or higher priority to execute.

Explanation: The yield() method pauses the currently executing thread, giving other threads of the same or higher priority a chance to execute. However, it does not guarantee that a specific thread will start executing.


Q8. Which method must be implemented when using the Runnable interface?

A) execute()
B) run()
C) start()
D) init()

A8: B) run()

Explanation: The Runnable interface requires the implementation of the run() method, which contains the code that will be executed by the thread.


Q9. How can thread synchronization be achieved in Java?

A) By using the join() method.
B) By using the yield() method.
C) By using the synchronized keyword.
D) By implementing the Runnable interface.

A9: C) By using the synchronized keyword.

Explanation: Thread synchronization in Java can be achieved using the synchronized keyword, which ensures that only one thread can execute a block of code or method at a time.


Q10. What is the purpose of the join() method in Java threads?

A) To terminate a thread.
B) To start a thread.
C) To make one thread wait until another thread completes its execution.
D) To set the priority of a thread.

A10: C) To make one thread wait until another thread completes its execution.

Explanation: The join() method is used to pause the execution of the current thread until the thread on which join() was called completes its execution.


Q11. Which of the following is a correct way to create a thread by implementing Runnable?

A) Thread t = new Runnable();
B) Thread t = new Thread(new Runnable());
C) Thread t = new Thread(new MyRunnable());
D) Thread t = new MyRunnable();

A11: C) Thread t = new Thread(new MyRunnable());

Explanation: When implementing Runnable, you create a thread by passing an instance of the class that implements Runnable to a Thread object, as shown in option C.


Q12. What does the wait() method do in Java threads?

A) It pauses the thread indefinitely until it is interrupted.
B) It pauses the thread until another thread invokes notify() or notifyAll().
C) It pauses the thread for a specified time.
D) It terminates the thread.

A12: B) It pauses the thread until another thread invokes notify() or notifyAll().

Explanation: The wait() method causes the current thread to wait until another thread calls notify() or notifyAll() on the same object.


Q13. Which keyword is used to prevent thread interference in Java?

A) volatile
B) transient
C) synchronized
D) static

A13: C) synchronized

Explanation: The synchronized keyword is used to prevent thread interference and ensure that only one thread at a time can access a synchronized block or method.


Q14. Which of the following is NOT true about the Thread class in Java?

A) It is part of the java.lang package.
B) It cannot be subclassed.
C) It provides methods to create, start, and manage threads.
D) It implements the Runnable interface.

A14: B) It cannot be subclassed.

Explanation: The Thread class can be subclassed to create new threads. It is part of the java.lang package and provides methods to manage threads.


Q15. What does the setDaemon(true) method do?

A) It sets a thread to be a user thread.
B) It sets a thread to be a daemon thread.
C) It pauses the thread indefinitely.
D) It stops the thread immediately.

A15: B) It sets a thread to be a daemon thread.

Explanation: The setDaemon(true) method sets a thread to be a daemon thread. Daemon threads are background threads that do not prevent the JVM from exiting when the program finishes.


Q16. What will happen if the synchronized keyword is applied to a static method?

A) The method will not be synchronized.
B) It will lock the entire class, preventing any thread from accessing any synchronized method of the class.
C) It will lock the object on which the method is invoked.
D) It will throw a runtime exception.

A16: B) It will lock the entire class, preventing any thread from accessing any synchronized method of the class.

Explanation: When the synchronized keyword is applied to a static method, it locks the entire class, preventing any thread from accessing any synchronized method of the class until the lock is released.


Q17. Which of the following will cause a thread to move from the Running state to the Blocked state?

A) The start() method is called.
B) The sleep() method is called.
C) The thread tries to enter a synchronized block that is already locked by another thread.
D) The thread completes its execution.

A17: C) The thread tries to enter a synchronized block that is already locked by another thread.

Explanation: When a thread tries to enter a synchronized block or method that is already

locked by another thread, it moves to the Blocked state until the lock is released.


Q18. Which of the following methods cannot be called on a thread object?

A) start()
B) run()
C) yield()
D) wait()

A18: D) wait()

Explanation: The wait() method is called on an object, not on a thread. The other methods (start(), run(), and yield()) are called on thread objects.


Q19. What is the effect of calling interrupt() on a thread that is sleeping?

A) The thread will continue sleeping until the sleep period is over.
B) The thread will immediately throw an InterruptedException.
C) The thread will terminate immediately.
D) The thread will ignore the interruption.

A19: B) The thread will immediately throw an InterruptedException.

Explanation: If a thread is sleeping and interrupt() is called on it, the thread will wake up and throw an InterruptedException.


Q20. What is a deadlock in Java threads?

A) When two or more threads are blocked forever, waiting for each other.
B) When a thread completes its execution.
C) When a thread cannot access a resource due to low priority.
D) When all threads finish execution.

A20: A) When two or more threads are blocked forever, waiting for each other.

Explanation: A deadlock occurs when two or more threads are blocked forever, each waiting for the other to release a resource.


Q21. Which of the following methods is used to check if a thread is alive?

A) isRunning()
B) isAlive()
C) isExecuting()
D) isFinished()

A21: B) isAlive()

Explanation: The isAlive() method is used to check whether a thread has been started and has not yet terminated.


Q22. What does the notify() method do?

A) It wakes up all threads that are waiting on the object’s monitor.
B) It wakes up a single thread that is waiting on the object’s monitor.
C) It wakes up the thread itself.
D) It puts the thread to sleep.

A22: B) It wakes up a single thread that is waiting on the object’s monitor.

Explanation: The notify() method wakes up one thread that is waiting on the object’s monitor. If multiple threads are waiting, only one is chosen, and the choice is arbitrary.


Q23. What is a thread pool in Java?

A) A group of threads that are created to execute a task at the same time.
B) A group of idle threads that are reused to execute tasks.
C) A collection of threads that are terminated after execution.
D) A set of threads that are synchronized together.

A23: B) A group of idle threads that are reused to execute tasks.

Explanation: A thread pool is a collection of pre-created threads that are reused to execute tasks. It helps in managing a large number of threads efficiently.


Q24. Which of the following can result in a race condition?

A) When multiple threads read a shared resource.
B) When multiple threads write to a shared resource simultaneously.
C) When a single thread writes to a shared resource.
D) When threads are synchronized.

A24: B) When multiple threads write to a shared resource simultaneously.

Explanation: A race condition occurs when multiple threads attempt to modify a shared resource simultaneously without proper synchronization, leading to unpredictable results.


Q25. How can deadlock be avoided in Java?

A) By using multiple locks.
B) By ensuring that all threads acquire and release locks in a consistent order.
C) By using the sleep() method.
D) By using the yield() method.

A25: B) By ensuring that all threads acquire and release locks in a consistent order.

Explanation: Deadlock can be avoided by ensuring that all threads acquire and release locks in a consistent order, preventing circular dependencies.


Q26. Which of the following is NOT a way to create a thread in Java?

A) By extending the Thread class.
B) By implementing the Runnable interface.
C) By implementing the Callable interface.
D) By using the Threadable class.

A26: D) By using the Threadable class.

Explanation: There is no Threadable class in Java. The other options are valid ways to create threads in Java.


Q27. What does the synchronized keyword apply to?

A) Variables
B) Methods and blocks of code
C) Classes
D) Interfaces

A27: B) Methods and blocks of code

Explanation: The synchronized keyword can be applied to methods and blocks of code to ensure that only one thread can execute them at a time.


Q28. What happens if a thread is interrupted while in a sleeping or waiting state?

A) It continues sleeping or waiting.
B) It immediately throws an InterruptedException.
C) It terminates without throwing any exceptions.
D) It moves to the Blocked state.

A28: B) It immediately throws an InterruptedException.

Explanation: If a thread is interrupted while it is in a sleeping or waiting state, it throws an InterruptedException.


Q29. Which interface does the Callable interface resemble?

A) Runnable
B) Thread
C) Future
D) CompletableFuture

A29: A) Runnable

Explanation: The Callable interface is similar to the Runnable interface, but unlike Runnable, Callable can return a result and throw a checked exception.


Q30. How can a thread in Java be terminated?

A) By calling the terminate() method.
B) By returning from the run() method.
C) By throwing an InterruptedException.
D) By calling the stop() method.

A30: B) By returning from the run() method.

Explanation: A thread in Java terminates when the run() method completes and returns.


Q31. What is the result of calling notifyAll() on an object?

A) It wakes up all threads waiting on the object’s monitor.
B) It terminates all threads waiting on the object’s monitor.
C) It makes the current thread wait until all threads finish execution.
D) It makes the object available to all threads.

A31: A) It wakes up all threads waiting on the object’s monitor.

Explanation: The notifyAll() method wakes up all threads that are waiting on the object’s monitor, allowing them to compete for the lock.


Q32. Which of the following states is a thread NOT in when it has called the wait() method?

A) Blocked
B) Waiting
C) Runnable
D) Timed Waiting

A32: C) Runnable

Explanation: When a thread calls the wait() method, it moves to the Waiting or Timed Waiting state, depending on whether it is waiting for a specific amount of time or indefinitely. It is not in the Runnable state.


Q33. Which method of the Thread class is used to temporarily suspend a thread’s execution?

A) sleep()
B) wait()
C) pause()
D) yield()

A33: D) yield()

Explanation: The yield() method temporarily suspends the currently executing thread to allow other threads of the same or higher priority to execute.


Q34. What is a thread-safe class in Java?

A) A class that can be used by only one thread at a time.
B) A class that can be safely used by multiple threads concurrently.
C) A class that cannot be extended by other classes.
D) A class that terminates threads safely.

A34: B) A class that can be safely used by multiple threads concurrently.

Explanation: A thread-safe class is one that ensures safe concurrent access by multiple threads without causing data inconsistency or corruption.


Q35. Which of the following methods does NOT belong to the Thread class?

A) start()
B) run()
C) interrupt()
D) await()

A35: D) await()

Explanation: The await() method does not belong to the Thread class; it is part of the CountDownLatch class in the java.util.concurrent package.


Q36. What does the ExecutorService interface provide?

A) Methods to execute Runnable tasks asynchronously.
B) Methods to create threads directly.
C) Methods to manage thread groups.
D) Methods to synchronize threads.

A36: A) Methods to execute Runnable tasks asynchronously.

Explanation: The ExecutorService interface provides methods to execute Runnable and Callable tasks asynchronously, manage thread pools, and handle task completion.


Q37. How can thread priority be changed in Java?

A) By calling the setPriority() method on a thread.

B) By using the priority keyword.
C) By calling the changePriority() method on a thread.
D) By implementing the Priority interface.

A37: A) By calling the setPriority() method on a thread.

Explanation: Thread priority can be changed by calling the setPriority() method on a thread object, with a value between Thread.MIN_PRIORITY (1) and Thread.MAX_PRIORITY (10).


Q38. What is the purpose of the Callable interface?

A) To create threads that can return results and throw exceptions.
B) To create threads without returning results.
C) To create synchronized methods.
D) To create threads that cannot be interrupted.

A38: A) To create threads that can return results and throw exceptions.

Explanation: The Callable interface is used to create tasks that can return results and throw checked exceptions. It is similar to the Runnable interface but more flexible.


Q39. Which method is called when a thread is in the waiting state and another thread calls notify() on the same object?

A) wait()
B) sleep()
C) run()
D) notify()

A39: C) run()

Explanation: When a thread is in the waiting state and another thread calls notify() on the same object, the waiting thread is moved back to the Runnable state, and when it gets CPU time, its run() method will continue execution.


Q40. Which of the following does NOT affect thread scheduling?

A) Thread priority
B) Thread state
C) Time slicing
D) Thread ID

A40: D) Thread ID

Explanation: The thread ID does not affect scheduling. Thread priority, state, and time slicing are factors that can influence thread scheduling.


Latest Blog Post

Leave a Reply

Harish

Typically replies within a hours