Bird
Raised Fist0
Angularframework~20 mins

PUT and DELETE requests in Angular - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
REST API Mastery in Angular
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when the PUT request succeeds?

Consider this Angular component that updates a user profile using a PUT request. What will be the value of updateStatus after a successful response?

Angular
import { Component, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-user-update',
  template: `<button (click)="updateUser()">Update User</button><p>{{updateStatus}}</p>`,
  standalone: true
})
export class UserUpdateComponent {
  private http = inject(HttpClient);
  updateStatus = '';

  updateUser() {
    this.http.put('/api/users/1', { name: 'Alice' }).subscribe({
      next: () => this.updateStatus = 'Update successful',
      error: () => this.updateStatus = 'Update failed'
    });
  }
}
AAn error will be thrown because PUT requests cannot be made this way.
BThe text 'Update successful' will be displayed.
CNothing will change because the PUT request does not update the status.
DThe text 'Update failed' will be displayed.
Attempts:
2 left
💡 Hint

Think about what happens when the next callback runs in the subscription.

📝 Syntax
intermediate
1:30remaining
Which option correctly sends a DELETE request with Angular HttpClient?

Choose the correct syntax to send a DELETE request to /api/items/42 using Angular's HttpClient.

Athis.http.deleteRequest('/api/items/42').subscribe();
Bthis.http.remove('/api/items/42').subscribe();
Cthis.http.del('/api/items/42').subscribe();
Dthis.http.delete('/api/items/42').subscribe();
Attempts:
2 left
💡 Hint

Check the official Angular HttpClient method names for DELETE requests.

❓ state_output
advanced
2:30remaining
What is the value of items after deleting an item?

Given this Angular component code, what will be the value of items after deleteItem(2) is called and the DELETE request succeeds?

Angular
import { Component, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-item-list',
  template: `<ul><li *ngFor="let item of items">{{item.name}}</li></ul>`,
  standalone: true
})
export class ItemListComponent {
  private http = inject(HttpClient);
  items = [
    { id: 1, name: 'Pen' },
    { id: 2, name: 'Notebook' },
    { id: 3, name: 'Eraser' }
  ];

  deleteItem(id: number) {
    this.http.delete(`/api/items/${id}`).subscribe(() => {
      this.items = this.items.filter(item => item.id !== id);
    });
  }
}
A[{ id: 1, name: 'Pen' }, { id: 3, name: 'Eraser' }]
B[{ id: 1, name: 'Pen' }, { id: 2, name: 'Notebook' }, { id: 3, name: 'Eraser' }]
C[]
D[{ id: 2, name: 'Notebook' }]
Attempts:
2 left
💡 Hint

Look at how the items array is updated inside the subscribe callback.

🔧 Debug
advanced
2:00remaining
Why does this PUT request not update the server?

Examine this Angular service method. The PUT request does not update the server as expected. What is the most likely cause?

Angular
updateUser(user: any) {
  this.http.put('/api/users/' + user.id, user);
}
AThe HttpClient does not support PUT requests without a body.
BThe URL is incorrect because it should use backticks for string interpolation.
CThe PUT request is not subscribed to, so it never executes.
DThe user object is missing required headers for PUT requests.
Attempts:
2 left
💡 Hint

Remember how Angular HttpClient observables work when not subscribed.

🧠 Conceptual
expert
1:30remaining
What is the main difference between PUT and DELETE requests in REST APIs?

Choose the best explanation of how PUT and DELETE requests differ in RESTful web services.

APUT replaces or updates a resource, while DELETE removes the resource entirely.
BPUT removes a resource, while DELETE updates the resource partially.
CPUT creates a new resource, while DELETE only reads resource data.
DPUT and DELETE both update resources but differ in request headers.
Attempts:
2 left
💡 Hint

Think about the purpose of each HTTP method in REST.

Practice

(1/5)
1. What is the main purpose of a PUT request in Angular's HttpClient?
easy
A. To retrieve data from the server
B. To delete a resource from the server
C. To update an existing resource by sending the full new data
D. To partially update a resource

Solution

  1. Step 1: Understand HTTP methods

    PUT is used to update or replace an existing resource completely.
  2. Step 2: Compare with other methods

    DELETE removes data, GET retrieves data, PATCH partially updates data.
  3. Final Answer:

    To update an existing resource by sending the full new data -> Option C
  4. Quick Check:

    PUT updates full resource = A [OK]
