0
0
Rest APIprogramming~20 mins

PUT for full replacement in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PUT Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the result of a PUT request replacing a resource?

Consider a REST API where a resource at /users/123 currently has the data:

{"name": "Alice", "age": 30, "city": "New York"}

A client sends a PUT request to /users/123 with the body:

{"name": "Alice", "age": 31}

What will be the state of the resource after this PUT request?

A{"name": "Alice", "age": 31, "city": "New York"}
B{"name": "Alice", "age": 31}
C{"name": "Alice", "age": 30, "city": "New York"}
D{"name": "Alice", "age": 31, "city": null}
Attempts:
2 left
💡 Hint

Remember that PUT replaces the entire resource, not just updates parts.

🧠 Conceptual
intermediate
1:00remaining
Which HTTP method is best for full replacement of a resource?

You want to replace an entire resource on the server with new data. Which HTTP method should you use?

APOST
BGET
CPUT
DPATCH
Attempts:
2 left
💡 Hint

One method is designed to replace the entire resource, while another updates parts.

🔧 Debug
advanced
3:00remaining
Why does this PUT request not update the resource as expected?

A developer sends a PUT request to update a user resource with the following JSON body:

{"name": "Bob", "age": 25}

But after the request, the resource still has the old data including city. The server code handling the PUT is:

def put_user(user_id, data):
    user = get_user(user_id)
    user.update(data)
    save_user(user)

Why does the resource not get fully replaced?

Rest API
def put_user(user_id, data):
    user = get_user(user_id)
    user.update(data)
    save_user(user)
AThe code uses update(), which merges data instead of replacing the resource.
BThe server should use PATCH instead of PUT for full replacement.
CThe save_user function is missing a commit call.
DThe get_user function returns a copy, so changes are lost.
Attempts:
2 left
💡 Hint

Think about what update() does to a dictionary.

📝 Syntax
advanced
1:30remaining
Which JSON body is valid for a PUT request replacing a resource?

You want to send a PUT request to replace a resource. Which JSON body is syntactically valid?

A{"name": "Eve", "age": 29}
B{name: "Eve", age: 29}
C{"name": "Eve" "age": 29}
D{"name": "Eve", "age": 29,}
Attempts:
2 left
💡 Hint

Remember JSON syntax rules for commas and quotes.

🚀 Application
expert
2:30remaining
What happens if a PUT request omits required fields in the resource?

A REST API requires the fields name, age, and email for a user resource.

A client sends a PUT request with the body:

{"name": "Sam", "age": 40}

The email field is missing. What is the expected server behavior according to REST principles?

AThe server ignores the PUT and leaves the resource unchanged.
BThe server replaces the resource without <code>email</code>, possibly causing invalid data.
CThe server merges the data and keeps the old <code>email</code> value.
DThe server rejects the request with a 400 Bad Request error due to missing required fields.
Attempts:
2 left
💡 Hint

Think about data validation and the meaning of PUT replacing the entire resource.