Java Backend Interview Questions

The Java backend questions that come up most in Spring Boot and enterprise Java interviews, with concise, correct answers. Work through them, then run a live mock to practice explaining them out loud.

Practice these live with an AI interviewer
Get asked questions one at a time and a scored feedback report — free.

12 common Java Backend questions

What is the difference between the JDK, JRE, and JVM?

The JVM is the runtime engine that executes bytecode. The JRE bundles the JVM with the standard class libraries needed to run Java programs. The JDK is the full development kit — it includes the JRE plus the compiler (javac), debugger, and other tools. You develop with the JDK and ship/run with the JRE.

Explain Java's memory model — heap vs. stack.

The stack holds method frames with local variables and references, allocated and freed per-call in LIFO order. The heap holds all object instances and is shared across threads — managed by the garbage collector. Large long-lived objects go on the heap; primitives and references in an active frame stay on the stack.

What is the difference between checked and unchecked exceptions?

Checked exceptions (extend Exception but not RuntimeException) must be declared in the method signature or caught — the compiler enforces this. Unchecked exceptions (RuntimeException and its subclasses, plus Errors) don't require declaration. Use checked for recoverable conditions and unchecked for programming errors.

How does Spring Boot auto-configuration work?

Spring Boot scans the classpath for libraries and, based on what it finds (e.g. spring-data-jpa + a datasource), uses @Conditional annotations to register beans automatically. The spring.factories / AutoConfiguration.imports file lists candidate configurations that are evaluated at startup — you can override any bean or property to customise or disable them.

What is the Spring Bean lifecycle?

Spring instantiates the bean, injects dependencies, then calls @PostConstruct / afterPropertiesSet(). The bean is then in use. On shutdown it calls @PreDestroy / destroy(). The container manages scope — singleton beans live for the application lifetime, request/session-scoped beans are shorter.

Explain @Transactional in Spring — what does it do and what are common pitfalls?

@Transactional wraps the method in a database transaction that commits on success or rolls back on a RuntimeException by default. Common pitfalls: calling a @Transactional method from within the same bean bypasses the proxy (no transaction); checked exceptions don't trigger rollback unless you set rollbackFor; and catching exceptions inside the method silently swallows the rollback.

What is N+1 query problem in JPA/Hibernate and how do you fix it?

N+1 occurs when loading a list of N entities triggers N extra lazy queries for an association. Fix it with JOIN FETCH in JPQL, @EntityGraph, or a @NamedEntityGraph to eagerly load associations in a single query, or use batch fetching. Avoid FetchType.EAGER globally — it causes over-fetching.

How does Java handle concurrency — threads, synchronized, and modern alternatives?

Threads share heap memory; synchronized methods/blocks acquire an intrinsic lock to prevent concurrent access. Modern alternatives: ReentrantLock for explicit locking, volatile for visibility without mutual exclusion, java.util.concurrent classes (ConcurrentHashMap, atomic types, locks, semaphores), and CompletableFuture for async composition. Prefer higher-level abstractions to raw synchronized.

What is the difference between ArrayList and LinkedList?

ArrayList is backed by an array — O(1) random access, O(n) insertions in the middle. LinkedList is a doubly-linked list — O(1) insertions/deletions at head/tail, O(n) random access. ArrayList is almost always the better default due to cache locality; use LinkedList when you insert/remove frequently at both ends.

Explain REST API best practices in a Spring Boot context.

Use meaningful URIs (nouns, plural: /users/{id}), correct HTTP verbs (GET/POST/PUT/PATCH/DELETE), and standard status codes (200, 201, 204, 400, 404, 409, 500). Return consistent error bodies. Version via URI (/v1/) or Accept header. Secure with Spring Security (JWT or OAuth2). Validate input with @Valid/@Validated and handle exceptions globally with @ControllerAdvice.

What is a microservice and what challenges does it introduce?

A microservice is a small, independently deployable service with a single bounded context. Benefits: independent scaling and deployment. Challenges: distributed system problems — network latency, partial failures, distributed transactions (use sagas/outbox pattern), data consistency, service discovery, and observability (distributed tracing with Sleuth/Zipkin, centralised logging, metrics).

How do you write unit tests for a Spring Boot service?

Use JUnit 5 and Mockito — annotate with @ExtendWith(MockitoExtension.class), mock dependencies with @Mock, inject them with @InjectMocks, and assert with AssertJ. For Spring slice tests use @WebMvcTest (controller layer with MockMvc) or @DataJpaTest (repository layer with an in-memory DB). Keep unit tests fast by avoiding the full context (@SpringBootTest is integration, not unit).

Ready to practice out loud?

Reading answers is one thing — saying them under pressure is another. Run a free AI mock interview and get scored feedback.

Start a mock interview

More interview questions