Java Interview Questions & Answers (2–5 Years Experience)
SECTION 1: CORE JAVA
Q: Explain OOPs concepts with examples.
A: OOPs has 4 main principles:
- Encapsulation – Binding data and methods together in a class. Example: private variables
with public getters/setters.
- Abstraction – Hiding internal details and showing only essential features. Example: Using
interfaces or abstract classes.
- Inheritance – One class inherits properties and behavior from another. Example: class Dog
extends Animal.
- Polymorphism – Same method behaves differently. Compile-time: method overloading,
Runtime: method overriding.
Q: What is the difference between == and .equals() in Java?
A: == compares object references (memory addresses), while .equals() compares object
content (values).
Example:
String a = new String("test");
String b = new String("test");
a == b → false, [Link](b) → true
Q: What are the types of memory areas allocated by JVM?
A: - Heap: Stores objects.
- Stack: Stores method calls and local variables.
- Method Area: Stores class metadata, static variables.
- PC Register: Stores current executing instruction address.
Q: Explain the difference between ArrayList and LinkedList.
A: ArrayList uses dynamic arrays; fast for access, slow for insert/delete in middle.
LinkedList uses doubly linked list; fast for insert/delete, slow for access.
Q: What is the difference between HashMap and Hashtable?
A: HashMap is not synchronized, faster, allows one null key and multiple null values.
Hashtable is synchronized, thread-safe, doesn’t allow null keys or values.
Q: What is the significance of the transient keyword?
A: It prevents a variable from being serialized.
Q: Explain exception hierarchy in Java.
A: Throwable
├── Error (JVM-level issues)
└── Exception
├── Checked: IOException, SQLException
└── Unchecked: NullPointerException, RuntimeException
Q: How does garbage collection work in Java?
A: Java automatically removes unreferenced objects using algorithms like Mark and Sweep,
Generational GC, etc.
SECTION 2: FRAMEWORK (Spring / Hibernate / Struts)
Q: What is the Spring Framework? What are its key features?
A: Spring is a lightweight framework with features like:
- Dependency Injection
- AOP
- MVC architecture
- Integration with databases and other frameworks
- Spring Boot for rapid setup
Q: Explain Dependency Injection in Spring.
A: Injects dependencies into a class rather than creating them inside. Improves testability
and loose coupling.
Example:
@Autowired
private OrderService orderService;
Q: What is the difference between @Component, @Service, and @Repository?
A: All are stereotype annotations:
- @Component: Generic bean
- @Service: Business logic layer
- @Repository: DAO layer with DB exception translation
Q: How does Spring Boot differ from Spring?
A: Spring Boot simplifies Spring development with auto-configuration, embedded servers,
and starter dependencies.
Q: What is ORM? How does Hibernate implement ORM?
A: ORM maps Java objects to DB tables. Hibernate uses annotations like @Entity and
automates SQL generation.
Q: What are the advantages of Hibernate over JDBC?
A: - Less boilerplate code
- Automatic schema generation
- Caching
- Transaction management
Q: What is the difference between get() and load() methods?
A: get(): Returns null if object not found; hits DB immediately.
load(): Throws exception if not found; may return a proxy.
Q: What is the architecture of Struts 2 framework?
A: MVC-based:
- View: JSP
- Controller: FilterDispatcher
- Model: Business logic/JavaBeans
Q: How are validations handled in Struts?
A: Using XML-based validation files or annotations like @RequiredStringValidator.
SECTION 3: DATABASE (SQL)
Q: Write an SQL query to find the second highest salary from an employee table.
A: SELECT MAX(salary) FROM employee WHERE salary < (SELECT MAX(salary) FROM
employee);
Q: Explain the difference between INNER JOIN, LEFT JOIN, and RIGHT JOIN.
A: INNER JOIN: Only matching rows
LEFT JOIN: All rows from left + matched from right
RIGHT JOIN: All rows from right + matched from left
Q: What is a view? How is it different from a table?
A: View: Virtual table from a query, no physical storage.
Table: Physically stores data.
Q: What are indexes? How do they improve performance?
A: Indexes speed up search and filtering by maintaining a reference structure. Improves
SELECT performance.
SECTION 4: MULTITHREADING
Q: What is the difference between Runnable and Thread?
A: Runnable: Uses interface, preferred due to flexibility.
Thread: Extends class, limited to single inheritance.
Q: Explain the concept of synchronization in Java.
A: Synchronization ensures only one thread accesses a shared resource at a time to prevent
race conditions.
Q: What is a deadlock? How can it be prevented?
A: Deadlock: Two threads wait on each other for resources.
Prevented by lock ordering, tryLock(), or avoiding nested locks.
Q: What are volatile and synchronized keywords?
A: volatile: Visibility guarantee for variables across threads.
synchronized: Ensures mutual exclusion and visibility for critical sections.
SECTION 5: CODING QUESTIONS
Q: Write a Java program to remove duplicates from an array.
A: Use LinkedHashSet to preserve order and remove duplicates:
Set<Integer> set = new LinkedHashSet<>([Link](array));
Q: Write a Java function to reverse the words in a sentence.
A: Split the sentence into words, reverse the array, and join:
Input: "Java is awesome" → Output: "awesome is Java"
Behavioral / Project Experience Questions
Q: Describe a project where you implemented one of the listed frameworks. What
challenges did you face?
A: Worked on FxRetail using Spring Boot and microservices.
Challenges:
- Real-time data sync with MQ/WebSocket
- Managing multiple environments with Spring Profiles
- Thread-safety and exception handling
Q: Have you worked in Agile teams? How do you handle task prioritization?
A: Yes, we use two-week sprints with JIRA. I prioritize based on:
- Business value
- Blockers
- Impact vs effort
- Coordination with PO and team