Top Java Advanced Interview Questions and Answers - Textnotes

Top Java Advanced Interview Questions and Answers


A curated list of advanced Java interview questions, covering JVM internals, concurrency, memory management, Spring, JPA, performance tuning, and design patterns — ideal for 4+ years experienced developers.

1. What is the Java Memory Model (JMM)?

JMM defines how threads interact through memory and ensures visibility, ordering, and atomicity of shared data.


2. What are the different memory areas in JVM?

  1. Heap
  2. Stack
  3. Method Area
  4. PC Register
  5. Native Method Stack


3. What is the difference between Heap and Stack memory?

  1. Heap: Stores objects, shared across threads
  2. Stack: Stores method frames and local variables, thread-specific


4. Explain Garbage Collection (GC) in Java.

GC automatically removes unused objects from memory using:

  1. Mark & Sweep
  2. Generational GC
  3. G1 GC


5. What is the difference between G1 GC and CMS GC?

  1. G1 GC: Region-based, better for large heaps
  2. CMS GC: Low pause time but deprecated in latest JVMs


6. What is a ClassLoader?

Loads classes into JVM.

Types: Bootstrap → Extension → App ClassLoader


7. What is Reflection in Java?

Allows runtime inspection/modification of classes, fields, and methods.


8. What is the difference between synchronized and Lock interface?

  1. synchronized: Automatic lock release, simple
  2. Lock: Manual unlock(), better control, tryLock()


9. What is the Fork/Join Framework?

Used for parallel processing by splitting tasks into smaller subtasks.


10. What are Callable and Future?

  1. Callable: Returns a value and can throw exceptions
  2. Future: Holds result of asynchronous computation


11. What is CompletableFuture?

An asynchronous computation API for chaining tasks without blocking.


12. What is JVM JIT compiler?

Just-In-Time compiler converts frequently used bytecode into native machine code for better performance.


13. What is Bytecode?

Intermediate code generated by Java compiler that runs on JVM.


14. What is a SoftReference, WeakReference, and PhantomReference?

Used for advanced memory management.

  1. Soft: GC removes only when memory is low
  2. Weak: Removed immediately when no strong references exist
  3. Phantom: Used for cleanup before object is removed


15. What is ThreadLocal?

Provides thread-specific variables so each thread has its own copy.


16. What is a Memory Leak in Java?

Occurs when objects remain referenced and GC cannot reclaim memory.


17. What is SPI (Service Provider Interface)?

A mechanism that allows framework-level extensibility using service providers.


18. What is the difference between REST and SOAP?

  1. REST: Lightweight, JSON/HTTP-based
  2. SOAP: Strict protocol, XML-based, more secure


19. What are Microservices?

Small, independent services communicating via APIs.


20. What is Dependency Injection (DI)?

A technique where objects are provided to a class rather than creating them inside.


21. What is IoC (Inversion of Control)?

Framework controls object creation instead of developer.


22. What is AOP (Aspect-Oriented Programming)?

Helps separate cross-cutting concerns like logging, security, transaction management.


23. What is Hibernate/JPA?

ORM framework for mapping Java objects to database tables.


24. What is Lazy Loading and Eager Loading?

  1. Lazy: Loads only when accessed
  2. Eager: Loads immediately


25. What is the N+1 Select Problem in Hibernate?

Occurs when one query triggers multiple additional queries due to lazy loading.


26. What is Connection Pooling?

Reusing database connections to improve performance (HikariCP is common).


27. What is Double Checked Locking?

A technique to safely initialize a singleton in a multi-threaded environment.


28. What is the difference between HashMap and ConcurrentHashMap?

  1. HashMap → Not thread-safe
  2. ConcurrentHashMap → Thread-safe using segmented locking


29. What are Design Patterns used in Java?

Common patterns:

  1. Singleton
  2. Factory
  3. Builder
  4. Observer
  5. Strategy
  6. Adapter


30. What is Distributed Caching?

Storing data across multiple servers (Redis, Hazelcast) for speed and scalability.