Challenge - 5 Problems
Flask API Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask API endpoint?
Consider this Flask route that returns JSON data. What will the client receive when accessing
/api/data?Flask
from flask import Flask, jsonify app = Flask(__name__) @app.route('/api/data') def data(): return jsonify({'name': 'Alice', 'age': 30})
Attempts:
2 left
💡 Hint
Think about what jsonify does and what format the client receives.
✗ Incorrect
The jsonify function converts the dictionary into a JSON string with correct content-type. The client receives a JSON string, not a Python dict or a plain string.
📝 Syntax
intermediate2:00remaining
Which Flask route definition will cause a syntax error?
Identify the Flask route decorator that will cause a syntax error when defining an API endpoint.
Attempts:
2 left
💡 Hint
Check the indentation of the function body.
✗ Incorrect
Option A lacks indentation for the return statement, causing an IndentationError in Python.
❓ state_output
advanced2:00remaining
What is the response status code of this Flask API endpoint?
Given this Flask route, what status code will the client receive when accessing
/api/status?Flask
from flask import Flask, jsonify app = Flask(__name__) @app.route('/api/status') def status(): return jsonify({'status': 'ok'}), 201
Attempts:
2 left
💡 Hint
Look at the second value returned by the function.
✗ Incorrect
Returning a tuple with (response, status_code) sets the HTTP status code. Here it is 201 Created.
🔧 Debug
advanced2:00remaining
Why does this Flask API endpoint raise a TypeError?
Examine the code below. Why will calling
/api/error cause a TypeError?Flask
from flask import Flask app = Flask(__name__) @app.route('/api/error') def error(): return {'message': 'fail'}
Attempts:
2 left
💡 Hint
Flask needs a proper response object or string, not a raw dict.
✗ Incorrect
Returning a dict directly without jsonify causes Flask to raise a TypeError because it cannot convert dict to a valid response.
🧠 Conceptual
expert3:00remaining
Which option correctly implements a Flask API endpoint that accepts a URL parameter and returns it in JSON?
You want to create an API endpoint
/api/item/<id> that returns the id parameter as JSON. Which code snippet does this correctly?Attempts:
2 left
💡 Hint
Check the function parameters and route variable types carefully.
✗ Incorrect
Option B correctly defines the route with a string parameter and passes it to the function. It returns JSON using jsonify. Option B misses the id parameter in the function. Option B misses the id parameter and will cause a NameError. Option B returns a dict without jsonify, causing a TypeError.