Interview Preparation

Java Interview Questions & Answers for 2026

Curated questions covering core concepts, practical scenarios, and tradeoffs — suitable for fresher, 2-year, and 5-year experience levels.

Q1. What is the difference between == and .equals() in Java?

== compares object references — it checks whether two variables point to the exact same object in memory. .equals() compares the content or value of two objects as defined by the class's equals() method. For String, Integer, and most value objects always use .equals(). String literals may pass == due to the string pool, which creates a false sense of correctness. When you override equals() you must also override hashCode() so objects work correctly in HashMap and HashSet. This is one of the most common Java interview questions.

Q2. Explain the Java memory model: heap, stack, and method area.

The heap stores all objects and class instances — shared across all threads, managed by the garbage collector. The stack stores method call frames, local variables, and references — each thread has its own stack. Stack memory is faster but limited. The method area (Metaspace in Java 8+) stores class metadata, static variables, and constant pools. When you create a new object with new, it goes to the heap; the reference variable lives on the stack. Understanding this explains NullPointerException, StackOverflowError, and OutOfMemoryError.

Q3. What are the differences between ArrayList and LinkedList?

ArrayList uses a dynamic array internally — O(1) for random access by index, O(n) for insertion or deletion in the middle because elements must shift. LinkedList uses doubly-linked nodes — O(n) for random access, O(1) for insertion or deletion at a known position. In practice ArrayList outperforms LinkedList for most workloads because of better cache locality. Use LinkedList when you frequently insert or remove from the beginning or middle and rarely access by index. Both implement List but LinkedList also implements Deque.

Q4. What is the difference between an abstract class and an interface in Java?

An abstract class can have instance variables, constructors, and concrete methods alongside abstract ones. A class can extend only one abstract class. An interface (Java 8+) can have default and static methods but historically only abstract methods and constants. A class can implement multiple interfaces. Use abstract classes when classes share common state and behaviour. Use interfaces to define a contract or capability that unrelated classes can implement. Java 8 default methods blurred the line but interfaces still cannot have instance variables or constructors.

Q5. How does Java's garbage collection work and what are its types?

Java GC automatically frees heap memory occupied by objects with no live references. The heap is divided into Young Generation (Eden + Survivor spaces) and Old Generation (Tenured). Most objects die young — Minor GC collects the Young Generation frequently and cheaply. Surviving objects are promoted to Old Generation, collected by Major/Full GC which is more expensive and causes longer pauses. Common collectors: Serial (single-threaded), Parallel (multi-threaded), G1 (default since Java 9, balanced pause times), and ZGC/Shenandoah (low-latency, sub-millisecond pauses for large heaps).

Q6. What is the difference between synchronized, volatile, and ReentrantLock?

synchronized ensures mutual exclusion — only one thread executes a synchronized block or method at a time, and it also provides visibility (changes made inside are visible to other threads). volatile ensures visibility only — reads and writes to a volatile variable bypass the CPU cache and go directly to main memory, but it does not ensure atomicity for compound operations. ReentrantLock provides the same mutual exclusion as synchronized but with additional features: tryLock with timeout, interruptible locking, and fair ordering. Always prefer higher-level java.util.concurrent utilities when possible.

Q7. What is Spring Boot and how does it differ from the Spring Framework?

Spring Framework is a comprehensive dependency injection and AOP container requiring significant XML or Java configuration. Spring Boot is an opinionated wrapper around Spring that provides auto-configuration, embedded servers (Tomcat by default), and starter dependencies — you can have a production-ready REST API in minutes with minimal configuration. Spring Boot uses convention over configuration: it detects which libraries are on the classpath and auto-configures beans accordingly. application.properties/yml overrides defaults. Spring Boot does not replace Spring — it simplifies bootstrapping and is now the standard way to build Spring applications.

Q8. Explain Java Streams API with a practical example.

The Streams API processes collections declaratively using method chaining. Example: List<String> seniors = employees.stream().filter(e -> e.getAge() > 30).map(Employee::getName).sorted().collect(Collectors.toList()); — this filters by age, extracts names, sorts, and collects to a list. Streams are lazy — intermediate operations (filter, map, sorted) are not executed until a terminal operation (collect, forEach, count) is called. Use parallel streams for large datasets with independent operations. Avoid streams for simple loops — they add overhead for trivial cases.

Practice these questions with AI

Use our Mock Interview tool to answer questions and receive instant AI scoring and model answers.

Start Mock InterviewGenerate Custom Questions