Thursday, November 25, 2021

Top Java Interview Question and Answers series - Part4

********************************************
Top Java Interview Question and Answers series - Part4
********************************************


***Java Interview questions on threads***


76) What are user defined exceptions?
To create customized error messages we use userdefined exceptions. We can create user defined exceptions as checked or unchecked exceptions. We can create user defined exceptions that extend Exception class or subclasses of checked exeptions so that userdefined exception becomes checked. Userdefined exceptions can extend RuntimeException to create userdefined unchecked exceptions.
Note : It is recommended to keep our customized exception class as unchecked,i.e we need to extend Runtime Exception class but not Excpetion class.


77) Can we rethrow the same exception from catch handler?
Yes we can rethrow the same exception from our catch handler. If we want to rethrow checked exception from a catch block we need to declare that exception.


78) Can we nested try statements in java?
Yes try statements can be nested. We can declare try statements inside the block of another try statement.


79) Explain the importance of throwable class and its methods?
Throwable class is the root class for Exceptions. All exceptions are derived from this throwable class. The two main subclasses of Throwable are Exception and Error. The three methods defined in throwable class
are :
1) void printStackTrace() : This prints the exception information in the following format : Name of the exception, description followed by stack trace.
2) getMessage(): This method prints only the description of Exception.
3) toString(): It prints the name and description of Exception.


80) Explain when ClassNotFoundException will be raised ?
When JVM tries to load a class by its string name, and couldn’t able to find the class classNotFoundException will be thrown. An example for this exception is when class name is misspelled and when we try to load the class by string name hence class cannot be found which raises ClassNotFoundException.


81) Explain when NoClassDefFoundError will be raised ?
This error is thrown when JVM tries to load the class but no definition for that class is found NoClassDefFoundError will occur. The class may exist at compile time but unable to find at runtime. This might be due to misspelled classname at command line, or classpath is not specified properly , or the class file with byte code is no longer available.


83) What is process ?
A process is a program in execution. Every process have their own memory space.Process are heavy weight and requires their own address space. One or more threads make a process.


84) What is thread in java?
Thread is separate path of execution in program. Threads are
1) Light weight
2) They share the same address space.
3) creating thread is simple when compared to process because creating thread requires less resources when compared to process
4) Threads exists in process. A process have atleast one thread.


85) Difference between process and thread?
Process Thread
1) Program in execution. Separate path of execution in program. One or more threads is called as process.
2) Processes are heavy weight Threads are light weight.
3) Processes require separate address space. Threads share same address space.
4) Interprocess communication is expensive. Interthread communication is less expensive compared to processes.
5) Context switching from one process to another is costly. Context switching between threads is low cost.


86) What is multitasking ?
Multitasking means performing more than one activity at a time on the computer. Example Using spreadsheet and using calculator at same time.


87) What are different types of multitasking?
There are two different types of multitasking :
1) Process based multitasking
2) Thread based multitasking
Process based multitasking : It allows to run two or more programs concurrently. In process based multitasking a process is the smallest part of code .
Example : Running Ms word and Ms powerpoint at a time.
Thread based multitasking : It allows to run parts of a program to run concurrently.
Example : Formatting the text and printing word document at same time . Java supports thread based multitasking and provides built in support for multithreading.


88) What are the benefits of multithreaded programming?
Multithreading enables to use idle time of cpu to another thread which results in faster execution of program. In single threaded environment each task has to be completed before proceeding to next task making cpu idle.


89) Explain thread in java?
1) Thread is independent path of execution with in a program.
2) A thread consists of three parts Virtual Cpu, Code and data.
3) At run time threads share code and data i.e they use same address space.
4) Every thread in java is an object of java.lang.Thread class.


90) List Java API that supports threads?
java.lang.Thread : This is one of the way to create a thread. By extending Thread class and overriding run() we can create thread in java.
java.lang.Runnable : Runnable is an interface in java. By implementing runnable interface and overriding run() we can create thread in java.
java.lang.Object : Object class is the super class for all the classes in java. In object class we have three
methods wait(), notify(), notifyAll() that supports threads. java.util.concurrent : This package has classes and interfaces that supports concurrent programming.
Ex : Executor interface, Future task class etc.


91) Explain about main thread in java?
Main thread is the first thread that starts immediately after a program is started Main thread is important because :
1) All the child threads spawn from main thread.
2) Main method is the last thread to finish execution. When JVM calls main method() it starts a new thread. Main() method is temporarily stopped while the new thread starts running.


92) In how many ways we can create threads in java?
We can create threads in java by any of the two ways :
1) By extending Thread class
2) By Implementing Runnable interface.


93) Explain creating threads by implementing Runnable class?
This is first and foremost way to create threads . By implementing runnable interface and implementing run() method we can create new thread.
Method signature : public void run() Run is the starting point for execution for another thread within our program.
Example :
public class MyClass implements Runnable {
@Override
public void run() {
// T
}
}


94) Explain creating threads by extending Thread class ?
We can create a thread by extending Thread class. The class which extends Thread class must override the run() method.
Example :
public class MyClass extends Thread {
@Override
public void run() {
24
// Starting point of Execution
}
}



95) Which is the best approach for creating thread ?
The best way for creating threads is to implement runnable interface. When we extend Thread class we can’t extend any other class. When we create thread by implementing runnable interface we can implement Runnable interface. In both ways we have to implement run() method.


96) Explain the importance of thread scheduler in java?
Thread scheduler is part of JVM use to determine which thread to run at this moment when there are multiple threads. Only threads in runnable state are choosen by scheduler.
Thread scheduler first allocates the processor time to the higher priority threads. To allocate microprocessor time in between the threads of the same priority, thread scheduler follows round robin
fasion.



97) Explain the life cycle of thread?
A thread can be in any of the five states :
1) New : When the instance of thread is created it will be in New state.
Ex : Thread t= new Thread(); In the above example t is in new state. The thread is created but not in active state to make it active we need to call start() method on it.
2) Runnable state : A thread can be in the runnable state in either of the following two ways :
a) When the start method is invoked or
b) A thread can also be in runnable state after coming back from blocked or sleeping or waiting state.
3) Running state : If thread scheduler allocates cpu time, then the thread will be in running state.
4) Waited /Blocking/Sleeping state: In this state the thread can be made temporarily inactive for a short period of time. A thread can be in
the above state in any of the following ways:
1) The thread waits to acquire lock of an object.
2) The thread waits for another thread to complete.
3) The thread waits for notification of other thread.
5) Dead State : A thread is in dead state when thread’s run method execution is complete. It dies
automatically when thread’s run method execution is completed and the thread object will be garbage
collected.


98) Can we restart a dead thread in java?
If we try to restart a dead thread by using start method we will get run time exception since the thread is not alive.


99) Can one thread block the other thread?
No one thread cannot block the other thread in java. It can block the current thread that is running.


100) Can we restart a thread already started in java?
A thread can be started in java using start() method in java. If we call start method second time once it is started it will cause RunTimeException(IllegalThreadStateException). A runnable thread cannot be restarted.

No comments:

Post a Comment

How to install Java on EC2

***************************************** How to install Java on EC2 ***************************************** To be continued, In this post...

All Time Popular Post