CRUD stands for Create, Read, Update, and Delete. These are the four basic operations to manage data in an application.
Click to reveal answer
beginner
In Flask, which HTTP methods are commonly used for each CRUD operation?
Create: POST Read: GET Update: PUT or PATCH Delete: DELETE
Click to reveal answer
beginner
How do you define a route in Flask to handle a GET request for reading data?
Use the @app.route decorator with methods=['GET']. For example:
@app.route('/items', methods=['GET']) def get_items(): return 'List of items'
Click to reveal answer
intermediate
What is the purpose of the Flask request object in CRUD operations?
The request object holds data sent by the client, such as form data or JSON payloads. It helps to access input when creating or updating data.
Click to reveal answer
intermediate
Why is it important to handle errors in CRUD operations in Flask?
Handling errors ensures the app responds clearly when something goes wrong, like missing data or invalid input. It improves user experience and app stability.
Click to reveal answer
Which HTTP method is typically used to update existing data in a Flask app?
APOST
BGET
CPUT
DDELETE
✗ Incorrect
PUT is commonly used to update existing data, while POST is for creating new data.
What Flask decorator is used to define a route for a CRUD operation?
A@app.route
B@app.method
C@app.handle
D@app.request
✗ Incorrect
The @app.route decorator defines URL routes and HTTP methods in Flask.
Which CRUD operation corresponds to the HTTP DELETE method?
ACreate
BRead
CUpdate
DDelete
✗ Incorrect
DELETE is used to remove data from the server.
In Flask, how do you access JSON data sent in a POST request?
Arequest.json
Brequest.form
Crequest.args
Drequest.data
✗ Incorrect
request.json provides parsed JSON data sent by the client.
Why should you specify HTTP methods in Flask routes for CRUD?
ATo improve app speed
BTo restrict route to intended operations
CTo allow all methods by default
DTo enable database connections
✗ Incorrect
Specifying methods restricts routes to handle only intended CRUD operations, improving security and clarity.
Explain how you would implement the four CRUD operations in a Flask app, including which HTTP methods and Flask features you would use.
Think about how URLs and HTTP methods map to CRUD actions.
You got /7 concepts.
Describe why error handling is important in CRUD operations in Flask and how you might handle a missing resource when trying to update or delete.
Consider what happens if the user tries to change data that does not exist.
You got /5 concepts.
Practice
(1/5)
1. What does the 'CRUD' acronym stand for in Flask web development?
easy
A. Cache, Route, User, Data
B. Connect, Run, Upload, Download
C. Code, Render, Use, Deploy
D. Create, Read, Update, Delete
Solution
Step 1: Understand CRUD basics
CRUD is a common term in web development describing four basic operations on data.
Step 2: Match each letter to its meaning
C stands for Create, R for Read, U for Update, and D for Delete.
Final Answer:
Create, Read, Update, Delete -> Option D
Quick Check:
CRUD = Create, Read, Update, Delete [OK]
Hint: Remember CRUD as the four main data actions [OK]
Common Mistakes:
Confusing CRUD with unrelated terms
Mixing up the order of operations
Thinking CRUD refers to Flask-specific commands
2. Which HTTP method is typically used in Flask to update an existing resource in a CRUD API?
easy
A. PUT
B. POST
C. GET
D. DELETE
Solution
Step 1: Identify HTTP methods for CRUD
GET is for reading, POST for creating, PUT for updating, DELETE for deleting.
Step 2: Match update operation
Updating a resource uses the PUT method in RESTful APIs like Flask.
Final Answer:
PUT -> Option A
Quick Check:
Update = PUT method [OK]
Hint: Update uses PUT method in REST APIs [OK]
Common Mistakes:
Using POST instead of PUT for updates
Confusing GET with update actions
Using DELETE for updating data
3. Given this Flask route code snippet, what will be the output when a DELETE request is sent to /item/5?
from flask import Flask, jsonify
app = Flask(__name__)
items = {1: 'apple', 5: 'banana'}
@app.route('/item/', methods=['DELETE'])
def delete_item(id):
if id in items:
del items[id]
return jsonify({'message': 'Deleted'}), 200
else:
return jsonify({'error': 'Not found'}), 404
medium
A. {"error": "Not found"} with status 404
B. {"message": "Deleted"} with status 200
C. SyntaxError due to missing return statement
D. 500 Internal Server Error
Solution
Step 1: Check if item with id 5 exists
The dictionary items contains key 5 with value 'banana'.
Step 2: Analyze DELETE request handling
Since id 5 exists, it is deleted and a JSON message 'Deleted' with status 200 is returned.
Final Answer:
{"message": "Deleted"} with status 200 -> Option B
Quick Check:
Item 5 exists, so deletion returns success message [OK]
Hint: Check if key exists before delete to decide response [OK]
Common Mistakes:
Assuming item does not exist and returning 404
Forgetting to delete the item before returning
Confusing status codes for success and error
4. Identify the error in this Flask route for creating a new item:
The code uses request.json but does not import request from flask.
Step 2: Verify data structure usage
Assuming items is a list, append is valid; HTTP method POST is correct; status 201 is correct.
Final Answer:
Missing import of request from flask -> Option A
Quick Check:
request must be imported to access request.json [OK]
Hint: Always import request to access JSON data in Flask [OK]
Common Mistakes:
Forgetting to import request
Using append on a dict instead of list
Using GET instead of POST for creation
5. You want to implement a Flask route to update an item only if it exists, otherwise return a 404 error. Which code snippet correctly performs this update using JSON data sent in a PUT request?
A)
@app.route('/item/', methods=['PUT'])
def update_item(id):
if id not in items:
return jsonify({'error': 'Not found'}), 404
data = request.json
items[id] = data['name']
return jsonify({'message': 'Updated'}), 200
C)
@app.route('/item/', methods=['POST'])
def update_item(id):
data = request.json
if id in items:
items[id] = data['name']
return jsonify({'message': 'Updated'}), 200
D)
@app.route('/item/', methods=['PUT'])
def update_item(id):
data = request.json
if id in items:
items[id] = data['name']
else:
items[id] = 'default'
return jsonify({'message': 'Updated'}), 200
hard
A. Updates without checking existence, always returns 200
B. Uses POST method incorrectly for update
C. Checks existence, updates if found, else returns 404
D. Adds default if not found instead of 404 error
Solution
Step 1: Verify HTTP method and existence check
Checks existence, updates if found, else returns 404 uses PUT method and checks if id exists before updating.
Step 2: Confirm correct response for missing item
If id not found, it returns 404 error, which is correct behavior for update.
Final Answer:
Checks existence, updates if found, else returns 404 -> Option C
Quick Check:
Update with existence check returns 404 if missing [OK]
Hint: Always check if item exists before update to avoid errors [OK]
Common Mistakes:
Not checking if item exists before update
Using POST instead of PUT for update
Adding default values instead of error on missing item
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.