When implementing caching in Spring Boot, how can you handle cache concurrency?

  • By using a ConcurrentCacheManager as the cache manager.
  • By setting the spring.cache.concurrency property in the application.properties file.
  • By using the @Cacheable annotation with a sync attribute.
  • By implementing custom synchronization logic in your code.
To handle cache concurrency in Spring Boot, you can use the @Cacheable annotation with a sync attribute set to true. This ensures that cacheable methods are synchronized, preventing multiple threads from recomputing the same cached value concurrently. The other options are not standard approaches to handling cache concurrency in Spring Boot, and using a ConcurrentCacheManager is not a built-in feature of Spring Boot's caching framework.

Which annotation is primarily used for writing integration tests in Spring Boot applications?

  • @Autowired
  • @Component
  • @RunWith(SpringRunner.class)
  • @SpringBootTest
The @SpringBootTest annotation is primarily used for writing integration tests in Spring Boot applications. It loads the complete Spring application context, allowing you to perform tests in a real Spring environment.

Which of the following is true about the deleteById method of a JpaRepository?

  • It deletes an entity by its primary key and returns the deleted entity.
  • It deletes all entities in the repository and returns the number of deletions.
  • It marks the entity as "deleted" but does not physically remove it from the database.
  • It is not a standard method provided by JpaRepository.
The deleteById method of a JpaRepository deletes an entity by its primary key and returns the deleted entity. This method is a convenient way to remove a specific entity from the database. The other options do not accurately describe the behavior of this method, as it neither deletes all entities nor marks an entity as "deleted" without removing it from the database.

In a Spring Boot application, the _____ annotation is used to bind the value of a method parameter to a named cookie value.

  • @Cookie
  • @CookieParam
  • @CookieValue
  • @ValueCookie
In a Spring Boot application, the @CookieValue annotation is used to bind the value of a method parameter to a named cookie value. This allows you to access and use cookies sent by the client in your controller methods. It simplifies the process of working with cookies in a Spring Boot application.

Suppose you are working on a Spring Boot project and need to ensure that certain fields in the incoming request payload are consistent with each other (e.g., startDate should be before endDate). How would you implement this validation?

  • Implement field consistency checks in a custom validator.
  • Perform field consistency checks in the controller layer.
  • Use exception handling to enforce field consistency.
  • Use the @AssertTrue annotation for field consistency checks.
To enforce consistency between certain fields in the incoming request payload in a Spring Boot project, implementing field consistency checks in a custom validator is a suitable approach. The @AssertTrue annotation is typically used for boolean conditions, and exception handling is not the ideal way to validate such constraints. The controller layer should primarily handle request/response handling, not field-level validation.

What is the role of the @ControllerAdvice annotation in a Spring Boot application?

  • To define request mapping for a controller.
  • To handle exceptions at the controller level.
  • To specify the HTTP request method.
  • To declare a controller class.
The @ControllerAdvice annotation in a Spring Boot application is used to handle exceptions at the controller level. It allows you to define global exception handling logic that can be applied across multiple controllers. This is particularly useful for defining consistent error handling behavior in your application. The other options do not accurately describe the role of @ControllerAdvice.

In reactive programming with Spring Boot, which interface represents a stream of 0 or 1 item?

  • Mono
  • Flux
  • Observable
  • Stream
In Spring Boot's reactive programming, the Mono interface represents a stream of 0 or 1 item. It's part of Project Reactor, which is used for reactive programming in Spring. A Mono can emit either a single item or no item at all, making it suitable for situations where you expect zero or one result, such as fetching a single record from a database or handling optional values.

Which annotation is primarily used to declare a field to be validated using JSR-303 Bean Validation?

  • @Assert
  • @NotNull
  • @Valid
  • @Validate
The primary annotation used to declare a field to be validated using JSR-303 Bean Validation is @NotNull. This annotation specifies that a field must not be null, and it is commonly used to validate input parameters or form fields to ensure they have values. The other annotations mentioned have different purposes and are not typically used for field validation.

To resolve ambiguity and specify which bean should be wired when there are multiple beans of the same type, one can use the _____ annotation in Spring

  • @Component
  • @Qualifier
  • @Repository
  • @Service
To resolve ambiguity when there are multiple beans of the same type, the "@Qualifier" annotation in Spring is used. It allows you to specify which bean should be wired by providing the name or ID of the desired bean. The other annotations, such as "@Component," "@Service," and "@Repository," are used for different purposes, like marking classes for component scanning, but they do not resolve bean wiring ambiguity.

The _____ utility in Spring Boot allows for creating disposable instances of common databases, web browsers, or anything that can run in a Docker container, for testing.

  • @Disposable
  • @DockerTest
  • @Profile
  • @TestContainers
Spring Boot, using the @TestContainers annotation, allows you to create disposable instances of databases, web browsers, or other services in Docker containers for testing purposes. It simplifies the process of setting up and tearing down these resources for testing.