Springboot Framework Practice Test Video Answers
1. B
The @SpringBootApplication annotation is a convenience annotation that combines @Configuration (marks the class as a source of bean definitions), @EnableAutoConfiguration (enables Spring Boot’s auto-configuration mechanism), and @ComponentScan (enables component scanning in the package and sub-packages).
2. C
Apache Tomcat is the default embedded servlet container in Spring Boot. While Jetty and Undertow are also supported, Tomcat is included by default with the spring-boot-starter-web dependency.
3. C
application.properties (or application.yml) is the primary configuration file in Spring Boot for externalizing configuration. It is automatically loaded from the src/main/resources directory.
4. B
Spring Boot Starters are dependency descriptors that aggregate common dependencies for specific functionality. For example, spring-boot-starter-web includes all dependencies needed for building web applications, simplifying Maven/Gradle configuration.
5. C
@RestController is a specialized version of @Controller that combines @Controller and @ResponseBody. It indicates that every method returns a domain object instead of a view, with the response body written directly to HTTP response as JSON/XML.
6. B
@Autowired enables automatic dependency injection by allowing Spring to resolve and inject collaborating beans into your bean. It can be applied to constructors, fields, and setter methods.
7. C
The /actuator/health endpoint provides health status information about the application. It shows whether the application is up or down and can include details about dependent systems like databases and message brokers.
8. B
Spring Boot web applications run on port 8080 by default. This can be changed using the server.port property in application.properties or through command-line arguments.
9. B
@Scheduled annotation is used to configure and schedule tasks. It supports various scheduling options including fixedRate, fixedDelay, and cron expressions. Requires @EnableScheduling on a configuration class.
10. C
The server.port property in application.properties or application.yml is used to change the default port. Example: server.port=9000 changes the port to 9000.
11. B
@ControllerAdvice is used for global exception handling across all controllers. It works in combination with @ExceptionHandler methods to handle exceptions thrown by any controller in the application.
12. A
@RequestMapping maps HTTP requests to handler methods in controller classes. It can be configured with path, method, params, headers, and other attributes to match specific request patterns.
13. B
spring-boot-starter-data-jpa is the starter dependency for using Spring Data JPA. It includes Hibernate as the default JPA implementation and provides repository support.
14. B
@Entity (from JPA/Jakarta Persistence) indicates that the class is a JPA entity and should be mapped to a database table. Each instance represents a row in the table.
15. C
@Value annotation is used to inject values from properties files, environment variables, or SpEL expressions into fields. Example: @Value(“${app.name}”) injects the value of app.name property.
16. B
@Transactional annotation manages database transactions declaratively. It ensures that a method executes within a transaction context, with automatic commit on success and rollback on exceptions.
17. B
CrudRepository provides generic CRUD operations (save, findById, findAll, delete, etc.). JpaRepository extends CrudRepository and adds JPA-specific methods like flush() and batch operations.
18. A
Spring Boot uses the naming convention application-{profile}.properties for profile-specific configuration files. For example, application-dev.properties is loaded when the “dev” profile is active.
19. B
@PostConstruct (from Jakarta EE) marks a method to be executed after dependency injection is complete. It’s useful for initialization logic that depends on injected dependencies.
20. B
@SpringBootTest creates an integration test that loads the full application context. It boots up the entire Spring Boot application, making it suitable for end-to-end testing.
21. B
@Valid (from Jakarta Validation) triggers validation on the annotated parameter. When used with @RequestBody, it validates the request body against constraints defined in the DTO class.
22. B
@PathVariable extracts values from URI template variables. For example, in /users/{id}, @PathVariable(“id”) captures the id value from the URL path.
23. C
The “request” scope creates a new bean instance for each HTTP request. It’s available only in web-aware Spring ApplicationContext. The bean is destroyed after the request completes.
24. B
@Qualifier specifies which bean to inject when multiple beans of the same type exist. It’s used alongside @Autowired to disambiguate between candidate beans by their names or qualifiers.
25. B
@EnableWebSecurity enables Spring Security’s web security support and provides Spring MVC integration. It’s typically placed on a configuration class that extends WebSecurityConfigurerAdapter or uses SecurityFilterChain.
26. B
Spring Boot uses SLF4J as the logging facade with Logback as the default implementation. This combination provides powerful logging capabilities with minimal configuration required.
27. C
@PutMapping is idempotent, meaning multiple identical requests have the same effect as a single request. It’s used for updating/replacing entire resources. POST is not idempotent, while DELETE and GET are also idempotent.
28. B
@Lazy delays the initialization of a bean until it is first requested. By default, Spring creates singleton beans eagerly during application startup. @Lazy can improve startup time for beans that are expensive to create.
29. B
@Bean is used within @Configuration classes to define bean instances. The method return value is registered as a bean in the Spring application context. The method name becomes the bean name by default.
30. B
@ConditionalOnProperty creates beans conditionally based on property values. It checks if a specified property exists and optionally matches a given value before creating the bean.
31. B
findAll(Pageable pageable) accepts a Pageable parameter and returns a Page object containing paginated results. The Pageable interface specifies page number, page size, and sorting options.
32. B
@EnableCaching activates Spring’s annotation-driven cache management capability. Once enabled, methods can be annotated with @Cacheable, @CachePut, and @CacheEvict to cache method results.
33. B
@Query allows defining custom JPQL or native SQL queries on repository methods. It overrides the default query generation from method names, providing more control over the executed query.
34. B
@ResponseStatus sets the HTTP status code for a response. It can be applied to exception classes or handler methods to specify the response status code (e.g., HttpStatus.NOT_FOUND for 404).
35. B
Spring Boot DevTools provides development-time features including automatic restart when classpath files change. It also enables LiveReload for browser refresh and provides sensible development-time defaults.
36. B
@RequestParam binds query parameters (URL parameters) to method parameters in controller methods. For example, in “/search?keyword=spring”, @RequestParam captures the “keyword” value. It can also specify default values and whether the parameter is required.
37. A
@Service (along with @Component, @Repository, and @Controller) creates a singleton bean by default in Spring. The singleton scope means only one instance of the bean exists in the Spring container throughout the application lifecycle.
38. B
spring-boot-starter-actuator provides production-ready features for monitoring and managing your application. It includes endpoints for health checks, metrics, info, environment details, HTTP trace, and more for operational insights.
39. A
@JsonIgnore (from Jackson library) is used to ignore a field during JSON serialization and deserialization. @Transient is used for JPA to exclude fields from database persistence, not JSON processing.
40. B
@EnableAutoConfiguration tells Spring Boot to automatically configure the application based on the dependencies present in the classpath. It examines what JARs are available and configures beans accordingly, reducing manual configuration effort.
41. C
@Primary indicates that a bean should be given preference when multiple candidates qualify to autowire a single-valued dependency. It resolves ambiguity without requiring @Qualifier on every injection point.
42. C
The default scope for Spring beans is singleton, meaning the Spring container creates only one instance of the bean, and all requests for that bean return the same cached instance. This can be changed using @Scope annotation.
43. B
@RequestBody binds the HTTP request body to a method parameter. It uses HTTP message converters (like Jackson for JSON) to deserialize the request body into the specified Java object type.
44. B
@Repository indicates that the class is a Data Access Object (DAO) that accesses the database. It’s a specialization of @Component and enables automatic exception translation from database-specific exceptions to Spring’s DataAccessException hierarchy.
45. B
spring.profiles.active property is used to set the active profile(s) in Spring Boot. It can be set in application.properties, as a command-line argument (–spring.profiles.active=dev), or as an environment variable.
46. B
@MockBean creates a Mockito mock and adds it to the Spring application context. It replaces any existing bean of the same type in the context, allowing you to define mock behavior for integration tests without affecting real beans.