5 Common Java Interview Questions About Objects and Object Class
These are some common questions asked in Java/J2EE interviews. These questions are based on Java objects and their behavior.
Q.1. How can you define an object? How is it related to the real world and how does it behave? With Example.
Ans: This question is often asked in Java interviews for candidates with 1-8 years of experience. As we know, an object is a real-world entity. It has three basic properties i.e. State, Behavior and Identity. Simply we can say that a class is a blueprint for creating objects, and an object is an instance of a class. Anything around you can be represented as an object. But yes, it has the above three properties by which we can define an object.
State: what the object has. Dog has a name, age, color, etc. It is implemented by variables in Java.
Behavior: what the object does. Dog can bark, bite, run, etc. It is implemented by methods in Java.
Identity: what makes the object unique. Identity is what makes one object distinguishable from another object. It does not necessarily mean that every object must have a unique id variable. Different objects can also have the same 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 can be distinguished from other objects. And now we can say Dick is a dog object. And this Dick can bark, bite and run. These are the behaviors that the object can do, and they are implemented by methods.
Q.2. How many constructors & methods are in Object class?
Ans: The Object class has one constructor and several methods. The exact list depends on the Java version. It is available inside the java.lang package.
Constructor :-
public Object()
Methods :-
clone() - This creates and returns a copy of the object.
equals() - This is used to compare whether some other object is equal to this object or not.
toString() - Converts and returns a string representation of the object.
finalize() - An old mechanism associated with garbage collection. It is deprecated and should not be relied upon for resource cleanup.
getClass() - This returns the runtime class of this object.
hashCode() - Returns a hash code for this object. The hash code is not guaranteed to be unique.
notify() - Wakes up a single thread that is waiting on this object's monitor.
notifyAll() - Wakes up all threads that are waiting on this object's monitor.
wait() - This makes the current thread wait until another thread notifies it.
Q.3. Why are wait() and notify() methods inside Object class instead of Thread class?
Ans: This is a FAQ for any Java interview. The wait() and notify() methods are used in a multi-threaded environment. These methods are inside the Object class because we are doing the monitoring work over an object. Whenever we are calling these methods, we are calling them on an object, not on a thread.
The wait() and notify() methods are used for thread coordination through an object's monitor.
Q.4. How many ways can we create an object in Java?
Ans: There are basic four ways for creating an 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") .getDeclaredConstructor() .newInstance();By using deserialization -
ObjectInputStream inStream = new ObjectInputStream(anInputStream); Dog dogObject = (Dog) inStream.readObject();By using clone() -
Dog dogObj = new Dog(); Dog dogObj2 = dogObj.clone();
The class must implement the Cloneable interface and provide appropriate cloning support.
Q.5. How does JVM allocate objects in memory? What is heap and stack memory?
Ans: Normally, objects are allocated on heap memory. Heap memory is a dynamic memory area used for objects during runtime.
Each thread has its own stack for method calls and local variables. Objects are generally allocated on the heap, while references and local variables may be stored in the stack depending on the JVM implementation and optimization.
Every thread creates a stack (call stack), and when the thread completes, its stack is no longer needed and its memory can be reclaimed.
Stack operations are generally simpler and faster than heap allocation, but the actual behavior depends on the JVM and its optimizations.
You can share your interview experience with asked questions and answers for publishing on this blog.
No comments:
Post a Comment