This collection includes 30 Java interview questions and answers to help you prepare for technical interviews. Each question and answer is written clearly and uses straightforward language. These questions cover essential Java concepts, like the differences between classes and objects, keywords, data structures, exception handling, multithreading, access modifiers, and other key aspects of Java programming. The goal is to make it easier for you to confidently respond to interview questions in technical interviews.
1. Explain the difference between a class and an object in Java.
- A class is a blueprint for creating objects, while an object is an instance of a class. You can create multiple objects based on a single class.
2. What is the purpose of the static keyword in Java?
- The static keyword is used to create class-level variables and methods. These are associated with the class itself, rather than with instances of the class.
3. How does Java handle multiple inheritance, and why doesn’t it support it directly?
- Java achieves multiple inheritance through interfaces. Java doesn’t support direct multiple inheritance to avoid the “diamond problem” and maintain code simplicity and clarity.
4. Describe the differences between the equals() method and the == operator in Java.
- The equals() method is used to compare the content of objects for equality, while the == operator compares object references for identity.
5. What is the difference between the throw and throws keywords in Java?
- The throw keyword is used to explicitly throw an exception, while the throws keyword is used to declare exceptions that a method might throw.
6. Explain the purpose of the finalize() method in Java.
- The finalize() method is used for cleanup operations before an object is garbage collected. It’s called by the garbage collector when no more references to the object exist.
7. What is the difference between ArrayList and LinkedList in Java?
- ArrayList is implemented as a dynamic array, while LinkedList is implemented as a doubly-linked list. ArrayList provides fast random access, while LinkedList is efficient for insertions and deletions.
8. How does Java handle memory management, and what is the purpose of the garbage collector?
- Java uses automatic memory management to allocate and deallocate memory. The garbage collector’s purpose is to identify and free up memory occupied by objects that are no longer reachable or referenced.
9. Explain the purpose of the volatile keyword in Java.
- The volatile keyword is used to indicate that a variable’s value may be changed by multiple threads simultaneously. It ensures that changes to the variable are visible to all threads.
10. What are the differences between the public, private, protected, and default (package-private) access modifiers in Java?
- public allows access from any class, private restricts access to the declaring class, protected allows access to subclasses and classes in the same package, and default access is accessible only within the same package