B. json is not imported or defined; should use jsonify
C. Function name 'data' is invalid
D. Route URL must start with '/api/'
Solution
Step 1: Check function return statement
The code uses json({'key': 'value'}) but json is not imported or defined.
Step 2: Identify correct function for JSON response
Flask provides jsonify to return JSON responses, which is imported here.
Final Answer:
json is not imported or defined; should use jsonify -> Option B
Quick Check:
Use jsonify, not json, for JSON response [OK]
Hint: Use jsonify, not json, to return JSON in Flask [OK]
Common Mistakes:
Using json instead of jsonify
Forgetting to import jsonify
Assuming function names must follow special rules
5. You want to create a Flask API endpoint '/user/' that returns JSON with the user's id and a greeting message. Which code correctly implements this?
hard
A. @app.route('/user/')\ndef user():\n return jsonify({'id': id, 'message': 'Hello user'})
B. @app.route('/user/<id>')\ndef user(id):\n return {'id': id, 'message': 'Hello user ' + id}
C. @app.route('/user/')\ndef user(id):\n return jsonify({'id': id, 'message': f'Hello user {id}'})
D. @app.route('/user/')\ndef user(id):\n print({'id': id, 'message': f'Hello user {id}'})
Solution
Step 1: Check route URL and parameter type
The route uses '/user/' which correctly captures an integer parameter named id.
Step 2: Verify function parameters and return
The function accepts id and returns a JSON response using jsonify with the correct message format.
Step 3: Identify errors in other options
@app.route('/user/<id>')\ndef user(id):\n return {'id': id, 'message': 'Hello user ' + id} misses int type and returns a dict without jsonify; @app.route('/user/')\ndef user():\n return jsonify({'id': id, 'message': 'Hello user'}) misses id parameter; @app.route('/user/')\ndef user(id):\n print({'id': id, 'message': f'Hello user {id}'}) uses print instead of return.
Final Answer:
@app.route('/user/')\ndef user(id):\n return jsonify({'id': id, 'message': f'Hello user {id}'}) -> Option C
Quick Check:
Route with int param + jsonify return = A [OK]
Hint: Use in route and return jsonify with id [OK]
Common Mistakes:
Not specifying for integer parameter
Returning dict without jsonify
Using print instead of return in route
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.