0
0
Flaskframework~20 mins

Context lifecycle execution in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Context Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Flask request context availability
In a Flask app, what will be the output of this code snippet when accessing the '/' route?

from flask import Flask, request
app = Flask(__name__)

@app.route('/')
def index():
    return f"Method used: {request.method}"
Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/')
def index():
    return f"Method used: {request.method}"
AMethod used: None
BMethod used: GET
CRuntimeError: Working outside of request context.
DMethod used: POST
Attempts:
2 left
💡 Hint
Think about the default HTTP method when you visit a URL in a browser.
lifecycle
intermediate
2:00remaining
Order of Flask context teardown
Which of the following statements correctly describes when Flask's teardown_request functions are called?
AAfter the response is sent to the client, even if an exception occurred during request handling.
BBefore the request function is called, to prepare the context.
COnly if the request function completes without exceptions.
DOnly when the Flask app is shutting down.
Attempts:
2 left
💡 Hint
Think about cleanup tasks that must run regardless of errors.
🔧 Debug
advanced
2:00remaining
Identifying context errors in Flask
What error will this Flask code raise when executed outside a request?

from flask import request
print(request.method)
Flask
from flask import request
print(request.method)
ANameError: name 'request' is not defined
BAttributeError: 'LocalProxy' object has no attribute 'method'
CRuntimeError: Working outside of request context.
DTypeError: 'method' is not callable
Attempts:
2 left
💡 Hint
Flask's request object requires an active request context to work.
state_output
advanced
2:00remaining
Flask application context variable behavior
Given this Flask code, what will be printed?

from flask import Flask, g
app = Flask(__name__)

with app.app_context():
    g.value = 10
    print(g.value)

try:
    print(g.value)
except Exception as e:
    print(type(e).__name__)
Flask
from flask import Flask, g
app = Flask(__name__)

with app.app_context():
    g.value = 10
    print(g.value)

try:
    print(g.value)
except Exception as e:
    print(type(e).__name__)
A
10
RuntimeError
B
10
AttributeError
C
10
10
D
RuntimeError
RuntimeError
Attempts:
2 left
💡 Hint
Consider when the application context is active and when it ends.
🧠 Conceptual
expert
2:00remaining
Understanding Flask context locals
Which statement best explains why Flask uses context locals like request and g instead of global variables?
ATo prevent any data from being stored during a request lifecycle.
BTo improve performance by caching data globally across all requests.
CTo allow global variables to be shared between different Flask apps in the same process.
DTo ensure each request has its own separate data, avoiding conflicts in concurrent requests.
Attempts:
2 left
💡 Hint
Think about what happens when many users use the app at the same time.