Module 2: Java web development

Looking for ‘full stack web development module 2 answers‘?

In this post, I provide accurate answers and detailed explanations for Module 2: Java web development of Course 5: Full Stack Web DevelopmentAmazon Junior Software Developer Professional Certificate.

Whether you’re preparing for quizzes or brushing up on your knowledge, these insights will help you master the concepts effectively. Let’s dive into the correct answers and detailed explanations for each question.

Knowledge check: MVC (model view controller) architecture and Spring

Practice Assignment

1. You are building a web application using Spring MVC, where you need to separate the data, user interface, and business logic. What does MVC stand for in this context?

  • Model-Value-Configuration
  • Module-View-Controller
  • Multi-View-Controller
  • Model-View-Controller ✅

Explanation:

  • MVC is a design pattern that separates:
    • Model: Represents the data and business logic.
    • View: Handles the user interface.
    • Controller: Manages communication between the Model and View.

2. You are setting up a new controller class in a Spring MVC project to handle incoming HTTP requests. Which annotation should you use to create this class as a controller?

  • @ControllerBean
  • @SpringController
  • @WebController
  • @Controller ✅

Explanation:

  • The @Controller annotation is used to designate a class as a Spring MVC Controller, which can handle HTTP requests and map them to specific methods.

3. You’re creating a Spring Boot project that requires database interaction with JPA (Java Persistence API). Which dependency should you add to your project to use Spring Data JPA?

  • spring-boot-starter-data-jpa ✅
  • spring-boot-starter-security
  • spring-boot-starter-web
  • spring-boot-starter-test

Explanation:

  • The spring-boot-starter-data-jpa dependency includes all the libraries needed to work with JPA in Spring Boot, such as Hibernate.

4. You are defining an entity class in JPA for a Spring Boot project and need to specify which field will serve as the primary key. Which annotation should you use for this?

  • @PrimaryKey 
  • @Id ✅
  • @Primary
  • @Key

Explanation:

  • The @Id annotation is used in JPA to mark a field as the primary key for the entity.

5. You are building a form in a Spring Boot application and want to ensure that a user-provider field, like “email,” cannot be left null. Which annotation should you use in the validation framework?

  • @NonNull
  • @Range
  • @NotNull ✅
  • @Size

Explanation:

  • The @NotNull annotation ensures that the annotated field cannot be null.
  • Other annotations like @NonNull (from Lombok) or @Range and @Size serve different purposes, such as range or size validation.

Knowledge check: Java Server technologies

Practice Assignment

6. True or False: Spring Boot provides default configurations to help developers set up a Spring application quickly.

  • False
  • True ✅

Explanation:

  • Spring Boot includes default configurations for features like embedded web servers, data sources, and logging to simplify setup and reduce boilerplate code.

7. Which annotation is used to create a RESTful web service endpoint in Spring Boot?

  • @Service
  • @Controller
  • @Component
  • @RestController ✅

Explanation:

  • The @RestController annotation combines @Controller and @ResponseBody, making it ideal for developing REST APIs in Spring Boot.
  • Other annotations like @Service, @Controller, and @Component serve different purposes, such as defining services or components.

8. In a typical Spring Boot project structure, which package should contain the classes that handle HTTP requests?

  • Service
  • Repository
  • Model
  • Controller ✅

Explanation:

  • In Spring Boot, classes in the Controller package are responsible for handling HTTP requests and returning responses. These are typically annotated with @Controller or @RestController.

9. What is the primary responsibility of the Service layer in Spring Boot?

  • Handling business logic ✅
  • Rendering views
  • Managing database connections
  • Handling HTTP requests

Explanation:

  • The Service layer is responsible for implementing business logic. It acts as an intermediary between the Controller layer and the Repository layer, ensuring separation of concerns.

10. Which of the following can be configured in the application.properties file in Spring Boot?

  • Logging level
  • All of the options ✅
  • Server port
  • Database connection URL

