Bird
0
0

Consider this Python Flask code snippet for a REST API endpoint:

medium📝 Predict Output Q13 of 15
Rest API - HTTP Status Codes
Consider this Python Flask code snippet for a REST API endpoint:
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/data')
def get_data():
    try:
        result = 10 / 0  # This will cause an error
        return jsonify({'result': result})
    except Exception:
        return jsonify({'error': 'Server error occurred'}), 500

if __name__ == '__main__':
    app.run()

What will the client receive when requesting /data?
AA JSON response with {'result': 0} and status 200.
BA JSON response with {'error': 'Server error occurred'} and status 404.
CA JSON response with {'error': 'Server error occurred'} and status 500.
DA server crash with no response.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the code's try-except block

    The code tries to divide 10 by 0, which raises a ZeroDivisionError, triggering the except block.
  2. Step 2: Check the except block response

    The except block returns a JSON error message with status code 500 indicating an internal server error.
  3. Final Answer:

    A JSON response with {'error': 'Server error occurred'} and status 500. -> Option C
  4. Quick Check:

    Exception triggers 500 error response [OK]
Quick Trick: Errors in try block trigger 500 response in except [OK]
Common Mistakes:
MISTAKES
  • Expecting division by zero to return 0
  • Confusing 404 with 500 status code
  • Assuming server crashes without response

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes