โ Java Interview Questions
Master Java interviews with questions on Core Java, Spring Framework, JVM internals, and design patterns
Core Java concepts, JVM internals, memory management, and fundamentals
15-Minute Java Cheatsheet
Quick reference for last-minute interview preparation
โ Core Concepts
๐๏ธ OOP Principles
๐ฆ Collections Framework
โจ Modern Java (8-21)
๐ Concurrency
๐งน Memory & GC
๐ Spring Framework Essentials
โ ๏ธ Common Interview Gotchas
String: Immutable class. Once created, the value cannot be changed. Any modification creates a new String object. Thread-safe because it's immutable.
StringBuilder: Mutable class. Values can be modified without creating new objects. Not thread-safe, but faster than StringBuffer.
StringBuffer: Mutable class like StringBuilder, but thread-safe (synchronized methods). Slower than StringBuilder due to synchronization overhead.
Java 17 LTS Features:
- Sealed Classes - restrict which classes can extend/implement them
- Pattern Matching for switch (Preview)
- Text Blocks for multi-line strings
- Records - compact syntax for data classes
- Enhanced Pseudo-Random Number Generators
Java 21 LTS Features:
- Virtual Threads (Project Loom) - lightweight threads for high concurrency
- Sequenced Collections - new interfaces for collections with defined order
- Pattern Matching for switch (Standard)
- Record Patterns - deconstruct record values
- String Templates (Preview)
The Singleton pattern ensures a class has only one instance and provides a global point of access to it. It's commonly used for logging, driver objects, caching, and thread pools.
Thread-Safe Implementations:
Java's Garbage Collector automatically manages memory by reclaiming objects that are no longer reachable. The G1 (Garbage-First) GC is the default in Java 9+ and is designed for applications with large heaps.
G1GC Key Concepts:
- Region-based: Heap is divided into equal-sized regions
- Generational: Still uses Young/Old generation concepts
- Concurrent: Most work happens concurrently with application threads
- Predictable pause times: Aims to meet pause time goals
G1GC JVM Flags:
CompletableFuture is a powerful class for asynchronous programming, introduced in Java 8. It allows you to write non-blocking code and chain multiple asynchronous operations.
HashMap: Not thread-safe. Multiple threads modifying it can cause inconsistencies or infinite loops.
ConcurrentHashMap: Thread-safe without synchronizing the entire map. Uses lock striping for better concurrency.
Java Stream API provides a functional approach to process collections. Streams support lazy evaluation, parallel processing, and can significantly reduce boilerplate code.
Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces, and methods. They provide compile-time type safety and eliminate the need for casting.
Java 8 introduced functional interfaces in java.util.function package. These are interfaces with a single abstract method (SAM) used with lambda expressions.
OutOfMemoryError (OOM) occurs when JVM cannot allocate memory. Prevention is better than handling, but there are strategies for both.
WeakHashMap is a Map implementation where keys are held with weak references. When a key has no strong references elsewhere, the entry can be garbage collected.
Understanding time complexity of Java collections is crucial for choosing the right data structure and writing efficient code.
The Executor framework provides a higher-level abstraction for managing threads, decoupling task submission from execution mechanics.
Shutdown hooks are threads that run when JVM is shutting down, allowing cleanup of resources before the application terminates.
These are fundamental OOP concepts that describe relationships between classes. Understanding them is crucial for good software design.
Reflection allows inspection and modification of classes, interfaces, fields, and methods at runtime. It's used by frameworks like Spring, Hibernate, and JUnit for dependency injection, ORM, and testing.
== compares object references (memory addresses) for objects, and values for primitives.equals() compares the actual content/values of objects.
Java exceptions are divided into checked exceptions (must be handled), unchecked exceptions (RuntimeException), and errors. Proper exception handling improves code reliability and maintainability.
๐ง Memory Management
JVM memory is divided into Heap (objects), Stack (method calls), Metaspace (class metadata), and native memory.
๐ฅ Core Java Fundamentals
Object-Oriented Programming (OOP) in Java is built on four fundamental principles: Encapsulation, Inheritance, Polymorphism, and Abstraction.
Java provides robust multithreading support through the Thread class, Runnable interface, and the java.util.concurrent package for high-level concurrency utilities.
Creating a custom Iterator requires implementing the Iterator interface with hasNext() and next() methods, handling the 2D structure with varying column sizes.
SOLID is an acronym for five design principles that make software designs more understandable, flexible, and maintainable.
Interview Tips for Java
- โ Be prepared to discuss JVM internals, memory management, and GC
- โ Know the difference between JDK, JRE, and JVM
- โ Understand collections framework deeply (List, Set, Map implementations)
- โ Practice multithreading, synchronization, and concurrent collections
- โ Be familiar with Spring Framework and dependency injection
- โ Know modern Java features (Streams, Lambdas, Records, Sealed Classes)
- โ Practice coding problems: Arrays, Strings, LinkedList, Trees, Graphs
- โ Know time/space complexity of common algorithms and data structures