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 is the purpose of the finalize() method? |
|
Answer» Finalize() method is BASICALLY a method of Object class specially used to perform cleanup OPERATIONS on unmanaged resources just before garbage collection. It is not at all INTENDED to be called a normal method. After the complete EXECUTION of finalize() method, the object gets DESTROYED automatically. |
|
| 2. |
Can two threads execute two methods (static and non-static concurrently)? |
|
Answer» YES, it is possible. If both the THREADS acquire LOCKS on different objects, then they can EXECUTE concurrently without any problem. |
|
| 3. |
How do threads communicate with each other? |
|
Answer» Threads can COMMUNICATE USING three METHODS i.e., wait(), notify(), and NOTIFYALL(). |
|
| 4. |
Explain volatile variables in Java? |
|
Answer» A volatile VARIABLE is basically a keyword that is used to ensure and ADDRESS the visibility of changes to VARIABLES in multithreaded programming. This keyword cannot be used with classes and methods, INSTEAD can be used with variables. It is simply used to achieve thread-safety. If you mark any variable as volatile, then all the threads can read its value directly from the main MEMORY rather than CPU cache, so that each thread can get an updated value of the variable. |
|
| 5. |
Explain the meaning of the deadlock and when it can occur? |
|
Answer» Deadlock, as the name SUGGESTS, is a situation where MULTIPLE threads are BLOCKED FOREVER. It generally occurs when multiple threads HOLD locks on different resources and are waiting for other resources to complete their task. The above diagram shows a deadlock situation where two threads are blocked forever. Thread 1 is holding Object 1 but needs object 2 to complete processing whereas Thread 2 is holding Object 2 but needs object 1 first. In such conditions, both of them will hold lock forever and will never complete tasks. |
|
| 6. |
What do you mean by garbage collection? |
|
Answer» Garbage collection is basically a process of MANAGING memory AUTOMATICALLY. It uses several GC algorithms among which the popular one includes Mark and Sweep. The process includes THREE phases i.e., marking, deletion, and compaction/copying. In simple words, a garbage COLLECTOR finds objects that are no longer required by the program and then DELETE or remove these unused objects to free up the memory space. |
|
| 7. |
What’s the purpose of the join() method? |
|
Answer» join() method is generally used to pause the EXECUTION of a current THREAD UNLESS and until the specified thread on which join is called is dead or COMPLETED. To stop a thread from running until another thread gets ended, this method can be used. It joins the start of a thread execution to the end of another thread’s execution. It is CONSIDERED the final method of a thread class. |
|
| 8. |
Explain thread pool? |
|
Answer» A Thread pool is simply a collection of pre-initialized or worker threads at the start-up that can be used to execute TASKS and put back in the pool when completed. It is referred to as pool threads in which a group of fixed-size threads is created. By REDUCING the number of application threads and managing their lifecycle, one can mitigate the issue of performance using a thread pool. Using threads, performance can be enhanced and better system stability can occur. To create the thread POOLS, java.util.concurrent.Executors class USUALLY provides FACTORY methods. |
|
| 9. |
What is the start() and run() method of Thread class? |
|
Answer» start(): In simple words, the start() method is used to start or begin the EXECUTION of a NEWLY created THREAD. When the start() method is called, a new thread is created and this newly created thread executes the task that is kept in the run() method. One can call the start() method only once. |
|
| 10. |
What is Runnable and Callable Interface? Write the difference between them. |
||||||||||||||
|
Answer» Both the interfaces are generally used to encapsulate tasks that are needed to be executed by another THREAD. But there are some DIFFERENCES between them as given below: Runnable Interface vs Callable Interface
|
|||||||||||||||
| 11. |
Why wait(), notify(), and notifyAll() methods are present in Object class? |
|
Answer» We know that every object has a monitor that allows the thread to HOLD a lock on the object. But the thread class doesn't contain any MONITORS. Thread usually waits for the object’s monitor (lock) by calling the wait() METHOD on an object, and notify other threads that are waiting for the same lock using notify() or notifyAll() method. Therefore, these three methods are CALLED on objects only and allow all threads to communicate with each that are CREATED on that object. |
|
| 12. |
What’s the difference between notify() and notifyAll()? |
|
Answer» notify(): It sends a NOTIFICATION and wakes up only a single thread instead of multiple threads that are waiting on the OBJECT’s MONITOR. notifyAll(): It sends notifications and wakes up all threads and ALLOWS them to compete for the object's monitor instead of a single thread. |
|
| 13. |
What are the wait() and sleep() methods? |
|
Answer» wait(): As the name suggests, it is a non-static METHOD that causes the current thread to wait and go to sleep until some other threads call the NOTIFY () or notifyAll() method for the object’s monitor (lock). It simply releases the lock and is mostly used for inter-thread communication. It is defined in the object class, and should only be called from a SYNCHRONIZED context. Example: synchronized(monitor) { monitor.wait(); Here Lock Is RELEASED by Current Thread }sleep(): As the name suggests, it is a static method that pauses or stops the execution of the current thread for some specified period. It doesn’t release the lock while waiting and is mostly used to introduce pause on execution. It is defined in thread class, and no need to call from a synchronized context. Example: synchronized(monitor) { Thread.sleep(1000); Here Lock Is Held by The Current Thread //after 1000 MILLISECONDS, the current thread will wake up, or after we call that is interrupt() method } |
|
| 14. |
How can we create daemon threads? |
|
Answer» We can create DAEMON threads in java using the thread class setDaemon(true). It is used to mark the current thread as daemon thread or USER thread. isDaemon() method is generally used to check whether the current thread is daemon or not. If the thread is a daemon, it will return true otherwise it returns false. Output: t1 is Daemon thread t3 is Daemon thread t2 is User threadBut one can only call the setDaemon() method before start() method otherwise it will definitely throw IllegalThreadStateException as shown below: public class DaemonThread extends Thread { public void run() { System.out.println("Thread name: " + Thread.currentThread().getName()); System.out.println("Check if its DaemonThread: " + Thread.currentThread().isDaemon()); } public static void main(String[] args) { DaemonThread t1 = new DaemonThread(); DaemonThread t2 = new DaemonThread(); t1.start(); // Exception as the thread is already STARTED t1.setDaemon(true); t2.start(); } }Output: Thread name: Thread-0 Check if its DaemonThread: false |
|
| 15. |
What's the difference between User thread and Daemon thread? |
||||||||||
|
Answer» User and Daemon are basically two types of thread used in JAVA by using a ‘Thread Class’.
|
|||||||||||
| 16. |
What’s the difference between class lock and object lock? |
|
Answer» CLASS Lock: In java, each and every class has a UNIQUE lock usually referred to as a class level lock. These locks are achieved using the keyword ‘static synchronized’ and can be used to make static data thread-safe. It is generally used when ONE wants to prevent multiple threads from ENTERING a synchronized block. Object Lock: In java, each and every object has a unique lock usually referred to as an object-level lock. These locks are achieved using the keyword ‘synchronized’ and can be used to protect non-static data. It is generally used when one wants to synchronize a non-static method or block so that only the thread will be able to execute the code block on a given instance of the class. |
|
| 17. |
What's the difference between thread and process? |
||||||||||||||||||
|
Answer» Thread: It simply refers to the smallest UNITS of the PARTICULAR process. It has the ability to execute different parts (referred to as thread) of the program at the same time.
|
|||||||||||||||||||
| 18. |
What are the two ways of implementing thread in Java? |
|
Answer» There are basically two ways of IMPLEMENTING thread in java as GIVEN below:
Example: class MultithreadingDemo extends Thread { public VOID RUN() { System.out.println("My thread is in running state."); } public static void main(String args[]) { MultithreadingDemoobj=new MultithreadingDemo(); obj.start(); } }Output: My thread is in running state.
Example: class MultithreadingDemo implements Runnable { public void run() { System.out.println("My thread is in running state."); } public static void main(String args[]) { MultithreadingDemo obj=new MultithreadingDemo(); Threadtobj =new Thread(obj); tobj.start(); } }Output: My thread is in running state. |
|
| 19. |
What is Thread in Java? |
|
Answer» Threads are basically the lightweight and smallest unit of processing that can be managed independently by a scheduler. Threads are REFERRED to as PARTS of a process that simply let a program EXECUTE efficiently with other parts or threads of the process at the same time. Using threads, one can perform complicated tasks in the easiest way. It is CONSIDERED the SIMPLEST way to take advantage of multiple CPUs available in a machine. They share the common address space and are independent of each other. |
|
| 20. |
What are the benefits of using Multithreading? |
|
Answer» There are various benefits of multithreading as GIVEN below:
|
|