InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What Are All The Methods Used For Inter Thread Communication And What Is The Class In Which These Methods Are Defined? |
|
Answer» a. wait(),NOTIFY() & NOTIFYALL() a. wait(),notify() & notifyall() |
|
| 2. |
Can You Declare A Static Method As Synchronized? |
|
Answer» Yes, we can DECLARE static METHOD as synchronized. But the CALLING thread should acquire lock on the CLASS that owns the method.
Yes, we can declare static method as synchronized. But the calling thread should acquire lock on the class that owns the method.
|
|
| 3. |
What Are The Two Ways That A Code Can Be Synchronised? |
Answer»
|
|
| 4. |
What Does The Start() Method Of Thread Do? |
|
Answer» The thread's start() method PUTS the thread in ready STATE and MAKES the thread eligible to RUN. start() method automatically calls the run () method. The thread's start() method puts the thread in ready state and makes the thread eligible to run. start() method automatically calls the run () method. |
|
| 5. |
What Is The Difference Between Normal Thread And Daemon Thread? |
|
Answer» Normal threads do mainstream activity, whereas DAEMON threads are USED LOW priority WORK. Hence daemon threads are also STOPPED when there are no normal threads.
Normal threads do mainstream activity, whereas daemon threads are used low priority work. Hence daemon threads are also stopped when there are no normal threads.
|
|
| 6. |
How To Make A Normal Thread As Daemon Thread? |
|
Answer» We should CALL setDaemon(true) METHOD on the thread object to make a thread as DAEMON thread. We should call setDaemon(true) method on the thread object to make a thread as daemon thread. |
|
| 7. |
What Is The Difference Between Sleep() And Yield()? |
|
Answer» When a THREAD calls the sleep() METHOD, it will RETURN to its waiting state. When a Thread calls the yield() method, it returns to the ready state. When a Thread calls the sleep() method, it will return to its waiting state. When a Thread calls the yield() method, it returns to the ready state. |
|
| 8. |
How Do You Ensure That N Threads Can Access N Resources Without Deadlock? |
|
Answer» A very SIMPLE way to avoid deadlock while using N threads is to impose an ORDERING on the locks and force each THREAD to FOLLOW that ordering. Thus, if all threads lock and unlock the mutexes in the same order, no deadlocks can arise. A very simple way to avoid deadlock while using N threads is to impose an ordering on the locks and force each thread to follow that ordering. Thus, if all threads lock and unlock the mutexes in the same order, no deadlocks can arise. |
|
| 9. |
How Does Thread Synchronization Occurs Inside A Monitor ? What Levels Of Synchronization Can You Apply? |
|
Answer» The JVM uses locks in CONJUNCTION with monitors. A monitor is basically a guardian that watches over a SEQUENCE of SYNCHRONIZED code and ensuring that only one thread at a time executes a synchronized PIECE of code. Each monitor is associated with an object reference. The thread is not allowed to execute the code until it obtains the lock.
The JVM uses locks in conjunction with monitors. A monitor is basically a guardian that watches over a sequence of synchronized code and ensuring that only one thread at a time executes a synchronized piece of code. Each monitor is associated with an object reference. The thread is not allowed to execute the code until it obtains the lock.
|
|
| 10. |
What Is The Difference Between A Synchronized Method And A Synchronized Block? |
|
Answer» In JAVA programming, each object has a lock. A thread can acquire the lock for an object by using the synchronized keyword. In Java programming, each object has a lock. A thread can acquire the lock for an object by using the synchronized keyword. |
|
| 11. |
Explain The Available Thread States In A High-level? |
|
Answer» During its execution, a THREAD can reside in one of the following states: During its execution, a thread can reside in one of the following states: |
|
| 12. |
Explain Different Ways Of Creating A Thread. Which One Would You Prefer And Why? |
|
Answer» There are three ways that can be used in order for a Thread to be created: There are three ways that can be used in order for a Thread to be created: |
|
| 13. |
What Does Wait() Method Do? |
|
Answer» WAIT() method releases CPU, releases objects LOCK, the thread enters into pool of WAITING threads. wait() method releases CPU, releases objects lock, the thread enters into pool of waiting threads. |
|
| 14. |
What Does Notifyall() Method Do? |
|
Answer» notifyAll() METHOD MOVES all waiting threads from the waiting pool to ready STATE. notifyAll() method moves all waiting threads from the waiting pool to ready state. |
|
| 15. |
What Exception Does The Wait() Method Throw? |
|
Answer» The java.lang.Object CLASS WAIT() METHOD THROWS "INTERRUPTEDEXCEPTION".
The java.lang.Object class wait() method throws "InterruptedException".
|
|
| 16. |
What Does The Yield() Method Do? |
|
Answer» The yield() method puts CURRENTLY RUNNING thread in to READY STATE. The yield() method puts currently running thread in to ready state. |
|
| 17. |
Is It Important To Acquire Object Lock Before Calling Wait(), Notify() And Notifyall()? |
|
Answer» Yes, it’s mandatory to acquire object lock before calling these methods on object. wait(), notify() and notifyAll() methods are ALWAYS called from SYNCHRONIZED block only, and as soon as thread enters synchronized block it ACQUIRES object lock (by holding object monitor). If we call these methods without ACQUIRING object lock i.e. from outside synchronize block then java.lang. IllegalMonitorStateException is thrown at runtime. Yes, it’s mandatory to acquire object lock before calling these methods on object. wait(), notify() and notifyAll() methods are always called from Synchronized block only, and as soon as thread enters synchronized block it acquires object lock (by holding object monitor). If we call these methods without acquiring object lock i.e. from outside synchronize block then java.lang. IllegalMonitorStateException is thrown at runtime. |
|
| 18. |
Why Wait(), Notify() And Notifyall() Are In Object Class And Not In Thread Class? |
|
Answer» 1.Every Object has a monitor, acquiring that monitors allow thread to hold lock on object. But Thread class does not have any monitors.
1.Every Object has a monitor, acquiring that monitors allow thread to hold lock on object. But Thread class does not have any monitors.
|
|
| 19. |
How Threads Communicate Between Each Other? |
|
Answer» THREADS can COMMUNICATE with each other by using wait(), notify() and NOTIFYALL() methods. Threads can communicate with each other by using wait(), notify() and notifyAll() methods. |
|
| 20. |
Can You Again Start Thread? |
|
Answer» No, we cannot start THREAD again, doing so will throw runtimeException java.lang.IllegalThreadStateException. The reason is once run() method is executed by Thread, it GOES into dead state. No, we cannot start Thread again, doing so will throw runtimeException java.lang.IllegalThreadStateException. The reason is once run() method is executed by Thread, it goes into dead state. |
|
| 21. |
Differences And Similarities Between Yield() And Sleep()? |
|
Answer» Differences yield() and sleep() : Differences yield() and sleep() : |
|
| 22. |
Difference Between Wait() And Sleep()? |
|
Answer» •Should be called from synchronized block :wait() method is always called from synchronized block i.e. wait() method needs to lock object MONITOR before object on which it is called. But SLEEP() method can be called from outside synchronized block i.e. sleep() method doesn’t need any object monitor. •Should be called from synchronized block :wait() method is always called from synchronized block i.e. wait() method needs to lock object monitor before object on which it is called. But sleep() method can be called from outside synchronized block i.e. sleep() method doesn’t need any object monitor. |
|
| 23. |
What Is Significance Of Sleep() Method In Detail, What State Does It Put Thread In? |
|
Answer» sleep() is a native method, it’s IMPLEMENTATION is provided by JVM. sleep() is a native method, it’s implementation is provided by JVM. |
|
| 24. |
What Is Significance Of Yield() Method, What State Does It Put Thread In? |
|
Answer» yield() is a native method it’s implementation in java 6 has been changed as compared to its implementation java 5. As method is native it’s implementation is provided by JVM.
yield() is a native method it’s implementation in java 6 has been changed as compared to its implementation java 5. As method is native it’s implementation is provided by JVM.
|
|
| 25. |
Can A Constructor Be Synchronized? |
|
Answer» No, constructor cannot be SYNCHRONIZED. Because constructor is used for instantiating object, when we are in constructor object is under creation. So, until object is not INSTANTIATED it does not NEED any synchronization. No, constructor cannot be synchronized. Because constructor is used for instantiating object, when we are in constructor object is under creation. So, until object is not instantiated it does not need any synchronization. |
|
| 26. |
Can You Find Whether Thread Holds Lock On Object Or Not? |
|
Answer» holdsLock(OBJECT) method can be USED to find out whether current thread holds the LOCK on monitor of specified object.
holdsLock(object) method can be used to find out whether current thread holds the lock on monitor of specified object.
|
|
| 27. |
What Do You Mean By Thread Starvation? |
|
Answer»
When thread does not enough CPU for its execution Thread starvation happens.
When thread does not enough CPU for its execution Thread starvation happens.
|
|
| 28. |
What Is Addshutdownhook Method In Java? |
|
Answer» addShutdownHook method in java > addShutdownHook method in java > |
|
| 29. |
What Is Threadgroup In Java, What Is Default Priority Of Newly Created Threadgroup, Mention Some Important Threadgroup Methods? |
|
Answer» When program starts JVM creates a ThreadGroup named main. Unless specified, all newly created threads become MEMBERS of the main THREAD group. When program starts JVM creates a ThreadGroup named main. Unless specified, all newly created threads become members of the main thread group. |
|
| 30. |
What Are Thread Priorities? |
|
Answer» Thread Priority range is from 1 to 10.
Thread Priority range is from 1 to 10.
|
|
| 31. |
What Is Significance Of Using Volatile Keyword? |
|
Answer» Java allows threads to access shared variables. As a RULE, to ensure that shared variables are consistently UPDATED, a thread should ensure that it has exclusive use of such variables by obtaining a lock that enforces mutual EXCLUSION for those shared variables.
Java allows threads to access shared variables. As a rule, to ensure that shared variables are consistently updated, a thread should ensure that it has exclusive use of such variables by obtaining a lock that enforces mutual exclusion for those shared variables.
|
|
| 32. |
What Is Difference Between Starting Thread With Run() And Start() Method? |
|
Answer» When you call start() method, MAIN thread INTERNALLY CALLS run() method to start newly created Thread, so run() method is ultimately CALLED by newly created thread. When you call start() method, main thread internally calls run() method to start newly created Thread, so run() method is ultimately called by newly created thread. |
|
| 33. |
How Can You Ensure All Threads That Started From Main Must End In Order In Which They Started And Also Main Should End In Last? |
|
Answer» We can use join() methodto ensure all threads that started from main must end in order in which they started and also main should end in LAST.In other WORDS waits for this thread to die. Calling join() method internally CALLS join(0);
We can use join() methodto ensure all threads that started from main must end in order in which they started and also main should end in last.In other words waits for this thread to die. Calling join() method internally calls join(0);
|
|
| 34. |
When Threads Are Not Lightweight Process In Java? |
|
Answer» Threads are lightweight PROCESS only if threads of same process are executing CONCURRENTLY. But if threads of different PROCESSES are executing concurrently then threads are heavy WEIGHT process. Threads are lightweight process only if threads of same process are executing concurrently. But if threads of different processes are executing concurrently then threads are heavy weight process. |
|
| 35. |
We Should Implement Runnable Interface Or Extend Thread Class. What Are Differences Between . How Can You Say Thread Behaviour Is Unpredictable? |
|
Answer» Thread behaviour is unpredictable because execution of Threads DEPENDS on Thread scheduler, thread scheduler may have different implementation on different platforms like windows, unix etc. Same threading program may produce different OUTPUT in subsequent executions even on same platform.
Thread behaviour is unpredictable because execution of Threads depends on Thread scheduler, thread scheduler may have different implementation on different platforms like windows, unix etc. Same threading program may produce different output in subsequent executions even on same platform.
|
|
| 36. |
We Should Implement Runnable Interface Or Extend Thread Class. What Are Differences Between Implementing Runnable And Extending Thread? |
|
Answer» Thread only when you are looking to modify run() and other methods as well. If you are simply looking to modify only the run() method implementing Runnable is the BEST option (Runnable interface has only one abstract method i.e. run() ).
Thread only when you are looking to modify run() and other methods as well. If you are simply looking to modify only the run() method implementing Runnable is the best option (Runnable interface has only one abstract method i.e. run() ).
|
|
| 37. |
How To Implement Threads In Java? |
|
Answer» Threads can be created in two ways i.e. by IMPLEMENTING java.lang.RUNNABLE interface or extending java.lang.Thread class and then extending run method. Thread has its own variables and methods, it lives and dies on the heap. But a thread of execution is an INDIVIDUAL process that has its own call STACK. Thread are lightweight process in java. We will create object of class which implements Runnable interface : MyRunnable runnable=new MyRunnable(); Thread thread=new Thread(runnable); 2.And then create Thread object by calling constructor and passing REFERENCE of Runnable interface i.e. runnable object : Thread thread=new Thread(runnable); Threads can be created in two ways i.e. by implementing java.lang.Runnable interface or extending java.lang.Thread class and then extending run method. Thread has its own variables and methods, it lives and dies on the heap. But a thread of execution is an individual process that has its own call stack. Thread are lightweight process in java. We will create object of class which implements Runnable interface : MyRunnable runnable=new MyRunnable(); Thread thread=new Thread(runnable); 2.And then create Thread object by calling constructor and passing reference of Runnable interface i.e. runnable object : Thread thread=new Thread(runnable); |
|
| 38. |
What Is Difference Between Process And Thread In Java? |
|
Answer» One process can have multiple Threads,Thread are subdivision of Process. One or more Threads runs in the CONTEXT of process. Threads can execute any part of process. And same part of process can be executed by multiple Threads. One process can have multiple Threads,Thread are subdivision of Process. One or more Threads runs in the context of process. Threads can execute any part of process. And same part of process can be executed by multiple Threads. |
|
| 39. |
What Is Thread In Java? |
Answer»
|
|