Backend Developer (Java) Interview Questions: Complete Guide

Milad Bonakdar
Author
Master Java backend development with essential interview questions covering Spring Boot, Microservices, JPA, and system design. Perfect preparation for Java developer interviews.
Introduction
Java remains a powerhouse in the enterprise backend world, especially with the modern Spring Boot ecosystem. Its robustness, scalability, and vast ecosystem make it a top choice for building microservices and cloud-native applications.
This guide covers essential interview questions for Backend Developers specializing in Java and Spring Boot. We explore core framework concepts, microservices architecture, database interactions with JPA/Hibernate, and best practices to help you prepare for your next interview.
Spring Boot Core Concepts
1. What is Spring Boot and how does it differ from the traditional Spring Framework?
Answer:
- Spring Framework: A comprehensive framework for enterprise Java development. It requires significant manual configuration (XML or Java-based) to set up.
- Spring Boot: An extension of the Spring Framework that simplifies the setup and development of new Spring applications.
- Auto-Configuration: Automatically configures beans based on classpath dependencies.
- Embedded Servers: Includes Tomcat, Jetty, or Undertow, so you don't need to deploy WAR files.
- Starters: Curated dependencies to simplify build configuration.
- Opinionated Defaults: "Convention over configuration" approach.
Rarity: Common Difficulty: Easy
2. Explain the @SpringBootApplication annotation.
Answer: It is a convenience annotation that combines three other annotations:
@Configuration: Marks the class as a source of bean definitions.@EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.@ComponentScan: Tells Spring to look for other components, configurations, and services in the current package and sub-packages.
Rarity: Common Difficulty: Easy
3. What is Dependency Injection (DI) and Inversion of Control (IoC)?
Answer:
- IoC: A principle where the control of object creation and management is transferred from the programmer to a container (Spring IoC Container).
- DI: A design pattern used to implement IoC. Instead of an object creating its dependencies, they are "injected" into it (via Constructor, Setter, or Field).
- Benefit: Decoupling, easier testing (mocking dependencies), and better maintainability.
Rarity: Very Common Difficulty: Medium
4. What are Spring Boot Starters? Give examples.
Answer: Starters are a set of convenient dependency descriptors that you can include in your application. They contain all the dependencies you need to get a project up and running quickly with a consistent, supported set of managed transitive dependencies.
- Examples:
spring-boot-starter-web: For building web applications (includes Tomcat and Spring MVC).spring-boot-starter-data-jpa: For using Spring Data JPA with Hibernate.spring-boot-starter-test: For testing (includes JUnit, Mockito).spring-boot-starter-security: For Spring Security.
Rarity: Common Difficulty: Easy
5. What is Spring Actuator?
Answer: Spring Boot Actuator provides production-ready features to help you monitor and manage your application.
- Endpoints: Exposes endpoints like
/health(app status),/metrics(memory, CPU usage),/info(app info),/env(environment properties). - Usage: Critical for operations teams to check the health of microservices in production.
Rarity: Common Difficulty: Medium
Microservices & Architecture
6. What are the benefits of Microservices over Monolithic Architecture?
Answer:
- Scalability: Individual services can be scaled independently based on demand.
- Technology Agnostic: Different services can use different technologies (Java, Go, Python) best suited for the task.
- Fault Isolation: A failure in one service doesn't necessarily bring down the entire system.
- Independent Deployment: Teams can deploy services independently, enabling faster release cycles (CI/CD).
Rarity: Common Difficulty: Medium
7. What is an API Gateway? Why use it?
Answer: An API Gateway is a server that acts as a single entry point into the system. It takes all API calls from clients, then routes them to the appropriate microservice.
- Functions: Request routing, composition, and protocol translation.
- Cross-cutting concerns: Authentication, SSL termination, Rate Limiting, Caching, Logging.
- Examples: Spring Cloud Gateway, Netflix Zuul, Kong.
Rarity: Very Common Difficulty: Medium
8. Explain Service Discovery (Eureka).
Answer: In a microservices environment, service instances have dynamic IP addresses. Service Discovery is a mechanism for services to find each other.
- Eureka Server: Acts as a service registry.
- Eureka Client: Microservices register themselves with the Eureka Server on startup and send heartbeats.
- Discovery: When Service A needs to call Service B, it asks Eureka for the address of Service B.
Rarity: Common Difficulty: Medium
9. What is the Circuit Breaker Pattern?
Answer: The Circuit Breaker pattern prevents an application from repeatedly trying to execute an operation that's likely to fail (e.g., calling a down service).
- States:
- Closed: Requests flow normally.
- Open: Requests are blocked immediately (fail fast) to allow the failing service to recover.
- Half-Open: A limited number of requests are allowed through to test if the service has recovered.
- Tools: Resilience4j (recommended), Hystrix (deprecated).
Rarity: Very Common Difficulty: Hard
Database & JPA
10. What is JPA and Hibernate?
Answer:
- JPA (Java Persistence API): A specification for accessing, persisting, and managing data between Java objects and a relational database. It is just an interface.
- Hibernate: The most popular implementation of the JPA specification. It is an ORM (Object-Relational Mapping) tool.
Rarity: Common Difficulty: Easy
11. Explain the N+1 Select Problem in Hibernate.
Answer: The N+1 problem happens when the data access framework executes N additional SQL statements to fetch the same data that could have been retrieved when executing the primary SQL query.
- Scenario: Fetching a list of
Authors (1 query). Iterating and accessingauthor.getBooks()(N queries, one for each author). - Solution: Use Join Fetch in JPQL (
SELECT a FROM Author a JOIN FETCH a.books) or Entity Graphs to load related entities eagerly in a single query.
Rarity: Very Common Difficulty: Medium
12. What is the difference between @Entity, @Table, and @Column?
Answer:
@Entity: Specifies that the class is an entity and is mapped to a database table.@Table: Optional. Specifies the name of the database table to be used for mapping. If omitted, the class name is used.@Column: Optional. Specifies the details of the column to which a field or property will be mapped. If omitted, the field name is used.
Rarity: Common Difficulty: Easy
Security & Testing
13. How do you secure a Spring Boot application?
Answer: Using Spring Security.
- Authentication: Verifying who the user is (e.g., Basic Auth, Form Login, OAuth2/OIDC).
- Authorization: Verifying what the user is allowed to do (e.g., Role-based access control using
@PreAuthorize). - JWT: For stateless microservices, JSON Web Tokens are commonly used to pass identity between services.
Rarity: Common Difficulty: Medium
14. What is the difference between @Mock and @InjectMocks in Mockito?
Answer:
@Mock: Creates a mock object of the class/interface. It does not have any real behavior; you define its behavior usingwhen(...).thenReturn(...).@InjectMocks: Creates an instance of the class and injects the mocks that are created with the@Mock(or@Spy) annotations into this instance. Used for the class under test.
Rarity: Common Difficulty: Medium
15. How do you handle transactions in Spring Boot?
Answer:
Using the @Transactional annotation.
- Mechanism: Spring creates a proxy around the class/method. It starts a transaction before the method execution and commits it after the method returns. If a
RuntimeExceptionis thrown, it rolls back the transaction. - Propagation: You can configure how the transaction relates to existing transactions (e.g.,
REQUIRED,REQUIRES_NEW).
Rarity: Common Difficulty: Medium
Advanced Topics
16. What is Spring Cloud Config?
Answer: Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system.
- Config Server: Central place to manage external properties for applications across all environments. It can be backed by Git, SVN, or Vault.
- Benefit: You can change configuration without redeploying the application (using
@RefreshScope).
Rarity: Uncommon Difficulty: Medium
17. Explain the difference between Singleton and Prototype scope in Spring Beans.
Answer:
- Singleton (Default): Only one instance of the bean is created per Spring IoC container. It is cached and reused.
- Prototype: A new instance is created every time the bean is requested.
- Others: Request, Session (web-aware scopes).
Rarity: Common Difficulty: Easy
18. What is the difference between Checked and Unchecked exceptions in Java?
Answer:
- Checked Exceptions: Inherit from
Exception(but notRuntimeException). The compiler forces you to handle them (try-catch) or declare them (throws). Example:IOException,SQLException. Represent recoverable conditions. - Unchecked Exceptions: Inherit from
RuntimeException. The compiler does not force you to handle them. Example:NullPointerException,IllegalArgumentException. Represent programming errors. Spring's@Transactionalrolls back only on unchecked exceptions by default.
Rarity: Common Difficulty: Easy
19. How does Garbage Collection work in Java?
Answer: Garbage Collection (GC) is the process of automatically reclaiming memory occupied by objects that are no longer reachable.
- Generational Hypothesis: Most objects die young.
- Heap Structure: Young Generation (Eden, Survivor spaces) and Old Generation.
- Minor GC: Cleans Young Gen. Fast.
- Major/Full GC: Cleans Old Gen. Slower, can cause "Stop-the-world" pauses.
- Algorithms: G1GC (default in modern Java), ZGC, Shenandoah (low latency).
Rarity: Common Difficulty: Hard
20. What are Java Streams?
Answer: Introduced in Java 8, Streams provide a functional approach to processing collections of objects.
- Features: Declarative, Pipelining, Internal Iteration.
- Operations:
- Intermediate:
filter,map,sorted(lazy). - Terminal:
collect,forEach,reduce(trigger processing).
- Intermediate:
- Parallel Streams: Can process data in parallel using multiple threads (
.parallelStream()).
Rarity: Common Difficulty: Medium




