HTML Java

Java Interview Questions


Ques: How to achieve concurrency in java ?

Sol: Executing multiple task parallely called concurrency. If you want to achieve java concurrency what can you do ? you have to create multiple threads in java .


Ques: Can we overload run() method ?

Sol: Yes we can but the thread class start method invoke run method which no return type and no arguments.


Ques: Can we call wait method inside method which is not Synchronized?

Sol: Yes


Ques: What is the difference between notify and notifyAll?

Sol: We can use notify() to give notification for only 1 waiting thread. If multiple thread are waiting on same object but 1 thread will be notified and remaining threads will wait for further notifications. which thread will be notified it depends on JVM.So we should use nofitfyAll() method for notifying to all waiting thread on same object.

Example: suppose 10 thread is waiting for s1 object lock and 5 thread is waiting for s2 object lock. When we use this s1.notifyAll() , then all 10 thread come for waiting state to ready state and still 5 threads are waiting for object s2 lock.


Ques: What is a class loader?

Sol: Class loader are the program which performs class loading.

Loading are two types
  • Static loading
  • Dynamic loading

Ques: What happens at the class loading time?

Sol: Memory is allocated for Static Data Member ->default value is assign to static data member -> static block are execute in given order. At the time of class loading 1st static data get memory in class area ,where all the class data goes. If static block of any class is executed it means that class loaded into memory.


Ques: What is dynamic class loading?

Sol: We have 1 class in java. which name is Class and it has method forName().This method load classes dynamically. And return the object of loaded class.

Example:

class Class{
public static Class forName(String name){
}
}


Ques: Who is responsible for class loading?

Sol: Class loaders are the special program which run inside the jvm which performs class loading.

Bootstrap,Extension,Application/extension these loader goes to different-2 places to perform class loading and search the class.

The class loading means JVM load the class details into memory, like Datamember and constructor, methods.


Ques: Why we cannot use static before class and constructor?

Sol: Because when we use static, then we are fixing the class. Means it’s fix data and class. If we fix both then, it’s no purpose to create an object of that class.

And before constructor not use static ,why because constructor is object specific. And it call for every object if we use static then it fix data for every object.