Java Interview Questions and Answers
Common Java interview questions for freshers. Tap a question to see the answer.
1. What is the difference between JDK, JRE and JVM?
The JVM runs Java bytecode. The JRE is the JVM plus the libraries needed to run programs. The JDK includes development tools such as the compiler, along with everything needed to run Java.
2. Why is Java platform independent?
Java code is compiled into bytecode, which can run on any system that has a suitable JVM.
3. What are the four main OOP concepts?
Encapsulation, inheritance, polymorphism and abstraction.
4. What is the difference between == and equals()?
For objects, == checks whether two references point to the same object. equals() can compare contents if the class overrides it, as String does.
5. What is method overloading and overriding?
Overloading means the same method name with different parameters. Overriding means a subclass provides its own version of an inherited method.
6. Why are Strings immutable?
A String cannot be changed after it is created. This allows safe sharing, reuse through the String pool and thread safety.
7. What is the difference between ArrayList and LinkedList?
ArrayList uses a resizable array and gives fast access by index. LinkedList uses linked nodes and is efficient for adding or removing at either end.
8. What is the difference between an abstract class and an interface?
An abstract class can have constructors and instance fields, and a class can extend only one. A class can implement many interfaces, whose fields are constants.
9. What are checked and unchecked exceptions?
Checked exceptions must be caught or declared, for example IOException. Unchecked exceptions are not forced by the compiler, for example NullPointerException.
10. What is garbage collection?
It is the JVM process that frees memory used by objects that can no longer be reached.