Interview Series: Top Java + Spring Boot Interview Questions & Answers

Here’s a curated interview series on Java and Spring Boot. The following list includes the top interview questions along with comprehensive answers that can help candidates prepare for technical interviews.


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

Answer:

  • JDK (Java Development Kit): It is a software development kit used to develop Java applications. It includes the JRE and development tools such as compilers and debuggers.
  • JRE (Java Runtime Environment): It is a runtime environment that allows you to run Java applications. It includes the JVM and libraries necessary to run applications but doesn’t provide the tools to develop them.
  • JVM (Java Virtual Machine): It is the engine that runs Java bytecode. It is platform-independent and helps Java achieve its “write once, run anywhere” capability.

2. What is the concept of Object-Oriented Programming in Java?

Answer: Object-Oriented Programming (OOP) in Java is a programming paradigm based on the concept of “objects” and “classes”. The key principles of OOP are:

  1. Encapsulation: Bundling the data (attributes) and methods (functions) that operate on the data into a single unit known as a class. It helps in data hiding and access control.
  2. Abstraction: Hiding the complex implementation details and showing only the essential features. This is achieved using abstract classes and interfaces.
  3. Inheritance: Mechanism to define a new class based on an existing class, inheriting its properties and behaviors, thus promoting code reuse.
  4. Polymorphism: The ability of an object to take on multiple forms. It includes method overloading (compile-time polymorphism) and method overriding (runtime polymorphism).

3. What are the different types of collections in Java?

Answer: Java collections can be broadly classified into three types:

  1. List: An ordered collection that allows duplicate elements. Example: ArrayList, LinkedList.
  2. Set: A collection that doesn’t allow duplicate elements. Example: HashSet, LinkedHashSet.
  3. Map: A collection that stores key-value pairs, where each key is unique. Example: HashMap, TreeMap.

Additional interfaces and classes in collections include Queue, Deque, and Stack.


4. What is the difference between == and .equals() in Java?

Answer:

  • ==: It is a reference comparison operator. It checks if two object references point to the same memory location.
  • .equals(): It is a method provided by the Object class. It checks the actual content of two objects to see if they are logically equivalent. It’s commonly overridden in custom classes to define a content-based equality check.

5. Explain Spring Boot’s auto-configuration feature.

Answer: Spring Boot’s auto-configuration feature automatically configures Spring application context based on the libraries in the classpath and the properties defined in the application. It simplifies the configuration process by guessing the required configurations based on the environment and dependencies.

For example, if Spring Boot detects a H2 database in the classpath, it will automatically configure a data source, JDBC connection, etc., without the need for explicit configuration.


6. What is Dependency Injection (DI) in Spring?

Answer: Dependency Injection is a design pattern where an object’s dependencies (services, repositories, etc.) are provided to it from the outside rather than the object creating the dependencies itself. This is a core concept in Spring, and it helps with:

  • Loose Coupling: Objects don’t need to know how to create their dependencies, making the system more flexible.
  • Easier Testing: Dependencies can be injected using mock objects or stubs during testing.

Spring supports three types of dependency injection:

  • Constructor Injection
  • Setter Injection
  • Field Injection

7. What are Spring Boot Starter POMs?

Answer: Spring Boot Starter POMs are a set of pre-configured dependencies that help developers quickly set up common functionalities. These “starters” provide a simple way to include commonly used libraries, configurations, and tools in the Spring Boot project.

For example:

  • spring-boot-starter-web: Includes dependencies for building web applications (Spring MVC, Tomcat, etc.).
  • spring-boot-starter-data-jpa: Includes dependencies for working with databases using Spring Data JPA.
  • spring-boot-starter-test: Includes testing libraries like JUnit, Mockito, and Spring Test.

8. What is the role of @SpringBootApplication annotation?

Answer: The @SpringBootApplication annotation is a convenience annotation in Spring Boot that combines several other annotations:

  1. @Configuration: Marks the class as a source of bean definitions for the application context.
  2. @EnableAutoConfiguration: Enables Spring Boot’s auto-configuration mechanism.
  3. @ComponentScan: Tells Spring to scan the current package and its sub-packages for components.

This annotation is typically placed on the main class to set up the Spring Boot application context and start the application.


9. What is the difference between @Component, @Service, @Repository, and @Controller annotations?

Answer: These annotations are all part of Spring’s component scanning and define the role of the beans in the application.

  1. @Component: A general-purpose stereotype annotation used to define a Spring bean.
  2. @Service: A specialization of @Component, used to define service beans (business logic).
  3. @Repository: A specialization of @Component, used to define DAO beans (data access objects). It also enables exception translation (converts database-related exceptions to Spring’s DataAccessException).
  4. @Controller: A specialization of @Component, used to define Spring MVC controllers, handling HTTP requests.

10. What is Spring AOP and how is it used?

Answer: Spring AOP (Aspect-Oriented Programming) is a programming paradigm that allows you to separate cross-cutting concerns (such as logging, transaction management, security, etc.) from the core business logic. This separation allows for cleaner code and better maintainability.

Key concepts in Spring AOP:

  • Aspect: A module that encapsulates a concern (e.g., logging or transaction management).
  • Advice: The action taken at a specific point in the program (before, after, or around method execution).
  • Pointcut: A predicate that defines where the advice should be applied (e.g., methods with a certain annotation).
  • Joinpoint: A point during the execution of the program (e.g., method execution) where the advice can be applied.

Spring AOP is typically used for declarative transaction management and logging.


11. Explain the lifecycle of a Spring Bean.

Answer: The lifecycle of a Spring bean can be broken down into the following steps:

  1. Instantiation: Spring creates the bean using the default or specified constructor.
  2. Dependency Injection: Spring injects dependencies into the bean using DI (via constructor, setter, or field).
  3. Initialization: Spring calls any custom initialization methods, such as those defined with the @PostConstruct annotation or InitializingBean interface.
  4. Usage: The bean is ready for use in the application.
  5. Destruction: If the bean is a singleton, it is destroyed when the context is closed. Spring calls any custom destroy methods defined with @PreDestroy or DisposableBean.

12. What is the difference between @RequestMapping and @GetMapping in Spring MVC?

Answer: Both @RequestMapping and @GetMapping are used to handle HTTP requests in Spring MVC, but they have different purposes:

  • @RequestMapping: It is a more general annotation used to map HTTP requests to handler methods. It supports all HTTP methods (GET, POST, PUT, DELETE, etc.). You can specify the request type using the method attribute. javaCopyEdit@RequestMapping(value = "/home", method = RequestMethod.GET) public String home() { return "home"; }
  • @GetMapping: It is a shortcut for @RequestMapping(method = RequestMethod.GET). It is specifically used for HTTP GET requests. javaCopyEdit@GetMapping("/home") public String home() { return "home"; }

13. What is the use of @Transactional annotation in Spring?

Answer: The @Transactional annotation in Spring is used to manage transactions. It ensures that all operations within a method are part of a single transaction. If any operation fails, the transaction is rolled back, and all changes are reverted. It can be applied at the method or class level.


These are just a few examples, but there are many more advanced questions and answers that can be discussed based on the experience level and the specific technologies in use.