Java Interview Question Set 14
Categories: Java 8(JDK1.8)
Can we declare a constructor as final?
The constructor can never be declared as final because it is never inherited. Constructors are not ordinary methods; therefore, there is no sense to declare constructors as final. However, if you try to do so, The compiler will throw an error.
Can we declare an interface as final?
No, we cannot declare an interface as final because the interface must be implemented by some class to provide its definition. Therefore, there is no sense to make an interface final. However, if you try to do so, the compiler will show an error.
What is the difference between the final method and abstract method?
The main difference between the final method and abstract method is that the abstract method cannot be final as we need to override them in the subclass to give its definition.
What is multithreading?
Multithreading is a process of executing multiple threads simultaneously. Multithreading is used to obtain the multitasking. It consumes less memory and gives the fast and efficient performance. Its main advantages are:
a) Threads share the same address space.
b) The thread is lightweight.
c) The cost of communication between the processes is low.
What is the thread?
A thread is a lightweight subprocess. It is a separate path of execution because each thread runs in a different stack frame. A process may contain multiple threads. Threads share the process resources, but still, they execute independently.
Differentiate between process and thread?
There are the following differences between the process and thread.
a) A Program in the execution is called the process whereas; A thread is a subset of the process
b) Processes are independent whereas threads are the subset of process.
c) Process have different address space in memory, while threads contain a shared address space.
d) Context switching is faster between the threads as compared to processes.
e) Inter-process communication is slower and expensive than inter-thread communication.
f) Any change in Parent process doesn't affect the child process whereas changes in parent thread can affect the child thread.
What do you understand by inter-thread communication?
a) The process of communication between synchronized threads is termed as inter-thread communication.
b) Inter-thread communication is used to avoid thread polling in Java.
c) The thread is paused running in its critical section, and another thread is allowed to enter (or lock) in the same critical section to be executed.
d) It can be obtained by wait(), notify(), and notifyAll() methods.
What is the purpose of wait() method in Java?
The wait() method is provided by the Object class in Java. This method is used for inter-thread communication in Java. The java.lang.Object.wait() is used to pause the current thread, and wait until another thread does not call the notify() or notifyAll() method. Its syntax is given below.
public final void wait()
Why must wait() method be called from the synchronized block?
We must call the wait method otherwise it will throw java.lang.IllegalMonitorStateException exception. Moreover, we need wait() method for inter-thread communication with notify() and notifyAll(). Therefore It must be present in the synchronized block for the proper and correct communication.
What are the advantages of multithreading?
Multithreading programming has the following advantages:
a) Multithreading allows an application/program to be always reactive for input, even already running with some background tasks
b) Multithreading allows the faster execution of tasks, as threads execute independently.
c) Multithreading provides better utilization of cache memory as threads share the common memory resources.
d) Multithreading reduces the number of the required server as one server can execute multiple threads at a time.