Bird
Raised Fist0
Flaskframework~20 mins

Custom status codes in Flask - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the correct way to return a custom status code 450 from a Flask route?
easy
A. return "Error", 450
B. return 450, "Error"
C. return Response(status=450)
D. return make_response(450, "Error")

Solution

  1. Step 1: Understand Flask return tuple format

    Flask routes return a tuple with (response_body, status_code) to set custom status codes.
  2. Step 2: Match the tuple with custom code 450

    Returning "Error", 450 matches the format (body, status_code) correctly.
  3. Final Answer:

    return "Error", 450 -> Option A
  4. Quick Check:

    Tuple (body, status) = return "Error", 450 [OK]
Hint: Return (body, code) tuple to set custom status [OK]
Common Mistakes:
  • Swapping order of status and body in return tuple
  • Using Response without import or wrong parameters
  • Using make_response incorrectly
2. Which of the following is the correct syntax to return a JSON response with a custom status code 499 in Flask?
easy
A. return jsonify(message="Fail"), 499
B. return jsonify(message="Fail", status=499)
C. return jsonify(message="Fail", 499)
D. return jsonify(message="Fail").status_code(499)

Solution

  1. Step 1: Use jsonify to create JSON response

    jsonify returns a Response object with JSON content.
  2. Step 2: Return tuple with jsonify and status code

    To set custom status code, return (jsonify(...), status_code) tuple.
  3. Final Answer:

    return jsonify(message="Fail"), 499 -> Option A
  4. Quick Check:

    jsonify + tuple (response, code) = return jsonify(message="Fail"), 499 [OK]
Hint: Return (jsonify(...), code) tuple for JSON + custom code [OK]
Common Mistakes:
  • Passing status code inside jsonify arguments
  • Trying to call status_code as method
  • Incorrect tuple order or syntax
3. What will be the HTTP status code returned by this Flask route?
from flask import Flask
app = Flask(__name__)

@app.route('/test')
def test():
    return "Custom", 499
medium
A. 404
B. 200
C. 499
D. 500

Solution

  1. Step 1: Check the return statement in route

    The route returns a tuple ("Custom", 499), where 499 is the status code.
  2. Step 2: Flask uses the second tuple element as status code

    Flask sets HTTP status code to 499 as specified in the return tuple.
  3. Final Answer:

    499 -> Option C
  4. Quick Check:

    Return tuple second value = status code 499 [OK]
Hint: Second tuple value in return sets status code [OK]
Common Mistakes:
  • Assuming default 200 status code
  • Confusing return order
  • Thinking 499 is invalid and defaults to 500
4. Identify the error in this Flask route that tries to return a custom status code 499:
from flask import Flask
app = Flask(__name__)

@app.route('/error')
def error():
    return "Oops", status=499
medium
A. Route function must return a Response object
B. Custom status code 499 is not allowed in Flask
C. Missing jsonify for JSON response
D. Using 'status' keyword argument in return is invalid

Solution

  1. Step 1: Check return statement syntax

    Flask does not accept 'status' keyword in return statement directly.
  2. Step 2: Correct way is to return tuple (body, status_code)

    Use return "Oops", 499 instead of return "Oops", status=499.
  3. Final Answer:

    Using 'status' keyword argument in return is invalid -> Option D
  4. Quick Check:

    Return tuple (body, code) not keyword args [OK]
Hint: Return tuple (body, code), not keyword args [OK]
Common Mistakes:
  • Using status=code in return instead of tuple
  • Assuming Flask auto-detects status from keywords
  • Confusing with Response object parameters
5. You want to create a Flask route that returns a JSON message with a custom status code 460 only if a query parameter 'valid' is 'no'. Otherwise, return status 200 with a success message. Which code correctly implements this?
hard
A. from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/check') def check(): if request.args.get('valid') == 'no': return jsonify(error="Invalid", status=460) return jsonify(message="OK")
B. from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/check') def check(): if request.args.get('valid') == 'no': return jsonify(error="Invalid"), 460 return jsonify(message="OK"), 200
C. from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/check') def check(): if request.args.get('valid') == 'no': return jsonify(error="Invalid").status_code = 460 return jsonify(message="OK")
D. from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/check') def check(): if request.args.get('valid') == 'no': return jsonify(error="Invalid") return jsonify(message="OK"), 200

Solution

  1. Step 1: Check conditional logic for query parameter

    Use request.args.get('valid') to check if it equals 'no'.
  2. Step 2: Return JSON with custom status code 460 if invalid

    Return (jsonify(...), 460) tuple to set custom status code correctly.
  3. Step 3: Return success JSON with status 200 otherwise

    Return (jsonify(...), 200) tuple for success response.
  4. Final Answer:

    Correct conditional returns with tuples for custom and normal codes -> Option B
  5. Quick Check:

    Return (jsonify, code) tuples for all cases [OK]
Hint: Return (jsonify(...), code) tuples for all responses [OK]
Common Mistakes:
  • Passing status inside jsonify arguments
  • Trying to set status_code attribute directly
  • Missing status code tuple on error response
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