Collections Framework
Explain the difference between 'Comparable' and 'Comparator'. When is each appropriate?
What is the time complexity of searching in an ArrayList vs. a LinkedList? Why?
What is the difference between HashMap and Hashtable?
What is the difference between Iterator, ListIterator, and the Iterable interface?
What is the difference between a fail-fast and a fail-safe iterator, and which collections use which and why?
What are Sequenced Collections, and what problem do they solve in the existing Collections hierarchy?
Why does ConcurrentHashMap not allow null keys or values, while HashMap does?
What is the difference between 'fail-fast' and 'fail-safe' (non-blocking) iterators?
When would you use a CopyOnWriteArrayList over a synchronized List?
What problem do Sequenced Collections (introduced in Java 21) solve that the previous Collections hierarchy did not?
How does a HashMap handle collisions internally in Java 8+, including the transition from Linked List to Balanced Tree?
What is the difference between List.of() and Arrays.asList() regarding mutability and null handling?
Explain the 'Fail-Fast' vs. 'Fail-Safe' iterator contract. Which collections use which?
What gap in the Collections Framework did Java 21's SequencedCollection interface fill, and how does it unify the behavior of LinkedHashSet and ArrayList?
How do TreeMap and TreeSet maintain ordering, and what is their time complexity for basic operations?
What is a BlockingQueue, and how does it support the producer-consumer pattern?
What is the difference between an immutable List created with List.of() and an unmodifiable view created with Collections.unmodifiableList()?
How does a HashMap handle collisions in Java 8+? At what point does a linked list convert into a Red-Black Tree?
How does a HashMap work internally in Java 8? Explain the transition from LinkedList to TreeNode.
In what specific scenarios would a LinkedList actually outperform an ArrayList, considering modern CPU cache behavior?
How does ConcurrentHashMap achieve thread safety, and how did its locking strategy change from segment locking to bucket-level CAS in Java 8?
Exceptions & Resource Handling
Explain the 'Try-with-Resources' statement. What interface must a class implement to be used within it?
What is the difference between Checked and Unchecked Exceptions? Why is there a trend toward Unchecked?
How does 'try-with-resources' work internally? What interface must the resource implement?
What is the philosophical difference between a Checked and an Unchecked exception, and when should you create a custom Checked exception?
Why does Java distinguish between Checked and Unchecked exceptions? What is the modern architectural trend regarding this distinction?
What is the conceptual difference between a checked and an unchecked exception, and why is there a trend in modern Java libraries to favor RuntimeException?
What is the difference between ClassNotFoundException and NoClassDefFoundError?
Oop Classes & Inheritance
What are the key methods defined on the Object class, and why are toString(), equals(), and hashCode() so important?
What is the difference between an Interface and an Abstract Class in Java 17+ (considering default/private methods)?
What is the 'Diamond Problem' in multiple inheritance, and how does Java solve it with Interfaces?
With the introduction of default and static methods in interfaces, is there still a reason to use an abstract class?
Explain the difference between 'Composition' and 'Inheritance.' Why is the industry moving toward 'Composition over Inheritance'?
Why is 'composition over inheritance' a recommended practice in Java, and what are the risks of deep inheritance hierarchies in large-scale systems?
What are the different types of nested classes in Java (static nested, inner, local, anonymous) and how do they differ?
How are enums implemented in Java, and why is an enum considered the best way to implement a singleton?
Strings
What is the 'String Pool' and how does it help with memory management?
What is the difference between String, StringBuilder, and StringBuffer, and when would you use each?
Why are Strings immutable in Java? Mention security and performance reasons.
Explain the 'String Templates' feature in Java 21. How does it improve security compared to simple string concatenation?
Why are Strings immutable in Java? Explain the security and performance implications of the String Pool.
Why is the String Pool critical for Java's memory management, and why was the decision made to keep Strings immutable?
Equality Immutability & Object Contracts
Explain the contract between 'equals()' and 'hashCode()'. What happens if you break it?
What is the contract between hashCode() and equals(), and what happens if you override one but not the other?
Why is it critical to override hashCode() whenever you override equals()? What happens in a HashSet if you don't?
How do you make a class truly immutable in Java, and what are the benefits of immutability in a multithreaded environment?
If you override equals(), why must you also override hashCode()? What happens if two unequal objects have the same hash code in a HashSet?
What is the difference between a shallow copy and a deep copy, and what are the pitfalls of the Cloneable interface and Object.clone()?
Language Fundamentals & Types
Is Java 'Pass-by-Value' or 'Pass-by-Reference'? Explain with an example of an Object.
Is Java pass-by-value or pass-by-reference? Explain what happens when you pass an object reference to a method.
What is the difference between method overloading and method overriding, and how does static (compile-time) versus dynamic (runtime) binding apply to each?
What is the difference between '==' and the 'equals()' method when comparing objects in Java?
What does the 'static' keyword mean, and how do static initializer blocks and static fields get initialized when a class is loaded?
What are the four access modifiers in Java (public, protected, default, private) and what visibility does each provide?
What are varargs, and what are the rules and pitfalls when using them?
What is autoboxing and unboxing, and why can comparing two Integer objects with '==' give surprising results due to the Integer cache?
What is the difference between BigDecimal and double, and when should you avoid floating-point types for calculations?
Functional Programming & Streams
What is a Functional Interface? Can you name three built-in ones in the JDK?
Explain the difference between 'Intermediate' and 'Terminal' operations in the Stream API.
What is the purpose of 'Optional'? Does it prevent NullPointerExceptions entirely?
What is a 'Method Reference' and how does it relate to Lambda expressions?
What makes an interface a 'Functional Interface', and can you have multiple methods in it if they are default or static?
What makes an interface a 'Functional Interface'? Can a functional interface have multiple methods such as default or static methods?
Are Java Streams 'lazy'? What does that mean for performance?
Explain the difference between intermediate and terminal operations in the Stream API, and why are Streams considered 'lazy'?
What is the primary purpose of the Optional class, and why is it generally discouraged to use Optional as a method parameter or a class field?
What are the tradeoffs of using Java Streams? In what specific scenarios would a traditional for loop be more performant?
Explain the 'lazy evaluation' of Java Streams. Why won't an intermediate operation like filter() execute until a terminal operation is called?
Why is it considered a 'code smell' to use Optional.get() without isPresent()? When should you use Optional as a return type versus a method parameter?
How do Collectors work in the Stream API, and how would you use groupingBy or partitioningBy?
What is the difference between map() and flatMap() in the Stream API?
Why must local variables captured by a lambda or anonymous class be final or effectively final?
What are the risks of using parallelStream(), and how does it interact with the common ForkJoinPool?
When is it actually detrimental to use .parallelStream(), and how does the underlying ForkJoinPool affect the performance of other parts of the application?
Records Sealed Classes & Pattern Matching
How do Java Records differ from standard POJOs, and why are they considered 'transparent carriers' of data?
Explain Pattern Matching for switch. How does it improve upon the traditional switch statement?
What is the purpose of 'Unnamed Patterns and Variables' in Java 21?
What is the purpose of a sealed class, and how does it improve the security or maintainability of a class hierarchy?
What are Java Records, and why are they considered more than just 'syntactic sugar' for POJOs?
Explain how Pattern Matching for switch and instanceof changes the way we handle complex conditional logic.
Why are Records considered 'transparent carriers' of data, and how does this differ from a standard class?
Explain 'Pattern Matching for switch.' How does it change the way we handle complex object hierarchies?
Why should you use a Record instead of a standard class for a DTO, and what are the implications for inheritance and immutability?
Explain how 'Pattern Matching for switch' changes the way we handle complex object hierarchies compared to the instanceof check.
What is the 'Closed World Assumption' introduced by Sealed Classes, and why is it useful for the compiler?
What is the 'Algebraic Data Type' (ADT) pattern in Java, and how do Sealed Classes and Records work together to implement it?
What is the architectural benefit of using sealed classes and interfaces, and how do they improve the reliability of switch pattern matching?
Concurrency Fundamentals & Memory Model
Explain the different states of a Java thread and what causes a thread to move from Runnable to Waiting.
What does the 'volatile' keyword actually guarantee regarding visibility and instruction reordering?
What is the difference between 'synchronized' and 'ReentrantLock'? When would you prefer the latter?
How does a 'Deadlock' occur, and what are the four necessary conditions for it?
What is the difference between the volatile keyword and Atomic variables, and why doesn't volatile guarantee atomicity?
When would you use ReentrantLock instead of a synchronized block, and what are the trade-offs in terms of fairness and flexibility?
How do wait(), notify(), and notifyAll() work, and why must they be called from within a synchronized block?
What is a daemon thread, and how does it differ from a user thread in terms of JVM shutdown?
Explain the Java Memory Model (JMM) and the 'happens-before' relationship.
What is the 'happens-before' relationship in the Java Memory Model, and why is it critical for thread safety?
Explain the Java Memory Model (JMM). Why is it possible for a thread to see a partially initialized object?
What is 'False Sharing' in a multithreaded Java application, and how does the @Contended annotation address it?
Explain the 'happens-before' relationship. Why does volatile solve visibility issues but not atomicity issues?
When would you use StampedLock over ReentrantReadWriteLock, and what are the tradeoffs of optimistic reading?
How do atomic classes like AtomicInteger work, and what is Compare-And-Swap (CAS)?
Memory Stack Heap & Leaks
What is the difference between the Stack and the Heap? What specifically is stored in each?
Explain the 'Metaspace' in Java 8+. How does it differ from the old PermGen?
How can a memory leak occur in a Java application despite having an automatic Garbage Collector?
How would you identify a memory leak in a Java application if the Heap usage is constantly growing?
What is 'Escape Analysis' and how does it allow the JVM to perform scalar replacement?
Why can an OutOfMemoryError occur even if the heap has plenty of free space?
What is the 'Double Brace Initialization' anti-pattern, and why does it cause memory leaks?
What is Metaspace, and how does its management of class metadata differ from the old PermGen model? Why does it cause OutOfMemoryError differently?
Walk me through what happens in memory when you call new ArrayList(). Where is the object header stored, and how does the JVM track its age for GC?
Why are primitives stored on the stack while objects are on the heap? How does 'Escape Analysis' allow the JVM to sometimes allocate objects on the stack?
Garbage Collection
What is the 'Stop-the-World' phase in GC, and how do modern collectors try to minimize it?
Explain the concept of Generational Garbage Collection. Why is the heap divided into Young and Old generations?
What is the difference between Minor, Major, and Full Garbage Collection, and what triggers a 'Stop-the-World' event?
Explain the difference between the G1 Garbage Collector and the ZGC (Z Garbage Collector).
What is the difference between the G1 and ZGC garbage collectors, and when would you prefer one over the other for a low-latency application?
How does the ZGC (Z Garbage Collector) achieve sub-millisecond pause times even with terabyte-sized heaps?
What are the primary differences between the G1 Garbage Collector and the ZGC, and in what scenario would you choose ZGC over G1?
Generics
What is Type Erasure in Java Generics, and what limitations does it impose on developers at runtime?
What is 'Type Erasure' in Generics? Why can't you use primitives as type parameters?
Explain the PECS (Producer Extends, Consumer Super) principle. When would you use <? extends T> versus <? super T>?
What is 'Type Erasure' in Java Generics? What are its limitations?
Explain the PECS (Producer Extends, Consumer Super) rule in Generics.
What is Type Erasure in Java Generics? Why can't you use primitives as type parameters, and how does 'Project Valhalla' aim to solve this?
Executors Futures & Synchronizers
Explain the difference between 'submit()' and 'execute()' in an ExecutorService.
What is the difference between Future and CompletableFuture, and what limitations of Future does CompletableFuture address?
How should you properly shut down an ExecutorService, and what is the difference between shutdown() and shutdownNow()?
How does 'CompletableFuture' handle exception propagation in an asynchronous pipeline?
What is the 'Fork/Join' framework, and how does 'work-stealing' help in load balancing?
What is the difference between CountDownLatch, CyclicBarrier, and Semaphore, and when would you use each?
Virtual Threads & Structured Concurrency
What are Virtual Threads (Project Loom) and how do they differ from traditional Platform Threads?
What are Virtual Threads, and how do they differ from traditional Platform Threads in terms of resource management?
What is the fundamental difference between a Platform Thread and a Virtual Thread?
When would you still prefer a fixed thread pool over virtual threads?
What is the fundamental difference between a platform thread and a virtual thread in terms of how they are mapped to the OS?
Explain the concept of 'Thread Pinning' in the context of Virtual Threads. When does it happen?
Why can't you use 'synchronized' blocks effectively with Virtual Threads in certain scenarios?
When would you choose Virtual Threads over a reactive programming model (like CompletableFuture or Flux)?
Explain the concept of 'Carrier Thread Pinning.' In what scenarios does a virtual thread fail to unmount from its carrier?
Why would you choose Scoped Values over ThreadLocal in a system using millions of virtual threads?
What is 'Structured Concurrency' and how does it prevent thread leaks compared to the traditional ExecutorService?
Explain the concept of 'mounting' and 'unmounting' a virtual thread. What happens to the carrier thread when a virtual thread performs a blocking I/O operation?
What is 'thread pinning' in the context of Virtual Threads, and why does using the synchronized keyword potentially cause performance issues in a virtual thread environment?
How does Structured Concurrency improve the observability and reliability of multi-threaded tasks compared to using ExecutorService?
Why were Scoped Values introduced in Java 21, and what are the memory and scalability tradeoffs compared to using ThreadLocal?
Jvm Platform & Execution
What is the difference between the JDK, the JRE, and the JVM?
Explain the role of the Bootstrap, Extension, and Application ClassLoaders.
How does the JIT (Just-In-Time) compiler decide when to compile a method into native code?
Explain the role of the JIT compiler. How does it optimize code at runtime compared to the initial bytecode interpretation?
What is 'Class Data Sharing' (CDS) and how does it improve the startup time of Java microservices in containers?
How does the JIT compiler decide when to move code from C1 to C2 (Tiered Compilation)?
Explain how the JIT compiler decides to move code from Level 1 to Level 4 (C2) compilation. What is 'code cache' exhaustion?
What is the Java Platform Module System (JPMS) introduced in Java 9, and what problems does it solve?
Reflection Serialization & Annotations
What is 'Reflection' and what are the performance and security risks of using it?
What is 'Serialization' and what is the role of 'serialVersionUID'?
What are 'Annotations' and how are they processed at runtime vs. compile time?
Why is standard Java Serialization often considered a security risk, and what are the modern alternatives for object persistence?
References & Thread Locals
Explain the 'final', 'finally', and 'finalize' keywords. Why is 'finalize' deprecated?
Explain the 'Strong vs. Weak vs. Soft' reference types. When would you use a WeakHashMap?
What is a 'ThreadLocal' variable, and what is the risk of using it in a thread-pooled environment?
Explain the difference between 'Strong', 'Soft', 'Weak', and 'Phantom' references.