Interview Prep

Java Interview Questions

Core Java, collections, concurrency, JVM, streams, records, and practical senior-level Java interview prep.

Showing 20 of 20 questions

What is the difference between HashMap and ConcurrentHashMap?

HashMap is not thread-safe. ConcurrentHashMap is designed for safe concurrent reads and updates without locking the entire map.

CollectionsConcurrencySenior Java

What is the difference between volatile and synchronized?

volatile gives visibility guarantees for a variable. synchronized gives visibility plus mutual exclusion, meaning only one thread can enter the critical section at a time.

ConcurrencyJava Memory ModelSenior Java

What is the difference between Comparable and Comparator?

Comparable defines an object's natural ordering (comparison with equals method) inside the class. Comparator defines external sorting strategies, which lets the same objects be sorted in multiple ways.

CollectionsSortingCore Java

What is the difference between ReentrantLock and synchronized?

synchronized is Java's built-in locking mechanism. ReentrantLock gives similar mutual exclusion but adds more control, such as tryLock, timed waits, interruptible locking, fairness, and explicit unlock behavior.

ConcurrencyLocksSenior Java

What is the difference between LongAdder and AtomicInteger?

AtomicInteger updates one shared value atomically. LongAdder spreads updates across multiple internal cells to reduce contention, making it better for high-throughput counters.

ConcurrencyAtomic ClassesPerformance

Thread Pools Explained Visually

A thread pool is a group of reusable worker threads that process tasks from a queue, helping avoid the cost and chaos of creating a new thread for every request.

ConcurrencyThread PoolsExecutorServiceSenior Java

What are the main JVM memory areas?

The JVM organizes runtime memory into areas like the heap, stack, method area, PC register, and native method stack. The key interview idea is knowing what is shared across threads, what is private to each thread, and where objects, method calls, and class metadata live.

JVMMemoryPerformanceSenior Java

What is the difference between wait/notify and Condition?

wait/notify is tied to synchronized and intrinsic object monitors. Condition is tied to explicit locks like ReentrantLock and allows multiple named wait queues for clearer thread coordination.

ConcurrencyLocksThread Coordination

What is the difference between Callable and Runnable?

Runnable represents work that runs without returning a result. Callable represents work that can return a result and throw checked exceptions, usually accessed through a Future.

ConcurrencyExecutorServiceFuture

How does HashMap work internally?

HashMap stores key-value pairs in buckets. It uses the key's hash code to find a bucket, handles collisions inside that bucket, and resizes when the map becomes too full.

CollectionsHashingCore Java

What is ThreadLocal in Java?

ThreadLocal lets each thread store its own isolated value, even when the same ThreadLocal variable is shared. It is useful for request context, user context, tracing IDs, and avoiding parameter-passing clutter, but it must be cleaned up carefully in thread pools.

ConcurrencyThreadLocalSenior Java

What are ExecutorService and thread pools in Java?

ExecutorService separates task submission from thread management. A thread pool reuses a limited number of worker threads instead of creating a new thread for every task.

ConcurrencyExecutorServiceThread PoolsBackend

Immutable Collections in Java

Understand List.of(), Set.of(), Map.of(), unmodifiable wrappers, shallow immutability, and common interview follow-ups.

JavaCollectionsImmutabilityListMap

HashSet vs TreeSet

HashSet is usually used when you only need fast uniqueness. TreeSet is used when you need uniqueness plus sorted order.

CollectionsSetHashSetTreeSet

Future vs CompletableFuture

Future represents a result that may be available later. CompletableFuture adds async composition: chaining, combining, transforming, and handling errors without immediately blocking.

ConcurrencyFutureCompletableFutureAsyncJava

Common Optional Mistakes

Learn the most common mistakes developers make with Optional and how to use it effectively in modern Java.

OptionalJavaModern JavaAPI Design

Stream API pitfalls

Streams are powerful for filtering, mapping, grouping, and aggregating data, but they can also make code harder to read when misused.

StreamsModern JavaJavaFunctional Programming

Structured Concurrency in Java

Structured concurrency treats related concurrent tasks as one unit of work, making cancellation, failure handling, and task ownership easier to reason about.

ConcurrencyStructured ConcurrencyModern JavaJava 21Virtual Threads

Virtual Threads in Java

Virtual threads are lightweight Java threads finalized in Java 21. They make thread-per-task style programming practical for many high-concurrency blocking I/O workloads.

ConcurrencyVirtual ThreadsJava 21Modern JavaProject Loom

How do you read a Java thread dump?

A thread dump is a snapshot of every thread in a JVM. Learn how to identify deadlocks, blocked threads, lock contention, and stuck requests.

JVMTroubleshootingConcurrencySenior Java