Interview Q&A · questions with model answers

Java Developer Interview Questions

Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.

  1. Most asked Explain Java concept: JVM.

    The JVM is the runtime that loads Java bytecode, verifies it, manages memory, and executes code through interpretation and JIT compilation.

  2. Most asked Explain Java concept: hashCode contract.

    Objects that are equal according to equals must return the same hashCode, or hash-based collections become incorrect.

  3. Most asked Explain Java concept: String immutability.

    Java String is immutable: once created, its character content cannot be changed.

  4. Most asked Java Collections: HashMap?

    HashMap stores key-value pairs using hashCode to choose a bucket and equals to find the matching key.

  5. Most asked Java Collections: ConcurrentHashMap?

    ConcurrentHashMap is a thread-safe hash map optimized for concurrent access without synchronizing the entire map for every operation.

  6. Most asked Concurrency in Java: synchronized?

    synchronized provides mutual exclusion and memory visibility using an object's monitor lock.

  7. Most asked Describe the request flow in a Spring MVC (Spring Boot) application.

    The servlet container runs the filter chain (including Spring Security), then the DispatcherServlet finds a handler via HandlerMapping, runs HandlerInterceptor.preHandle, resolves and validates…

  8. Explain Java concept: try-with-resources.

    try-with-resources automatically closes AutoCloseable resources at the end of the block, even when an exception is thrown.

  9. Concurrency in Java: ExecutorService?

    ExecutorService manages task execution with worker threads so application code submits work instead of manually creating threads for every task.

  10. Concurrency in Java: deadlock?

    Deadlock happens when threads wait forever for locks or resources held by each other in a cycle.

  11. Concurrency in Java: race condition?

    A race condition occurs when program correctness depends on unpredictable timing between threads accessing shared state.

  12. Explain Java concept: bytecode.

    Bytecode is the platform-neutral instruction format stored in .class files and executed by the JVM.

  13. Explain Java concept: JIT compilation.

    JIT compilation converts frequently executed bytecode into optimized native code at runtime.

  14. Explain Java concept: final / finally / finalize.

    final restricts reassignment or overriding, finally runs cleanup code after try/catch, and finalize was a deprecated object-finalization hook that should not be used.

  15. Explain Java concept: static.

    static members belong to the class rather than to an individual object instance.

  16. Explain Java concept: composition over inheritance.

    Composition over inheritance means building behavior by holding and delegating to collaborators instead of extending classes by default.

  17. Explain Java concept: access modifiers.

    Java access modifiers control where classes, fields, methods, and constructors are visible.

  18. Explain Java concept: records.

    A record is a compact Java type for immutable data carriers with generated constructor, accessors, equals, hashCode, and toString.

  19. Java Collections: HashSet?

    HashSet stores unique elements using a backing HashMap, so uniqueness depends on equals and hashCode.

  20. Java Collections: TreeMap / TreeSet?

    TreeMap and TreeSet keep entries sorted using natural ordering or a Comparator, with O(log n) core operations.

  21. Java Collections: PriorityQueue?

    PriorityQueue is a heap-based queue that removes the smallest element according to natural ordering or a Comparator.

  22. Java Collections: Generics type erasure?

    Type erasure means Java generic type parameters are checked at compile time but mostly removed from bytecode at runtime.

  23. Concurrency in Java: Thread lifecycle?

    A Java thread moves through states such as NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED.

  24. Concurrency in Java: volatile?

    volatile guarantees visibility and ordering for a variable, but it does not make compound actions atomic.

  25. Concurrency in Java: happens-before?

    happens-before is the Java Memory Model relationship that guarantees one action's effects are visible to another action.

  26. Concurrency in Java: Future / CompletableFuture?

    Future represents a pending asynchronous result; CompletableFuture adds composition, callbacks, manual completion, and async pipelines.

  27. Concurrency in Java: virtual threads (Project Loom)?

    Virtual threads are lightweight JVM-managed threads that make blocking-style code scale to many concurrent tasks.

  28. JVM memory: GC generations?

    Generational garbage collection separates objects by age because most objects die young while some live much longer.

  29. JVM memory: memory leaks in Java?

    A Java memory leak happens when objects are no longer useful but remain reachable, so garbage collection cannot reclaim them.

  30. JVM memory: OutOfMemoryError?

    OutOfMemoryError means the JVM could not allocate required memory in a memory area such as heap, metaspace, direct buffer memory, or native thread stacks.

  31. Explain Java concept: String pool.

    The String pool stores interned string instances so equal literals can share one object.

  32. Explain Java concept: sealed classes.

    Sealed classes and interfaces restrict which types may extend or implement them.

  33. Explain Java concept: var (local type inference).

    var lets the compiler infer the type of a local variable from its initializer.

  34. Java Collections: LinkedHashMap?

    LinkedHashMap is a HashMap variant that maintains a predictable iteration order using a linked list across entries.

  35. Concurrency in Java: ThreadLocal?

    ThreadLocal stores a separate value per thread, usually for per-request context or expensive non-thread-safe helpers.

  36. Concurrency in Java: CountDownLatch / Semaphore?

    CountDownLatch waits until a fixed count reaches zero; Semaphore limits how many permits can be acquired concurrently.

  37. JVM memory: mark and sweep?

    Mark-and-sweep garbage collection marks reachable objects and then reclaims unmarked objects.

  38. JVM memory: escape analysis?

    Escape analysis is a JIT optimization that determines whether an object is visible outside a method or thread and can therefore be optimized.

Was this page helpful?

Report a mistake