121 Java interview questions with answers
121 Java interview questions with detailed answers, sorted from basic to advanced. Free to practice, with progress saved automatically.
All Java interview questions
What is Java and why is it still widely used today?
What is the difference between == and .equals() in Java?
Is Java a purely object-oriented language?
What is the difference between Stack and Heap memory in Java?
What are the five key features of Java?
Why are Strings immutable in Java?
What is the difference between String, StringBuilder, and StringBuffer?
What is constructor overloading?
What are access modifiers in Java? Explain each one.
What is the difference between method overloading and method overriding?
What are the four characteristics (pillars) of OOP?
What is a static member? When should you use it?
What is a NullPointerException and how do you prevent it?
What is the difference between this and super in Java?
What is the final keyword and what are its three uses?
What is the difference between an abstract class and an interface?
What is the Stream API and functional programming in Java 8?
What is a functional interface? How do lambda expressions relate?
What is the difference between checked and unchecked exceptions?
What is a method reference in Java? How does it differ from a lambda?
What is the Optional class and how does it help avoid null pointer exceptions?
What is the Collections Framework and why is it important?
What is the difference between ArrayList and LinkedList?
What is the difference between HashSet and TreeSet?
What is the difference between HashMap and TreeMap?
What is the difference between HashSet and HashMap?
What is the Iterator interface and why should you use it?
What is the difference between fail-fast and fail-safe iterators?
What is the Comparable interface and how does it differ from Comparator?
What is the Queue interface and when would you use it?
What is ConcurrentHashMap and how does it differ from Hashtable?
What are generics and type erasure in Java?
What is inheritance and what are its types in Java?
What is the Liskov Substitution Principle (LSP)?
What is polymorphism? Explain compile-time and runtime polymorphism.
What is an abstract class? When should you use it?
What is an interface? What changed in Java 8?
What are the SOLID principles? Explain S and O.
What is the Singleton pattern?
What is the Factory pattern?
What is composition and when do you prefer it over inheritance?
What is method hiding? How does it differ from method overriding?
What is the diamond problem in Java and how does Java handle it?
What are Java 8 interface default methods and static methods?
What is a Thread? How do you create threads in Java?
What is synchronization and when do you use it?
What is a deadlock? How can you prevent it?
What is ExecutorService and how does it differ from creating threads manually?
What is the volatile keyword in Java?
What are Future and CompletableFuture in Java?
What is a race condition and how do you prevent it?
What is the difference between synchronized and volatile? When should you use each?
What is the happens-before relationship in Java?
What is the difference between CountdownLatch and CyclicBarrier?
What is the difference between JVM, JRE, and JDK?
Explain the Garbage Collection process in Java.
What are Generational Garbage Collection concepts?
What is OutOfMemoryError and how do you prevent it?
What is a memory leak in Java? Can the GC collect it?
What is the difference between stack overflow and heap overflow?
What is a ClassLoader and what are its types?
What is reflection in Java and what are its use cases?
What are Virtual Threads and what Java concurrency problem do they solve?
What are Records in Java? When should you use a Record instead of a regular class?
What are Sealed Classes and why use them with Pattern Matching?
How does GraalVM Native Image improve Spring Boot deployments, and what challenges does it introduce?
Compare G1GC, ZGC, and Shenandoah: which workloads suit each garbage collector?
How do you diagnose memory leaks in production using heap dumps and profiling tools?
What are text blocks (`"""..."""`) in Java and what problem do they solve?
How does pattern matching for switch (Java 21) change the way you write switch?
What is the contract between equals() and hashCode()? Why must you override both?
What is the difference between Comparable and Comparator in Java?
How do final, finally, and finalize differ?
How do wait() and sleep() differ?
Is Java pass-by-value or pass-by-reference?
How does HashMap work internally?
What is the String pool in Java? How do "abc" and new String("abc") differ?
How do you write an immutable class in Java?
How do shallow copy and deep copy differ?
What is JIT compilation and how does the JVM use it?
What is a wrapper class, and how do autoboxing/unboxing work?
What is the difference between `>>` and `>>>`?
What is a covariant return type?
What is the `transient` keyword and how does it affect serialization?
What is a marker interface (Serializable, Cloneable)?
Why can you not create a generic array in Java?
When and why do you use a private constructor?
How does ArrayList grow/resize its capacity internally?
What are the pitfalls of clone()/Cloneable, and what are the alternatives?
Why does `Integer a = 127, b = 127` give `a == b` true, but 128 gives false?
What happens when you remove an element while iterating with for-each? What is the correct way?
What kind of List does `Arrays.asList()` return? Why does calling `add()` throw `UnsupportedOperationException`?
Why is `0.1 + 0.2 != 0.3` in Java? What type should you use for money?
Since Java 8, how does HashMap handle heavily collided buckets?
What happens if you use a mutable object as a HashMap key?
How do you build an LRU cache using only the Java standard library?
What are the two traps of `Collectors.toMap()` when collecting a List into a Map?
Can a Stream be reused? Why does code with `filter`/`map` sometimes not run at all?
How does try-with-resources differ from try/finally? What is a suppressed exception?
What is wrong with concatenating strings using `+` inside a loop? What does the compiler translate `a + b` into?
What is PECS? When do you use `? extends T` versus `? super T`?
What causes `IllegalArgumentException: Comparison method violates its general contract!` during a sort?
Inside `equals()`, should you type-check with `instanceof` or `getClass()`?
How do `orElse` and `orElseGet` differ? What are the common ways people misuse Optional?
What does `String.intern()` do? Where does the string pool live, and how has that changed across Java versions?
What states does a Java thread go through?
How do `Runnable` and `Callable` differ?
How does a daemon thread differ from a user thread?
When does an object become eligible for GC? Does calling `System.gc()` force a collection?
How do `synchronized` and `ReentrantLock` differ? When do you pick each?
How does an instance-level lock differ from a class-level lock (`static synchronized`)?
How do you stop a running thread? Why was `Thread.stop()` removed?
How do you size a thread pool? How do CPU-bound and IO-bound tasks differ?
What parameters does `ThreadPoolExecutor` take? Why do many guidelines advise against `Executors.newFixedThreadPool()`?
How do `thenApply`, `thenCompose`, and `thenCombine` differ? Which thread runs the callback?
What is Metaspace? How does it differ from PermGen, and when do you hit `OutOfMemoryError: Metaspace`?
Why is double-checked locking broken without `volatile`? How does the Java Memory Model come in?
How does `ConcurrentHashMap` achieve thread safety? Why is `if (!map.containsKey(k)) map.put(k, v)` still wrong?
How does `ThreadLocal` leak memory with thread pools? Why must you call `remove()`?
How does CAS work in `AtomicInteger`? What is the ABA problem, and when should you use `LongAdder`?
What is pinning with virtual threads (Java 21)? Why should you not pool virtual threads?