Top 20 Java Interview Questions and Answers for Freshers (2025 Edition)
Preparing for your first Java interview can feel overwhelming. To help you, we’ve compiled the top 20 most frequently asked Java interview questions with expanded answers. These cover core concepts and practical knowledge that every fresher should be confident about.
Part 1: Core Java Basics
1. What is Java?
Java is a high-level, object-oriented, platform-independent programming language developed by Sun Microsystems (now owned by Oracle). Its key philosophy is “write once, run anywhere,” meaning code compiled into bytecode can run on any system that has the Java Virtual Machine (JVM). Java is widely used for web applications, enterprise systems, Android development, and cloud solutions because of its scalability, security, and community support.
2. What are the main features of Java?
Java provides several powerful features:
-
Platform Independence: Programs run across operating systems without recompilation.
-
Object-Oriented: Everything is treated as an object, making code modular and reusable.
-
Robust & Secure: Strong memory management and security checks.
-
Automatic Garbage Collection: Frees unused memory.
-
Multithreaded: Supports concurrent execution of tasks.
These features make Java suitable for building reliable, secure, and scalable applications.
3. What is JVM, JRE, and JDK?
-
JVM (Java Virtual Machine): Executes Java bytecode and provides a runtime environment.
-
JRE (Java Runtime Environment): Includes JVM plus libraries and components required to run Java applications.
-
JDK (Java Development Kit): Includes JRE along with development tools like compiler (
javac
), debugger, and documentation generator.
In short, JDK = JRE + development tools.
4. Difference between JDK 8 and JDK 17?
JDK 8 (2014) introduced major changes like Lambda Expressions, Stream API, and new Date-Time API. JDK 17 (2021) is a long-term support (LTS) version with advanced features such as Sealed Classes, Pattern Matching for instanceof
, Switch Expressions, Text Blocks, and strong performance optimizations. JDK 17 also removed outdated modules, making Java leaner and more efficient.
5. What is the difference between == and .equals() in Java?
-
==
operator: Compares memory references (whether two variables point to the same object). -
.equals()
method: Compares the actual content or value of objects.
For example, two different String objects with the same text will be equal using.equals()
but not necessarily with==
. Understanding this difference is crucial in avoiding logical errors during comparisons.
6. Explain OOPS concepts in Java.
Java’s object-oriented features revolve around four pillars:
-
Encapsulation: Bundling variables and methods into a class while restricting direct access.
-
Inheritance: Allowing one class to inherit features from another, promoting reusability.
-
Polymorphism: A method or object behaving differently depending on context (compile-time and runtime).
-
Abstraction: Hiding implementation details and exposing only functionality.
Together, these make Java flexible, reusable, and easier to maintain.
7. What is a constructor?
A constructor is a special method used to initialize objects. It has the same name as the class and does not have a return type. Constructors can be default (no arguments) or parameterized (accept values for initialization). Unlike regular methods, they are called automatically when an object is created. Example:
class Student {
String name;
Student(String n) {
name = n;
}
}
Here, new Student("John")
automatically calls the constructor.
8. Difference between method overloading and overriding.
-
Overloading: Defining multiple methods with the same name but different parameter lists. It is resolved at compile-time (static polymorphism).
-
Overriding: Redefining a method from the parent class in the child class with the same signature. It is resolved at runtime (dynamic polymorphism).
Overloading gives flexibility, while overriding provides specific implementations in subclasses.
9. What are access modifiers in Java?
Java provides four access modifiers:
-
public: Accessible from anywhere.
-
protected: Accessible within the package and subclasses.
-
default (no modifier): Accessible within the package only.
-
private: Accessible within the same class.
They control visibility and maintain security of class members.
10. What is the difference between ArrayList and LinkedList?
-
ArrayList: Based on a dynamic array. Fast for random access but slower for insertions/deletions (except at the end).
-
LinkedList: Based on a doubly linked list. Efficient for insertions and deletions but slower for searching.
Choice depends on the use case: frequent access → ArrayList, frequent modifications → LinkedList.
Part 2: Advanced Java Concepts
11. What is the difference between HashMap and Hashtable?
HashMap is non-synchronized, allows one null key and multiple null values, and is faster. Hashtable is synchronized, does not allow null keys/values, and is considered legacy. For multi-threaded applications, use ConcurrentHashMap instead.
12. Explain final, finally, and finalize.
-
final: Used for constants, preventing method overriding, or inheritance.
-
finally: Block that executes regardless of exception handling outcome.
-
finalize(): Method called by Garbage Collector before destroying an object.
These keywords help in code safety, resource cleanup, and preventing unintended changes.
13. What is multithreading in Java?
Multithreading is executing multiple threads concurrently to maximize CPU usage. Threads can be created using the Thread
class or Runnable
interface. Benefits include better resource utilization and responsiveness, but it also introduces complexity with synchronization and deadlocks.
14. Difference between throw and throws.
-
throw: Used inside methods to explicitly throw an exception. Example:
throw new IOException();
-
throws: Declares exceptions in the method signature that may be thrown. Example:
void readFile() throws IOException
.
throw
is for generating exceptions, whilethrows
is for declaring them.
15. What are checked and unchecked exceptions?
-
Checked Exceptions: Checked at compile-time, e.g., IOException, SQLException. Must be handled with try-catch or declared with
throws
. -
Unchecked Exceptions: Occur at runtime, e.g., NullPointerException, ArithmeticException. Usually caused by programming errors.
16. What is garbage collection?
Garbage collection is the process of automatic memory management where the JVM frees memory by removing unused objects. Developers do not explicitly delete objects, which prevents memory leaks. Tools like System.gc()
can request garbage collection, but JVM decides the timing.
17. Explain static keyword.
The static
keyword makes variables and methods belong to the class rather than objects.
-
Static variables: Shared among all instances.
-
Static methods: Called without creating an object.
-
Static blocks: Executed once when the class is loaded.
Static features reduce memory usage and support utility methods.
18. What are abstract classes and interfaces?
-
Abstract Class: Can have both abstract methods (no body) and concrete methods. Supports constructors and state (variables).
-
Interface: Defines a contract with abstract methods (till Java 7), and from Java 8, can include default and static methods. Interfaces are used for multiple inheritance of behavior.
19. Difference between String, StringBuilder, and StringBuffer.
-
String: Immutable, every modification creates a new object.
-
StringBuilder: Mutable, faster, but not thread-safe.
-
StringBuffer: Mutable, thread-safe, slower due to synchronization.
Choice depends on mutability and thread-safety requirements.
20. What is the difference between process and thread?
-
Process: Independent execution unit with its own memory space. Heavyweight.
-
Thread: Lightweight sub-process within a process, shares memory and resources.
Threads are more efficient for multitasking within a single application.
Conclusion
These 20 Java interview questions cover essential basics and advanced concepts. By preparing these thoroughly, freshers can answer confidently and stand out in interviews. For success in 2025, continue practicing coding problems, mock interviews, and stay updated with new Java features.
0 Comments