Java Interview Questions
Java is the most common programming topic in backend and full-stack interviews. These are the questions interviewers actually ask, with concise answers you can speak confidently.
16 questions with concise, interview-ready answers.
1. What is the difference between JDK, JRE, and JVM?
The JVM (Java Virtual Machine) is the runtime engine that executes Java bytecode and is platform-specific. The JRE (Java Runtime Environment) is the JVM plus core libraries needed to run Java applications. The JDK (Java Development Kit) is the JRE plus development tools like the compiler (javac) and debugger, so you need it to compile and build Java code.
2. Why is Java called platform-independent?
The Java compiler turns source code into bytecode rather than native machine code. That bytecode runs on any platform that has a JVM, following the "write once, run anywhere" principle. The JVM itself is platform-specific, but your compiled code is not.
3. What are the four pillars of OOP in Java?
Encapsulation (bundling data with the methods that operate on it and hiding internal state behind access modifiers), Inheritance (a class deriving fields and behavior from a parent with extends), Polymorphism (one interface taking many forms via overriding and overloading), and Abstraction (exposing essential behavior while hiding implementation through abstract classes and interfaces).
4. What is the difference between == and equals() in Java?
The == operator compares references for objects, checking whether two variables point to the same object in memory (for primitives it compares values). The equals() method compares logical equality of content, and classes like String and Integer override it to compare actual values. As a rule, use == for primitives and equals() for objects.
5. Why are String objects immutable in Java?
Once created, a String's value cannot be changed; any modifying operation returns a new String. Immutability enables safe sharing in the String pool, makes strings safe to use as HashMap keys (their hash code never changes), improves security for things like file paths and credentials, and makes them inherently thread-safe. For heavy modification, use StringBuilder or StringBuffer instead.
6. What is the String pool?
The String pool is a special area of heap memory where the JVM stores string literals to save memory. When you create a string with a literal, the JVM reuses an existing pooled instance if one matches, so two identical literals share the same reference. Strings created with new always produce a separate object on the heap, though you can add them to the pool with intern().
7. What is the difference between ArrayList and LinkedList?
ArrayList is backed by a dynamic array, giving fast O(1) random access by index but slower insertions and deletions in the middle because elements must shift. LinkedList is a doubly linked list with fast O(1) insertions and deletions at the ends but slow O(n) random access. Use ArrayList for frequent reads and LinkedList when you do a lot of add/remove at the boundaries.
8. How does a HashMap work internally?
A HashMap stores key-value pairs in an array of buckets, choosing a bucket from the key's hashCode(). When two keys hash to the same bucket (a collision), entries are chained in a linked list, which converts to a balanced tree once a bucket exceeds a threshold (8 in Java 8+) for O(log n) lookups. Equality of keys is resolved with equals(), and the map resizes and rehashes when it passes its load factor (default 0.75).
9. What is the difference between checked and unchecked exceptions?
Checked exceptions (like IOException and SQLException) are checked at compile time and must be either caught or declared with throws. Unchecked exceptions extend RuntimeException (like NullPointerException and ArrayIndexOutOfBoundsException) and represent programming errors, so the compiler does not force you to handle them. Errors, like OutOfMemoryError, are serious conditions you are generally not expected to catch.
10. What is the difference between an interface and an abstract class?
An abstract class can have both abstract and concrete methods, instance fields, and constructors, and a class can extend only one of them. An interface defines a contract and (before Java 8) only abstract methods; since Java 8 it can also have default and static methods, but its variables are implicitly public static final and a class can implement many interfaces. Use an abstract class for a shared base with common state, and an interface to define capabilities.
11. What is the difference between final, finally, and finalize?
final is a keyword: a final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be extended. finally is a block that always runs after a try/catch, typically to release resources. finalize() was a method called by the garbage collector before reclaiming an object, but it is deprecated and unreliable, so cleanup should use try-with-resources or explicit close instead.
12. What is the difference between method overloading and overriding?
Overloading means multiple methods in the same class share a name but differ in parameter list; it is resolved at compile time and is a form of compile-time polymorphism. Overriding means a subclass provides its own implementation of a method inherited from a parent with the same signature; it is resolved at runtime and is runtime polymorphism. Overriding uses the @Override annotation and follows rules on access level and return type.
13. What is the difference between List, Set, and Map?
A List is an ordered collection that allows duplicate elements and indexed access, with implementations like ArrayList and LinkedList. A Set is a collection that does not allow duplicates, with implementations like HashSet (unordered) and TreeSet (sorted). A Map stores key-value pairs with unique keys, with implementations like HashMap, LinkedHashMap, and TreeMap; Map is not part of the Collection interface hierarchy.
14. How do you create a thread in Java?
You can extend the Thread class and override its run() method, or implement the Runnable interface and pass it to a Thread; implementing Runnable is preferred because it keeps your class free to extend something else. In modern code you typically submit Runnable or Callable tasks to an ExecutorService from java.util.concurrent rather than managing threads by hand. You start a thread with start(), which invokes run() on a new thread of execution.
15. What is the difference between synchronized and volatile?
synchronized provides mutual exclusion so only one thread enters a block or method at a time, guaranteeing both atomicity and visibility of changes. volatile only guarantees visibility — every read sees the latest write directly from main memory — but it does not make compound operations like increment atomic. Use volatile for simple flags and synchronized (or locks and atomic classes) when you need atomic updates.
16. How does garbage collection work in Java?
The JVM automatically reclaims memory from objects that are no longer reachable from any live reference, so you do not free memory manually. The heap is divided into generations — a young generation (Eden and survivor spaces) for new objects and an old generation for long-lived ones — based on the observation that most objects die young. Collectors like G1 and the older parallel collector run minor and major GC cycles; you can suggest collection with System.gc() but cannot force it.
Get these answered live in your real interview
NostrobeAI is a real-time AI interview copilot — it hears the question and drafts a strong answer on your screen, invisible on Zoom, Meet, and Teams. One-time pricing, no subscription.
Try NostrobeAI free