0
0
Flaskframework~20 mins

Custom status codes in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Status Code Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the HTTP status code returned by this Flask route?
Consider this Flask route that returns a custom status code.

What status code will the client receive when accessing this route?
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/custom')
def custom_status():
    return jsonify(message='Hello'), 202

# Assume app is running and /custom is accessed
A202
B404
C200
D500
Attempts:
2 left
💡 Hint
Look at the second value returned by the route function.
📝 Syntax
intermediate
1:30remaining
Which option correctly sets a custom status code in Flask?
You want to return a JSON response with a custom status code 418.
Which code snippet correctly does this?
Areturn jsonify(error='I am a teapot'), status=418
Breturn jsonify(error='I am a teapot', status=418)
Creturn jsonify(error='I am a teapot'), 418
Dreturn jsonify(error='I am a teapot').status_code = 418
Attempts:
2 left
💡 Hint
Flask routes can return a tuple with response and status code.
🔧 Debug
advanced
2:00remaining
Why does this Flask route always return status 200 instead of 404?
Examine the code below:

```python @app.route('/item/') def get_item(id): item = find_item(id) if not item: return {'error': 'Not found'}, 404 return item ```
Despite returning 404 in the code, the client always receives 200. Why?
AThe function returns a dict directly which Flask converts to 200, ignoring the tuple in the if block.
BThe find_item function always returns a valid item, so 404 is never reached.
CFlask requires Response objects, not dicts, to set status codes.
DThe route decorator is missing a methods argument, causing default 200.
Attempts:
2 left
💡 Hint
Check what the return type is in each branch.
state_output
advanced
1:30remaining
What is the response status code after calling this Flask route twice?
Given this Flask app snippet:

```python from flask import Flask, jsonify app = Flask(__name__) counter = 0 @app.route('/count') def count(): global counter counter += 1 if counter > 1: return jsonify(message='Too many requests'), 429 return jsonify(message='First request'), 200 ```
What status code does the client receive on the second call to /count?
A200
B429
C500
D404
Attempts:
2 left
💡 Hint
Look at how the counter variable changes with each call.
🧠 Conceptual
expert
2:00remaining
Which statement about custom HTTP status codes in Flask is true?
Select the correct statement about using custom HTTP status codes in Flask responses.
AFlask automatically converts unknown status codes to 200 OK.
BFlask only supports standard HTTP status codes; custom codes cause errors.
CCustom status codes must be registered in Flask before use or they will raise exceptions.
DYou can return any integer as a status code, but some clients may not recognize non-standard codes.
Attempts:
2 left
💡 Hint
Think about HTTP protocol and client behavior.