This set of Java Quiz focuses on “Multithreading”.
1. What requires less resources?
c) Thread and Process
d) Neither Thread nor Process
Explanation: Thread is a lightweight and requires less resources to create and exist in the process. Thread shares the process resources.
2. What does not prevent JVM from terminating?
b) Daemon Thread
c) User Thread
d) JVM Thread
Explanation: Daemon thread runs in the background and does not prevent JVM from terminating. Child of daemon thread is also daemon thread.
3. What decides thread priority?
b) Process scheduler
d) Thread scheduler
Explanation: Thread scheduler decides the priority of the thread execution. This cannot guarantee that higher priority thread will be executed first, it depends on thread scheduler implementation that is OS dependent.
4. What is true about time slicing?
a) Time slicing is OS service that allocates CPU time to available runnable thread
b) Time slicing is the process to divide the available CPU time to available runnable thread
c) Time slicing depends on its implementation in OS
d) Time slicing allocates more resources to thread
Explanation: Time slicing is the process to divide the available CPU time to available runnable thread.
5. Deadlock is a situation when thread is waiting for other thread to release acquired object.
Explanation: Deadlock is java programming situation where one thread waits for an object lock that is acquired by other thread and vice-versa.
6. What should not be done to avoid deadlock?
a) Avoid using multiple threads
b) Avoid hold several locks at once
c) Execute foreign code while holding a lock
d) Use interruptible locks
Explanation: To avoid deadlock situation in Java programming do not execute foreign code while holding a lock.
7. What is true about threading?
a) run() method calls start() method and runs the code
b) run() method creates new thread
c) run() method can be called directly without start() method being called
d) start() method creates new thread and calls code written in run() method
Explanation: start() eventually calls run() method. Start() method creates thread and calls the code written inside run method.
8. Which of the following is a correct constructor for thread?
a) Thread(Runnable a, String str)
b) Thread(int priority)
c) Thread(Runnable a, int priority)
d) Thread(Runnable a, ThreadGroup t)
Explanation: Thread(Runnable a, String str) is a valid constructor for thread. Thread() is also a valid constructor.
9. Which of the following stops execution of a thread?
a) Calling SetPriority() method on a Thread object
b) Calling notify() method on an object
c) Calling wait() method on an object
d) Calling read() method on an InputStream object
Explanation: notify() wakes up a single thread which is waiting for this object.
10. Which of the following will ensure the thread will be in running state?
Explanation: wait() always causes the current thread to go into the object’s wait pool. Hence, using this in a thread will keep it in running state.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java for Quizzes, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.