Hint: PUT replaces full data, DELETE removes resource [OK]
Common Mistakes:
  • Confusing PUT with DELETE
  • Thinking PUT partially updates data
  • Mixing GET with PUT
2. Which of the following is the correct syntax to send a DELETE request using Angular's HttpClient?
easy
A. this.http.delete('api/items/1').subscribe();
B. this.http.put('api/items/1').subscribe();
C. this.http.get('api/items/1').subscribe();
D. this.http.post('api/items/1').subscribe();

Solution

  1. Step 1: Identify the method for DELETE

    Angular's HttpClient uses delete() to send DELETE requests.
  2. Step 2: Check syntax correctness

    The method call this.http.delete('api/items/1').subscribe(); correctly sends a DELETE request and subscribes to the response.
  3. Final Answer:

    this.http.delete('api/items/1').subscribe(); -> Option A
  4. Quick Check:

    DELETE method uses http.delete() [OK]
Hint: Use http.delete() for DELETE requests [OK]
Common Mistakes:
  • Using http.put() instead of http.delete()
  • Forgetting to subscribe to the observable
  • Using http.post() for deleting
3. Given this Angular code snippet, what will be logged after the PUT request completes successfully?
const updatedItem = { id: 5, name: 'New Name' };
this.http.put('api/items/5', updatedItem).subscribe(response => {
  console.log(response);
});
medium
A. The updated item object returned from the server
B. An error message because PUT requires no body
C. Nothing, because PUT does not return data
D. The original item before update

Solution

  1. Step 1: Understand PUT request behavior

    PUT sends the full updated data and usually returns the updated resource from the server.
  2. Step 2: Analyze the subscription callback

    The response logged is the server's reply, typically the updated item object.
  3. Final Answer:

    The updated item object returned from the server -> Option A
  4. Quick Check:

    PUT returns updated data = A [OK]
Hint: PUT usually returns updated resource in response [OK]
Common Mistakes:
  • Assuming PUT returns no data
  • Confusing PUT with DELETE response
  • Expecting original item instead of updated
4. Identify the error in this Angular DELETE request code:
this.http.delete('api/items').subscribe(response => {
  console.log('Item deleted');
});

Assuming the API requires an item ID to delete a specific item.
medium
A. DELETE requests cannot be sent without a request body
B. The URL is missing the item ID to specify which item to delete
C. The subscribe method is missing a parameter
D. The console.log statement is incorrect

Solution

  1. Step 1: Check the URL for DELETE request

    The URL 'api/items' does not specify which item to delete; an ID is needed.
  2. Step 2: Understand API requirements

    APIs usually require an ID in the URL like 'api/items/1' to delete a specific item.
  3. Final Answer:

    The URL is missing the item ID to specify which item to delete -> Option B
  4. Quick Check:

    DELETE URL needs item ID = C [OK]
Hint: DELETE URL must include resource ID to delete [OK]
Common Mistakes:
  • Sending DELETE without specifying resource ID
  • Thinking DELETE needs a request body
  • Ignoring subscribe callback
5. You want to update a user's profile and then delete their old profile picture in Angular. Which sequence of HttpClient calls correctly applies PUT and DELETE requests?
// Update user profile
this.http.put('api/users/10', updatedUser).subscribe(() => {
  // Delete old profile picture
  this.http.delete('api/pictures/oldPicId').subscribe(() => {
    console.log('Profile updated and old picture deleted');
  });
});
hard
A. DELETE request first to delete picture, then PUT request inside DELETE's subscribe to update profile
B. Use only PUT request to update profile and delete picture together
C. Send PUT and DELETE requests simultaneously without waiting for response
D. PUT request first to update profile, then DELETE request inside PUT's subscribe to delete picture

Solution

  1. Step 1: Understand request order

    First update the user profile with PUT, then delete the old picture after success.
  2. Step 2: Analyze code structure

    DELETE request is inside PUT's subscribe callback, ensuring sequential execution.
  3. Step 3: Evaluate other options

    Deleting first may remove picture before profile update; simultaneous calls lack order; PUT cannot delete picture.
  4. Final Answer:

    PUT request first to update profile, then DELETE request inside PUT's subscribe to delete picture -> Option D
  5. Quick Check:

    Update then delete sequentially = B [OK]
Hint: Chain DELETE inside PUT subscribe for correct order [OK]
Common Mistakes:
  • Deleting before updating causing data mismatch
  • Sending requests simultaneously without order
  • Trying to delete picture with PUT request
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard