Module 3: Web APIs

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

In this post, I provide accurate answers and detailed explanations for Module 3: Web APIs 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: RESTful services with Spring Boot

Practice Assignment

1. True or False: The @RestController annotation in Spring Boot is a specialized version of @Controller that combines @Controller and @ResponseBody, allowing data to be returned directly as JSON or XML without needing to specify @ResponseBody on each method.

  • True ✅
  • False

Explanation:
The @RestController annotation in Spring Boot is indeed a combination of @Controller and @ResponseBody. It allows returning data directly as JSON or XML without the need to annotate each method with @ResponseBody.

2. Which annotation handles HTTP PUT requests in a Spring Boot REST controller?

  • @PutMapping ✅
  • @DeleteMapping
  • @PostMapping
  • @GetMapping

Explanation:
The @PutMapping annotation is specifically designed to handle HTTP PUT requests in a Spring Boot REST controller.

3. Complete the following code to handle a DELETE request with /products/{id} URI and delete a product by its ID. Choose the correct annotation to bind the {id} path variable in the URL to the method parameter.

@DeleteMapping("/products/{id}")
public ResponseEntity deleteProduct(______) {
productService.deleteById(id);
return ResponseEntity.noContent().build();
}

  • Long id
  • @PathVariable Long id ✅
  • @PathVar Long id
  • @RequestParam Long id

Explanation:
The @PathVariable annotation binds the {id} path variable in the URL to the id method parameter. The corrected method signature would look like this:
public ResponseEntity<Void> deleteProduct(@PathVariable Long id) { … }

4. Which annotation is used to set a custom HTTP status code, such as 404 Not Found, when a resource is not found?

  • @ControllerAdvice
  • @ResponseStatus(HttpStatus.NOT_FOUND) ✅
  • @ResponseBody
  • @NotFound

Explanation:
The @ResponseStatus annotation is used to set a custom HTTP status code, such as 404 Not Found, for a method or exception. For example:
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException { … }

5. What’s wrong with the following code for updating a product using data from a JSON request body?

@PutMapping("/products/{id}")
public ResponseEntity updateProduct(@RequestBody Product
product, Long id) {
Product updatedProduct = productService.update(product, id);
return ResponseEntity.ok(updatedProduct);
}

  • The id parameter should be annotated with @PathVariable. ✅
  • @PutMapping should be replaced with @GetMapping.
  • The @RequestBody annotation should be on the id parameter.
  • ResponseEntity.ok() should be ResponseEntity.created().

Explanation:
In the provided code, the id parameter must be annotated with @PathVariable to bind it to the {id} in the URL. The corrected method would look like:
@PutMapping(“/products/{id}”)
public ResponseEntity<Product> updateProduct(@RequestBody Product product, @PathVariable Long id) { … }

Knowledge check: Consuming APIs on the frontend

Practice Assignment

6. What is one primary benefit of using Axios over the Fetch API for HTTP requests in JavaScript?

  • Axios is only available for Node.js environments.
  • Axios does not support setting custom headers.
  • Axios does not support error handling.
  • Axios automatically formats JSON data in requests and responses. ✅

Explanation:
Axios simplifies handling JSON by automatically transforming request data into JSON and parsing response data as JSON. Fetch requires additional steps to handle this explicitly.

7. Your front-end application is frequently interacting with an external API. Occasionally, API calls fail due to network interruptions or issues on the server side. What is the main benefit of implementing detailed error-handling techniques in this scenario?

  • It allows you to identify whether the error occurred due to network issues or server-side problems. ✅
  • It guarantees no errors will occur in the application.
  • It ensures faster performance of API calls.
  • It prevents users from seeing incomplete or incorrect data during an error.

Explanation:
Implementing detailed error-handling techniques helps developers distinguish between network and server-side issues, making debugging easier and improving the user experience.

8. Which of the following are the benefits of using Axios over the Fetch API? Select all that apply.

  • Simplified syntax and automatic JSON handling for both requests and responses.
  • Axios is designed only for use in React applications.
  • Fetch API offers faster performance than Axios.
  • Enhanced error handling that distinguishes between HTTP and network errors. ✅

Explanation:
Enhanced Error Handling: Axios can distinguish between network errors (e.g., no response from the server) and HTTP errors (e.g., 404 or 500 status codes), making error handling more robust.

9. You are building a shopping list application and need to update the price of a specific product. Which Axios request should you use to modify the product’s price?

  • GET
  • PUT ✅
  • DELETE
  • POST

Explanation:
The PUT method is used to update an existing resource with new data, such as modifying a product’s price.