Explanation:

  • The application.properties file allows configuration of:
    • Logging levels (e.g., logging.level.root=DEBUG)
    • Server port (e.g., server.port=8080)
    • Database connection URL (e.g., spring.datasource.url=jdbc:mysql://...)
    • And many other settings to customize the application.

Knowledge check: Advanced Spring Boot features and best practices

Practice Assignment

11. What’s missing in the following pom.xml configuration to build the Spring Boot application into an executable JAR?

  • Java Development Kit (JDK) dependency
  • H2 Database dependency
  • Spring Boot Starter Data JPA
  • Spring Boot Maven plugin ✅

Explanation:

  • The Spring Boot Maven plugin is required to package the application as an executable JAR. Without it, the build system won’t properly embed dependencies or create a runnable JAR.
  • Example:
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

12. In the following SecurityConfig class, what’s missing to properly secure the /admin/** endpoints for users with the "ADMIN" role?

http
.authorizeHttpRequests()
.requestMatchers("/admin/**").authenticated()
.anyRequest().permitAll()
.and()
.formLogin();

  • csrf().disable()
  • httpBasic()
  • hasRole(“ADMIN”) ✅
  • permitAll()

Explanation:

  • To secure the /admin/** endpoints for users with the “ADMIN” role, you need to specify the role explicitly using .hasRole("ADMIN"). The correct configuration would look
  • like:
    http
    .authorizeHttpRequests()
    .requestMatchers(“/admin/**”).hasRole(“ADMIN”)
    .anyRequest().permitAll()
    .and()
    .formLogin();

13. You’re developing a registration feature for a new web application. A user’s password needs to be stored securely in the system so they can log in with their credentials. Which of the following changes would allow a newly registered user to log in successfully?

public class UserService {
public void registerUser(User user) {
user.setPassword(user.getPassword()); // Issue here
users.put(user.getUsername(), user);
}
}

  • Encrypt the password using ✅ passwordEncoder.encode(user.getPassword()). ✅
  • Store the password in plain text.
  • Remove the password altogether.
  • Use findById() instead of put() for saving users.

Explanation:

  • Storing passwords securely is critical. Plaintext passwords are vulnerable to attacks. Use a password encoder, such as BCrypt, to hash the password before saving it.
  • For example:
    user.setPassword(passwordEncoder.encode(user.getPassword()));

14. True or False: Spring Boot Actuator automatically exposes all endpoints, including /metrics, /health, and /env, without any configuration.

  • False ✅
  • True

Explanation:

  • By default, Spring Boot Actuator exposes only a limited set of endpoints (e.g., /health and /info). Other endpoints, such as /metrics and /env, must be explicitly enabled in the application.properties file.
  • Example:
    management.endpoints.web.exposure.include=health,info,metrics,env

15. Why is this Spring Boot application not starting when attempting to run the JAR file for the given pom.xml file?

  • The application is missing the @SpringBootApplication annotation.
  • The target directory was not cleaned before building.
  • The application is missing a database connection.
  • The application is missing the Spring Boot Maven Plugin. ✅

Explanation:

  • The Spring Boot Maven Plugin is required for creating a runnable JAR. Without this plugin, the packaging won’t include the necessary classes or dependencies to run the application.

Module quiz: Java web development

Graded Assignment

16. True or False: Your team is building a Spring Boot application that will need to run independently on different environments without relying on an external application server.

  • True ✅
  • False

Explanation:

  • Spring Boot applications include an embedded server (e.g., Tomcat or Jetty) by default, allowing them to run independently without requiring an external application server.

17. A new requirement states that only authenticated admin users should be able to access the /admin endpoint. Which code snippet correctly configures this in Spring Security?

http.authorizeRequests()
.requestMatchers("/admin")._____;

  • hasRole(“ADMIN”) ✅
  • permitAll()
  • denyAll()
  • authenticated()

Explanation:

  • To restrict access to the /admin endpoint for users with the “ADMIN” role, use .hasRole("ADMIN") in the Spring Security configuration.
    Example:
    http.authorizeRequests()
    .requestMatchers(“/admin”).hasRole(“ADMIN”);

18. Which interface should you extend to access standard CRUD methods in Spring Data JPA?

  • QueryRepository
  • CrudRepository
  • JpaRepository ✅
  • DatabaseRepository

Explanation:

  • While CrudRepository provides basic CRUD functionality, JpaRepository extends CrudRepository and adds more advanced features such as batch operations and pagination. It is the recommended choice when working with JPA in Spring Data projects.

For example:public interface UserRepository extends JpaRepository<User, Long> {
}

19. Which of these endpoints provides detailed information on application metrics?

  • /actuator/health
  • /actuator/env
  • /actuator/metrics ✅
  • /actuator/info

Explanation:

  • The /actuator/metrics endpoint provides metrics about the application, such as memory usage, CPU, and custom metrics.

20. True or False: Spring Boot automatically loads properties from application.properties or application.yml.

  • False
  • True ✅

Explanation:

  • Spring Boot automatically loads configuration properties from application.properties or application.yml files located in the src/main/resources directory.

21. In a Spring Boot project, which plugin is used to package the application?

  • spring-boot-maven-plugin ✅
  • spring-boot-gradle-plugin
  • spring-mvc-plugin spring-boot-
  • starter-plugin

Explanation:

  • The spring-boot-maven-plugin is used to build and package Spring Boot applications into runnable JAR files.

22. Your team has chosen JSP as the view layer for a Spring Boot application. Which additional configuration step is needed to render JSP pages correctly?

  • Use @JspMapping
  • No configuration is needed
  • Use @Jsp
  • Add a view resolver for JSP ✅

Explanation:

  • JSP requires a ViewResolver configuration in Spring Boot, typically a InternalResourceViewResolver, to correctly map view names to JSP files.

23. Which annotation should you use to specify the table name for a JPA entity?

  • @Id
  • @Column
  • @Entity
  • @Table ✅

Explanation:

  • Use the @Table annotation to specify the table name associated with a JPA entity.
    Example:
    @Entity
    @Table(name = “products”)
    public class Product {
    // Fields and methods
    }

24. Complete the code by selecting the appropriate Spring MVC annotation to map the method to the /products endpoint for the correct HTTP request type.

// Missing annotation here
public String listProducts(Model model) {
model.addAttribute("products", productService.getAllProducts());
return "product-list";
}

  • @PostMapping (“/products”)
  • @DeleteMapping (“/products”)
  • @GetMapping (“/products”) ✅
  • @PutMapping (“/products”)

Explanation:

  • The @GetMapping annotation maps a method to an HTTP GET request, which is typically used to fetch data. In this case, it’s used to display the list of products.

25. Your team decides to add a service layer to manage business logic separately from the controller in a Spring Boot application.

  • True ✅
  • False

Explanation:

  • The service layer is a best practice in Spring applications to separate business logic from the controller, which is responsible for handling HTTP requests and responses.

Leave a Reply