Viva Voce on Java Threads
Q1. What is the thread?
A thread is a lightweight sub process.
Q2. What is multithreading?
Multithreading is a process of executing multiple threads simultaneously. Multithreading is used to obtain the multitasking.
Q3. Differentiate between process and thread?
There are the following differences between the process and thread.
o A Program in the execution is called the process whereas; A thread is a subset of the process
o Process have different address space in memory, while threads contain a shared address space.
o Context switching can be faster between the threads as compared to context switching between the threads.
Q4. What do you understand by inter-thread communication?
o The process of communication between synchronized threads is termed as inter-thread communication.
Q5. What is the purpose of wait() method in Java?
This method is used for inter-thread communication in Java. The java.lang.Object.wait() is used to pause the current thread.
Q6. What does join() method?
The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.
Q7. Describe the purpose of sleep() method.
The sleep() method in java is used to block a thread for a particular time, which means it pause the execution of a thread for a specific time.
Q8. What is the synchronization?
Synchronization is the capability to control the access of multiple threads to any shared resource.
Q9. What is shutdown hook?
The shutdown hook is a thread that is invoked implicitly before JVM shuts down. So we can use it to perform clean up the resource or save the state when JVM shuts down normally or abruptly.
Q10. Can we make the user thread as daemon thread if the thread is started?
Q11. What about the daemon threads?
The daemon threads are the low priority threads that provide the background support and services to the user threads.
Q12. What is the difference between wait() and sleep() method?
The wait() method is defined in Object class.
The sleep() method is defined in Thread class.
The wait() method releases the lock.
The sleep() method doesn’t release the lock.
Q13. When should we interrupt a thread?
We should interrupt a thread when we want to break out the sleep or wait state of a thread. We can interrupt a thread by calling the interrupt() throwing the InterruptedException.
Q14. What is the difference between notify() and notifyAll()?
The notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.
Q15. What is Thread Scheduler in java?
In Java, when we create the threads, they are supervised with the help of a Thread Scheduler, which is the part of JVM. Thread scheduler is only responsible for deciding which thread should be executed.
Q16. What is the volatile keyword in java?
Volatile keyword is used in multithreaded programming to achieve the thread safety, as a change in one volatile variable is visible to all other threads so one variable can be used by one thread at a time.
Q17. What do you understand by thread pool?
Java Thread pool represents a group of worker threads, which are waiting for the task to be allocated.
Q18. Is it possible to start a thread twice?
MCQs on Java Threads
Q1. In java multi-threading, a thread can be created by
A. Extending Thread class
B. Implementing Runnable interface
C. Using both
Q2. Which method is called internally by Thread start() method?
Q3. What is maximum thread priority in Java
Q4. Which method must be implemented by a Java thread?
Q5. Execution of a java thread begins on which method call?
A. Start ()
B. Run ()
C. Execute ()
Q6. How many ways a thread can be created in Java multithreading?
Q7. Which statements is/are correct
A. On calling Thread start () method a new thread get created.
B. Thread start () method call run () method internally
C. Both A and B.
Q8.Which method is used to make main thread to wait for all child threads
A. Join ()
B. Sleep ()
C. Wait ()
Q9. Daemon thread runs in
Q10. Default value of a java thread is
- Sebesta,”Concept of programming Language”, Pearson Edu
- Louden, “Programming Languages: Principles & Practices” , Cengage Learning
- Tucker, “Programming Languages: Principles and paradigms “, Tata McGraw –Hill.
- E Horowitz, “Programming Languages”, 2nd Edition, Addison Wesley