Top Java Interview Questions and Answers TO GET YOU HIRED in 2026
1. What is Java?
Answer:
Java is a high-level, object-oriented, platform-independent programming language developed by Sun Microsystems (now Oracle). Java follows the principle “Write Once, Run Anywhere (WORA)” because Java programs run on the JVM.
2. What are the main features of Java?
Answer:
- Platform Independent
- Object-Oriented
- Robust
- Secure
- Multithreaded
- High Performance (with JIT Compiler)
- Distributed
- Portable
- Automatic Garbage Collection
3. What is JVM?
Answer:
JVM (Java Virtual Machine) is responsible for executing Java bytecode. It converts bytecode into machine code and provides platform independence.
4. Difference between JDK, JRE, and JVM?
| JDK | JRE | JVM |
|---|---|---|
| Development Kit | Runtime Environment | Virtual Machine |
| Includes JRE + Tools | Includes JVM + Libraries | Executes Bytecode |
| Used by Developers | Used by Users | Runs Java Programs |
5. What is Object-Oriented Programming (OOP)?
Answer:
OOP is a programming paradigm based on objects and classes.
The four pillars are:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
6. What is Encapsulation?
Answer:
Encapsulation means wrapping data and methods together into a single unit (class) while restricting direct access using private variables.
Example:
class Student{
private int age;
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
7. What is Inheritance?
Answer:
Inheritance allows one class to acquire properties and methods of another class.
Example:
class Animal{
void eat(){}
}
class Dog extends Animal{
void bark(){}
}
8. What is Polymorphism?
Types
- Compile-time (Method Overloading)
- Runtime (Method Overriding)
9. Method Overloading vs Overriding
| Overloading | Overriding |
|---|---|
| Same method name | Same method name |
| Different parameters | Same parameters |
| Compile-time | Runtime |
| Same class | Parent & Child |
10. What is Abstraction?
Answer:
Abstraction hides implementation details while exposing only essential functionality.
Achieved using:
- Abstract Classes
- Interfaces
11. Abstract Class vs Interface
| Abstract Class | Interface |
|---|---|
| Can have constructors | Cannot have constructors |
| Supports partial abstraction | Supports full abstraction (plus default/static methods) |
Uses extends |
Uses implements |
12. What is a Constructor?
Answer:
A constructor initializes objects.
Types:
- Default Constructor
- Parameterized Constructor
13. What is this keyword?
Answer:this refers to the current object.
Uses:
- Refer current instance variable
- Call another constructor
- Return current object
14. What is super keyword?
Answer:super refers to the parent class.
Uses:
- Access parent variables
- Call parent methods
- Invoke parent constructor
15. What is String Pool?
Answer:
Java stores string literals in a special memory area called the String Constant Pool to improve memory efficiency.
Example:
String s1="Java";
String s2="Java";
Both refer to the same object.
16. Difference between == and .equals()
== |
.equals() |
|---|---|
| Compares references | Compares content |
| Memory address | Actual value |
17. String vs StringBuilder vs StringBuffer
| String | StringBuilder | StringBuffer |
|---|---|---|
| Immutable | Mutable | Mutable |
| Slow | Fast | Thread-safe |
18. What is Exception Handling?
Answer:
Java handles runtime errors using:
- try
- catch
- finally
- throw
- throws
19. Checked vs Unchecked Exceptions
Checked
- IOException
- SQLException
Unchecked
- NullPointerException
- ArithmeticException
- ArrayIndexOutOfBoundsException
20. What is Garbage Collection?
Answer:
Garbage Collector automatically removes unused objects from memory.
21. What is Multithreading?
Answer:
Multithreading allows multiple threads to execute simultaneously.
Advantages:
- Faster execution
- Better CPU utilization
- Improved responsiveness
22. Difference between Process and Thread
| Process | Thread |
|---|---|
| Independent | Part of process |
| Heavyweight | Lightweight |
| Separate memory | Shared memory |
23. What is Synchronization?
Answer:
Synchronization prevents multiple threads from accessing shared resources simultaneously.
24. What is the Collection Framework?
Answer:
Java Collection Framework provides classes and interfaces to store and manipulate data.
Major Interfaces:
- List
- Set
- Queue
- Map
25. ArrayList vs LinkedList
| ArrayList | LinkedList |
|---|---|
| Dynamic Array | Doubly Linked List |
| Faster search | Faster insertion/deletion |
26. HashMap vs Hashtable
| HashMap | Hashtable |
|---|---|
| Not synchronized | Synchronized |
| Allows one null key | No null key |
27. HashMap vs TreeMap
| HashMap | TreeMap |
|---|---|
| Unordered | Sorted |
| Faster | Slower |
28. HashSet vs TreeSet
| HashSet | TreeSet |
|---|---|
| Unordered | Sorted |
| Faster | Slower |
29. What is Java 8?
Features
- Lambda Expressions
- Stream API
- Functional Interface
- Optional Class
- Default Methods
- Method References
30. What is Lambda Expression?
Example:
(a,b) -> a+b;
Used for writing concise functional code.
31. What is Stream API?
Example:
list.stream()
.filter(x -> x > 10)
.forEach(System.out::println);
32. What is Optional?
Answer:
Optional helps avoid NullPointerException.
Example:
Optional<String> name = Optional.of("Java");
33. What is Functional Interface?
Answer:
An interface containing exactly one abstract method.
Example:
@FunctionalInterface
interface Demo{
void show();
}
34. What are Java 17 features?
- Sealed Classes
- Pattern Matching (enhanced in later releases)
- Improved Switch Expressions
- Records
- Better Garbage Collectors
- Long-Term Support (LTS)
35. What are Java 21 features?
- Virtual Threads (Project Loom)
- Sequenced Collections
- String Templates (Preview)
- Record Patterns
- Pattern Matching Improvements
- Scoped Values
36. What are Virtual Threads?
Answer:
Virtual Threads are lightweight threads introduced with Project Loom, enabling applications to handle a very large number of concurrent tasks with lower memory usage than traditional platform threads.
37. What is Spring Boot?
Answer:
Spring Boot is a Java framework for rapidly building production-ready applications with embedded servers, auto-configuration, and simplified dependency management.
38. What is Dependency Injection?
Answer:
Dependency Injection (DI) is a design pattern where dependencies are provided by a framework or container rather than being created inside the class, improving modularity and testability.
39. What is Hibernate?
Answer:
Hibernate is an ORM (Object-Relational Mapping) framework that maps Java objects to database tables and simplifies database operations.
40. What are REST APIs?
Answer:
REST APIs are web services that use HTTP methods (GET, POST, PUT, DELETE) to communicate between clients and servers, commonly exchanging JSON data.