Java with Spring Boot: Essential Annotations and Concepts Quiz

Explore fundamental Spring Boot concepts, annotations, and configuration features essential for backend development with Java. Challenge your understanding of application setup, REST APIs, dependency injection, and more.

  1. Spring Boot Main Class Annotation

    Which annotation is used to mark a class as the main entry point for a Spring Boot application?

    1. @EnableAutoConfiguration
    2. @SpringBootMain
    3. @SpringBootApplication
    4. @Configuration

    Explanation: @SpringBootApplication is the primary annotation that marks a configuration class as the main entry point in Spring Boot. @SpringBootMain does not exist. @Configuration enables Java-based configuration but does not designate the entry point. @EnableAutoConfiguration only enables auto-configuration but is usually included within @SpringBootApplication.

  2. Dependency Injection in Spring

    What does the @Autowired annotation do in a Spring component?

    1. It sets up REST endpoints.
    2. It schedules tasks for execution.
    3. It enables automatic logging.
    4. It automatically injects a required dependency into a class.

    Explanation: @Autowired allows Spring to resolve and inject dependencies automatically. Automatic logging is handled by other configurations. Setting up REST endpoints uses different annotations, and scheduling tasks uses @Scheduled.

  3. Embedded Server Default

    What is the default embedded server used by Spring Boot for running web applications?

    1. Netty
    2. Undertow
    3. Jetty
    4. Tomcat

    Explanation: Tomcat is the default embedded servlet container in Spring Boot. Jetty and Undertow are supported alternatives but not defaults. Netty is used for non-servlet use cases and is not the default server.

  4. Spring Boot Configuration File

    Which file is commonly used to configure properties for a Spring Boot application?

    1. config.xml
    2. server.ini
    3. settings.json
    4. application.properties

    Explanation: application.properties is the conventional file for configuration in Spring Boot, with application.yml also supported. config.xml and settings.json are used in other frameworks, while server.ini is unrelated to Spring Boot.

  5. REST Endpoint Annotation

    Which annotation is used in Spring Boot to create RESTful endpoints in a Java class?

    1. @RequestMapping
    2. @Component
    3. @Resource
    4. @RestController

    Explanation: @RestController defines a class as a RESTful web service controller. @RequestMapping is used for mapping URLs but does not create REST controllers by itself. @Component is a general-purpose annotation and @Resource is used for dependency injection, not REST endpoints.

  6. Mapping HTTP GET Requests

    How do you map a method to handle HTTP GET requests in a Spring Boot application?

    1. @RequestBody
    2. @PathVariable
    3. @PostMapping
    4. @GetMapping

    Explanation: @GetMapping maps HTTP GET requests to specific handler methods. @PostMapping is for POST requests. @RequestBody and @PathVariable are used as method parameters for request data but do not map HTTP methods themselves.

  7. Spring Boot Actuator Purpose

    What is the main purpose of Spring Boot Actuator in an application?

    1. For handling data validation.
    2. For automatic documentation generation.
    3. For monitoring and managing application health & metrics.
    4. For rendering HTML templates.

    Explanation: Spring Boot Actuator exposes endpoints to monitor, manage, and gain insights into a running application. Data validation, HTML rendering, and documentation are handled by other features or libraries.

  8. Global Exception Handling

    Which annotation is used to handle exceptions globally across Spring Boot controllers?

    1. @ControllerAdvice
    2. @ErrorController
    3. @CatchException
    4. @ExceptionHandler

    Explanation: @ControllerAdvice is the correct way to apply global handling of exceptions across controllers. @ExceptionHandler is used for handling exceptions locally within a controller, and the other options are incorrect or non-existent in this context.

  9. JPA Entity Annotation

    In Spring Boot JPA, which annotation marks a class as a persistent entity?

    1. @Entity
    2. @Repository
    3. @Data
    4. @Table

    Explanation: @Entity is used to specify that a class is a JPA entity. @Table customizes table mapping, @Repository marks a data access layer, and @Data is a utility annotation from another library.

  10. Service Layer Annotation

    What does the @Service annotation indicate when placed on a Java class in Spring Boot?

    1. The class represents a database entity.
    2. The class contains business logic and is a Spring-managed service component.
    3. The class declares data repositories.
    4. The class acts as a REST controller.

    Explanation: @Service is used for classes encapsulating business logic and marks them as service components managed by Spring. REST controllers use @RestController, repositories use @Repository, and entities use @Entity.