1. What are the type Of Exceptions?
- Checked Exception :
- Unchecked Exception :
- It includes:
- Arithmetic Exception
- ArrayIndexOutOfBounds Exception
These exceptions are checked by the compiler at the time of compilation. Classes that extend Throwable class except Runtime exception and Error are called checked Exception.
Checked Exceptions must either declare the exception using throes keyword (or) surrounded by appropriate try/catch.
These exceptions are not checked during the compile time by the compiler. The compiler doesn’t force to handle these exceptions.
2. What are the different ways to handle exceptions?
- Using try/catch:A risky code is surrounded by try block. If an exception occurs, then it is caught by the catch block which is followed by the try block.
- By declaring throws keyword:At the end of the method, we can declare the exception using throws keyword.
3. What is Thread? How do you make thread in java?
In Java, the flow of a execution is called Thread. Every java program has at least one thread called main thread, the Main thread is created by JVM. The user can define their own threads by extending Thread class (or) by implementing Runnable interface. Threads are executed concurrently.
There are two ways to make a thread
- Extend Thread class:Extending a Thread class and override the run method. The thread is available in java.lang.thread.
- Implement Runnable interface:Another way is implementing the runnable interface. For that we should provide the implementation for run () method which is defined in the interface.
4. What is Multithreading?
Multithreading in java is a process of executing multiple threads simultaneously.
A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. However, we use multithreading than multiprocessing because threads use a shared memory area. They don’t allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.
Advantages Of Multithreading
- It doesn’t block the user because threads are independent and you can perform multiple operations at the same time.
- You can perform many operations together, so it saves time.
- Threads are independent, so it doesn’t affect other threads if an exception occurs in a single thread.
5. Life Cycle Of Thread
There are five state in thread
- New : The thread is in new state if you create an instance of Thread class but before the invocation of start() method.
- Runnable : The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.
- Running : The thread is in running state if the thread scheduler has selected it.
- Non-Runnable (Blocked) : This is the state when the thread is still alive, but is currently not eligible to run.
- Terminated : A thread is in terminated or dead state when its run() method exits.
6. What is Synchronization?
Synchronization makes only one thread to access a block of code at a time. If multiple thread accesses the block of code, then there is a chance for inaccurate results at the end. To avoid this issue, we can provide synchronization for the sensitive block of codes.The synchronized keyword means that a thread needs a key in order to access the synchronized code.
7. What is Serialization ?
Converting a file into a byte stream is known as Serialization. The objects in the file is converted to the bytes for security purposes. For this, we need to implement java.io.Serializable interface. It has no method to define.
Variables that are marked as transient will not be a part of the serialization. So we can skip the serialization for the variables in the file by using a transient keyword.
8. What is the difference between Serialization and Deserialization ?
|Serialization is the process which is used to convert the objects into byte stream||Deserialization is the opposite process of serialization where we can get the objects back from the byte stream.|
|An object is serialized by writing it an ObjectOutputStream.||An object is deserialized by reading it from an ObjectInputStream.|