Java Interview Questions Part 1 - Vamsi Bhavani

1. What is the difference between == and .equals() in Java for comparing objects?
The == operator compares object references, checking if they refer to the same object..equals() is a method that should be overridden in classes to compare the actual content of objects.

2. Explain the concept of polymorphism in Java.
Polymorphism allows objects of different classes to be treated as objects of a common type.It can be achieved through method overloading and method overriding.

3. What is the static keyword used for in Java?
static is used to declare a class-level variable or method that belongs to the class rather than instances of the class.It allows access without creating an instance of the class.

4. How does exception handling work in Java?
Exceptions are handled using try, catch, finally blocks.The try block contains the code that might throw an exception, and the catch block handles the exception.The finally block is executed whether an exception is caught or not.

5. Differentiate between HashMap and HashTable in Java.
HashMap is not synchronized and allows null values, while HashTable is synchronized and doesn't allow nulls.HashMap is generally preferred unless synchronization is explicitly needed.

6. How does the equals() method work in Java, and why is it important to override it?
The equals() method is used to compare the content of objects for equality.It is important to override it to provide a meaningful comparison specific to the class.

7. What is the purpose of the super keyword in Java?
super is used to refer to the superclass (parent class) members.It is used to invoke the superclass methods and access the superclass fields.

8. How does garbage collection work in Java?
Garbage collection in Java is the process of automatically reclaiming memory occupied by objects that are no longer reachable.It is carried out by the garbage collector, which identifies and deletes unreferenced objects.

9. What is the difference between the throw and throws keywords in Java?
throw is used to explicitly throw an exception within a method.throws is used in the method signature to declare the exceptions that the method might throw.

10. How is method overloading different from method overriding in Java?
Method overloading involves defining multiple methods in the same class with the same name but different parameters.Method overriding occurs when a subclass provides a specific implementation for a method already defined in its superclass..