10. You want to delete a product from your API shopping list. Which of the following is the correct Axios code for this action?

  • axios.get(‘http://localhost:8080/api/v1/products/1’)
  • axios.post(‘http://localhost:8080/api/v1/products/1’)
  • axios.delete(‘http://localhost:8080/api/v1/products/1’) ✅
  • axios.put(‘http://localhost:8080/api/v1/products/1’)

Explanation:
The DELETE method is used to remove a specific resource from the server. Axios provides a straightforward syntax for this:
axios.delete(‘http://localhost:8080/api/v1/products/1’);

Knowledge check: Exploring cloud deployment and the Amazon ecosystem

Practice Assignment

11. True or False: AWS Lambda allows you to run code without provisioning or managing servers.

  • True ✅
  • False

Explanation:
AWS Lambda allows you to run code without provisioning or managing servers, making it a serverless computing service.

12. Which service is primarily used for relational database management in AWS?

  • AWS S3
  • AWS Lambda
  • AWS RDS ✅
  • AWS EC2

Explanation:
AWS Relational Database Service (RDS) is used for relational database management, supporting databases like MySQL, PostgreSQL, and SQL Server.

13. True or False: AWS S3 stores and retrieves object-based data, making it ideal for backups, archives, and media storage.

  • True ✅
  • False

Explanation:
AWS S3 (Simple Storage Service) is designed for object storage and is ideal for backups, archives, and storing media files.

14. Which of the following is a key benefit of using AWS EC2 for cloud deployment?

  • Built-in serverless architecture.
  • Scalability and flexibility to increase or decrease instance capacity as needed. ✅
  • Fully managed relational database services.
  • Object storage for unstructured data.

Explanation:
AWS EC2 (Elastic Compute Cloud) provides scalable virtual servers, allowing users to adjust instance capacity based on their needs.

15. What is the purpose of the AWS API Gateway?

  • To create serverless compute environments for code execution.
  • To store and retrieve unstructured data.
  • To allow developers to create, publish, and manage APIs at any scale. ✅
  • To provide virtual servers for application hosting.

Explanation:
AWS API Gateway helps developers create, deploy, and manage APIs for their applications, enabling integration with various services and scaling as needed.

Module quiz: Web APIs

Graded Assignment

16. You are managing an existing user profile that requires a complete update with new information. Which HTTP method should you use to ensure the profile’s content is entirely replaced?

  • DELETE
  • GET
  • POST
  • PUT ✅

Explanation:
The HTTP PUT method is used to completely replace an existing resource with new data.

17. Identify the missing piece to make the following Fetch API code, intended to retrieve a resource, more efficient:

fetch('https://api.example.com/resource', {
method: 'GET',
headers: {
'____________': 'application/json'
}
});

  • Content-Length
  • Content-Type ✅
  • Authorization
  • Accept

Explanation:
The Content-Type header specifies the media type of the request payload, helping the server understand how to process the incoming data.

18. Which of the following statements accurately describes the use of the @RequestMapping annotation in Spring Boot?

  • @RequestMapping can handle multiple HTTP methods, allowing a single endpoint to respond to various requests. ✅
  • @RequestMapping is deprecated and should be replaced with @GetMapping or @PostMapping for handling HTTP requests.
  • @RequestMapping can only handle GET requests and is not suitable for POST or PUT operations. Each
  • @RequestMapping annotation can only define a single HTTP method for an endpoint.

Explanation:
The @RequestMapping annotation in Spring Boot supports multiple HTTP methods such as GET, POST, PUT, and DELETE.

19. In a Spring Boot application, why are validation annotations important for your RESTful services?

  • Validation annotations are only needed for GET requests when retrieving data.
  • Using annotations like @Valid enables automatic validation of incoming data based on rules defined in the model. ✅
  • You can rely solely on error handling to validate data without using annotations.
  • Validation annotations are optional and can be skipped if you manually check the data elsewhere.

Explanation:
Validation annotations, like @Valid, ensure data integrity by automatically checking if incoming data adheres to predefined rules in the model.

20. Consider the following API response for a resource in your RESTful API. What is the primary benefit of including these links in the response?

"bookId": 123,
"title": "Learning REST",
"author": "John Doe",
"links": [
{
"rel": "self",
"href": "https://api.example.com/books/123"
},
{
"rel": "reviews",
"href": "https://api.example.com/books/123/reviews"
},
{
"rel": "author",
"href": "https://api.example.com/authors/john-doe"
}
]
}

  • They allow clients to explore related resources dynamically, such as reading reviews or finding more about the author. ✅
  • They ensure that all requests made to the API are validated against predefined rules.
  • They guarantee that the API response will always be faster.
  • They provide a standard way to handle errors in the API response

Explanation:
Including links in API responses adheres to the HATEOAS principle, making APIs more self-descriptive and discoverable.

21. Complete the following code to correctly delete a resource in Spring Boot by binding the {id} path variable to the method parameter id:

@DeleteMapping("/products/{id}") public void
deleteProduct(____________) {
productService.delete(id);
}

  • @RequestBody String id
  • @RequestParam String id
  • @PathVariable String id ✅
  • String id

Explanation:
The @PathVariable annotation binds the {id} placeholder in the URL to the method parameter.

22. You are developing a Java application to upload large files to AWS S3. Given the code snippet below, which method should you use to upload files?

private AmazonS3 s3Client =
AmazonS3ClientBuilder.defaultClient();

public void uploadFile(String bucketName, String fileName) {
// Call the appropriate S3 method to upload the file
s3Client.____________(bucketName, fileName);
}
}

  • uploadObject
  • completeMultipartUpload
  • addObject
  • putObject ✅

Explanation:
The putObject method of the AmazonS3 client is used to upload files to an S3 bucket.

23. What feature of AWS Lambda allows it to handle varying levels of user activity effectively?

  • Integration with static storage
  • Automatic scaling based on incoming requests ✅
  • Manual configuration of server resources
  • Fixed capacity that needs to be managed

Explanation:
AWS Lambda automatically scales in response to the number of incoming requests, making it highly efficient for varying workloads.

24. The following code snippet is intended to perform a PUT request using the Fetch API, but it has an error in formatting the body. What correction should be made to properly format the body for the PUT request?"

fetch('https://api.example.com/update-item', {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: data // This line is incorrect
});

  • Change body: data to body: new Array(data)
  • Change body: data to body: JSON.parse(data)
  • Change body: data to body: data.toJSON()
  • Change body: data to body: JSON.stringify(data) ✅

Explanation:
The body of a PUT request must be properly formatted as a JSON string, which is achieved using JSON.stringify().

25. To create and manage relational databases in a scalable and managed environment on AWS, you should use __________.

  • AWS RDS ✅
  • AWS Lambda
  • AWS S3
  • AWS EC2

Explanation:
AWS RDS (Relational Database Service) provides scalable and managed relational database environments, supporting engines like MySQL, PostgreSQL, and SQL Server.

Leave a Reply