Saturday, 27 September 2014

Top 5 Java Interview Questions on Object

This top 5 questions asked by most of the MNC during Java/J2ee interview . These questions are based on java object and its behavior. 


Q.1. How you can define an object ? How its related to real world and it behaves ? With Example.

Ans : This question is often asked for all java interview from 1-8 year experience candidate. As we know object is a real world entity. It has three basic property i.e. State, Behavior and Identity. Simply we can say its a blueprint of class. Any thing around you is called an object. But yes, it has the above three property by which we can define one object. 

State: what the objects have. Dog have a name, age, color,etc .It is implemented by variables in java.

Behavior: what the objects do.Dog can bark, bite, run, etc. It is implemented by methods in java.

Identity: what makes them unique, Dog have id-unique, which is unique. (this is important when implementing the equals method, to determine if the objects are different or not) . Every object has different hash code.

Example - Suppose , there are many dogs. But, you can't say , that is an object. You need to take one dog which has name - dick,age-8,color -white and it must have a unique identification. And now we can say dick is a dog object. And this dick can bark, bite and run. These are the behavior that object can do, and in its implemented by methods. 

Q.2. How many contructor & methods are in Object class ?

Ans: There are only one constructor and nine methods in Object class. Its available inside java.lang package.

Constructor :- 
public Object()
 
Methods :-
 
 clone() - This create and return copy of the same object.
 equals() - This is used to compare some other object is equal to this or not. 
 toString() - Convert and return a string representation.
 finalize() - This is called by garbage collector when there are no more active reference to that object exist.
 getClass() - This returns the runtime class of this object.
 hashCode() - Returns an hash code for this object and its unique.
 notify() - Wakes up a single thread that is waiting on this object's monitor.
 notifyAll() - Wakes up all thread that is waiting on this object's monitor.
 wait() - This will make object to wait , until any other thread notify this.

Q.3. Why wait() and notify() method is inside Object class instead of Thread class ?
 
 Ans : This is a FAQ for any java interview. Because, wait() and notify() method works with multi-threaded environment. This is inside Object class because, we are doing the monitoring work over an Object . When ever we are calling these methods , we are calling over object , not on thread.

Q.4. How many ways we can create object in Java ?

Ans : There are basic four way for creating object in java as below :-

By using new keyword -
Object obj=new Object();
By using class.forName() -
Dog dogObj=(Dog)class.forName("jdeveloperguide.lab.Dog").newtInstance();

By using deserialization -
ObjectInputStream inStream = new ObjectInputStream(anInputStream);
Dog dogObject = (
Dog ) inStream.readObject();

By using clone() -
Dog dogObj=new Dog();
Dog dogObj2=dogObj.clone(); //It must implement clonnable interface.
Q.5. How JVM allocates object in memory ? What is heap and stack memory ?

Ans : Normally objects are allocated on heap memory. Heap memory is a dynamic memory which increase and decrease the size on run time. All primitive types,methods are allocated on stack memory. All object type and reference types are allocated on heap area.Every thread creates a stack (call stack ) and when thread completes the stack is also release the reserve memory. The stack is faster compare to heap.