0
0
Flaskframework~20 mins

CRUD operations in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CRUD Mastery in Flask
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask route when a GET request is made?

Consider this Flask route that handles a GET request to fetch all items from a list.

from flask import Flask, jsonify
app = Flask(__name__)

items = ["apple", "banana", "cherry"]

@app.route('/items', methods=['GET'])
def get_items():
    return jsonify(items)

What will the client receive as a response body?

Flask
from flask import Flask, jsonify
app = Flask(__name__)

items = ["apple", "banana", "cherry"]

@app.route('/items', methods=['GET'])
def get_items():
    return jsonify(items)
A["apple", "banana", "cherry"] as JSON array
BPlain text string: "apple, banana, cherry"
CPython list printed as string: "['apple', 'banana', 'cherry']"
DEmpty JSON object: {}
Attempts:
2 left
💡 Hint

Remember that jsonify converts Python objects to JSON format.

📝 Syntax
intermediate
2:00remaining
Which Flask route code correctly handles creating a new item with POST?

You want to add a new item sent in JSON format to the items list using a POST request.

Which code snippet correctly extracts the JSON data and appends the new item?

Flask
items = []
A
@app.route('/items', methods=['POST'])
def add_item():
    data = request.form
    items.append(data['item'])
    return jsonify({'message': 'Item added'})
B
@app.route('/items', methods=['POST'])
def add_item():
    data = request.json
    items.append(data.item)
    return jsonify({'message': 'Item added'})
C
@app.route('/items', methods=['POST'])
def add_item():
    data = request.get_json()
    items.append(data['item'])
    return jsonify({'message': 'Item added'})
D
@app.route('/items', methods=['POST'])
def add_item():
    data = request.get_data()
    items.append(data['item'])
    return jsonify({'message': 'Item added'})
Attempts:
2 left
💡 Hint

Use the method that parses JSON body from the request.

🔧 Debug
advanced
2:00remaining
Why does this Flask PUT route raise a KeyError?

Given this Flask route to update an item by index, it raises a KeyError when called.

items = ["apple", "banana", "cherry"]

@app.route('/items/', methods=['PUT'])
def update_item(index):
    data = request.get_json()
    items[index] = data['value']
    return jsonify({'message': 'Item updated'})

What is the most likely cause of the KeyError?

Flask
items = ["apple", "banana", "cherry"]
AThe JSON sent does not have the key 'value', causing KeyError
BFlask route decorator syntax is incorrect causing KeyError
Crequest.get_json() returns None causing KeyError
DThe index is out of range causing KeyError
Attempts:
2 left
💡 Hint

Check the keys in the JSON data sent in the request body.

state_output
advanced
2:00remaining
What is the state of the items list after these DELETE requests?

Starting with items = ['apple', 'banana', 'cherry', 'date'], these DELETE requests are made:

@app.route('/items/', methods=['DELETE'])
def delete_item(index):
    items.pop(index)
    return jsonify({'message': 'Deleted'})

# Requests:
# DELETE /items/1
# DELETE /items/2

What is the final content of items after both requests?

Flask
items = ['apple', 'banana', 'cherry', 'date']
A['apple', 'banana']
B['apple', 'cherry', 'date']
C['apple', 'date']
D['apple', 'cherry']
Attempts:
2 left
💡 Hint

Remember that list indexes shift after each pop.

🧠 Conceptual
expert
2:00remaining
Which statement best describes idempotency in RESTful CRUD operations?

In REST APIs, some HTTP methods are idempotent. Which statement correctly explains idempotency in the context of CRUD operations?

AIdempotency means the method always creates a new resource every time it is called.
BAn idempotent method can be called multiple times with the same effect as a single call, like PUT updating a resource.
CPOST is idempotent because it updates existing resources without creating duplicates.
DDELETE is not idempotent because it removes resources permanently.
Attempts:
2 left
💡 Hint

Think about what happens if you repeat the same request multiple times.