Bird
0
0

Given this Flask code snippet, what will be the output when accessing /status/task1 if tasks = {'task1': 'running'}?

medium📝 Command Output Q13 of 15
Flask - Background Tasks
Given this Flask code snippet, what will be the output when accessing /status/task1 if tasks = {'task1': 'running'}?
from flask import Flask, jsonify
app = Flask(__name__)
tasks = {'task1': 'running'}

@app.route('/status/<task_id>')
def status(task_id):
    state = tasks.get(task_id, 'not found')
    return jsonify({'task': task_id, 'status': state})
A{"task": "task1", "status": "running"}
B{"task": "task1", "status": "not found"}
C404 Not Found error
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Check the tasks dictionary for 'task1'

    The dictionary has 'task1' with value 'running'.
  2. Step 2: Understand the route function output

    The function returns JSON with task id and its status from the dictionary.
  3. Final Answer:

    {"task": "task1", "status": "running"} -> Option A
  4. Quick Check:

    tasks['task1'] = running [OK]
Quick Trick: Check dictionary key presence for status [OK]
Common Mistakes:
MISTAKES
  • Assuming missing key returns error instead of default
  • Confusing JSON string with Python dict
  • Expecting 404 error without explicit handling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes