Python Flask Framework Quiz
A 60-question quiz covering the Flask microframework, from app initialization and routing to templates, blueprints, and error handling.
Question 1
How do you create a basic Flask application instance?
from flask import Flask
app = ____(__name__)
Question 2
What is the purpose of `if __name__ == '__main__':` in a simple Flask script?
if __name__ == '__main__':
app.run(debug=True)
Question 3
What does `app.run(debug=True)` enable?
Question 4
Which variable is used to determine the root path of the application?
Question 5
What is the 'Application Factory' pattern?
Question 6
Which object acts as a proxy to the currently active application instance?
Question 7
How do you define a route for the root URL '/'?
____('/')
def index():
return 'Hello'
Question 8
How do you capture a dynamic string segment from the URL?
@app.route('/user/<____>')
def profile(username):
return f'User: {username}'
Question 9
Which converter allows capturing a variable that includes slashes (e.g., a file path)?
Question 10
How do you restrict a route to only accept POST requests?
@app.route('/login', methods=[____])
def login(): ...
Question 11
What function generates a URL for a specific view function?
Question 12
What happens if two routes match the same URL?
Question 13
How do you access the query parameters (e.g., `?key=value`) from a URL?
Question 14
Where is data from a POST form stored?
Question 15
How do you access JSON data sent in the request body?
Question 16
Which object is `request`?
Question 17
How do you access uploaded files?
Question 18
What is the type of `request.method`?
Question 19
How do you return a simple JSON response from a view?
Question 20
Which function redirects the user to another endpoint?
Question 21
How do you set a custom status code, e.g., 404?
Question 22
What does `make_response()` do?
Question 23
How do you set a cookie on a response?
resp = make_response("Hello")
resp.____('username', 'john')
return resp
Question 24
What is the default status code if none is specified?
Question 25
Where does Flask look for templates by default?
Question 26
Which function renders a Jinja2 template?
Question 27
How do you pass a variable `user` to the template?
return render_template('index.html', ____='John')
Question 28
What syntax allows you to execute Python-like logic (loops, if) in Jinja2?
Question 29
How do you safely output a variable that might contain HTML characters (auto-escaping)?
Question 30
Which block is typically used to define the main content area in a base template?
{% ____ 'content' %}{% endblock %}
Question 31
Which folder is used for static files (CSS, JS, images) by default?
Question 32
How do you generate the URL for a static file 'style.css' in a template?
Question 33
Can you serve static files from a different folder?
Question 34
What is the purpose of the `static_url_path` argument?
Question 35
Why should you use `url_for` for static files instead of hardcoding paths?
Question 36
Does Flask serve static files in production?
Question 37
Which variable is automatically available in all templates without passing it explicitly?
Question 38
How do you add a custom function to be available in all templates?
Question 39
What does `g` represent in the template context?
Question 40
How do you access the `session` object in a template?
Question 41
What is a 'filter' in Jinja2?
Question 42
How do you register a custom filter?
@app.____('reverse')
def reverse_filter(s):
return s[::-1]
Question 43
What is the primary purpose of Blueprints in Flask?
Question 44
How do you create a Blueprint?
from flask import Blueprint
bp = Blueprint('auth', __name__, url_prefix='/auth')
Question 45
How do you register a blueprint with the application?
Question 46
If a blueprint has `url_prefix='/admin'`, and defines a route `/login`, what is the full URL?
Question 47
How do you refer to a route named 'login' inside a blueprint named 'auth' using `url_for`?
Question 48
Can Blueprints have their own templates and static folders?
Question 49
Which attribute of the `app` object holds the configuration?
Question 50
How do you load configuration from a Python file?
Question 51
What is the convention for configuration variable names?
Question 52
Why is `SECRET_KEY` important?
Question 53
How do you access a config variable inside a view?
Question 54
Which method allows loading config from environment variables?
Question 55
How do you register a custom error handler for 404 Not Found?
@app.____(404)
def page_not_found(e):
return render_template('404.html'), 404
Question 56
What argument does an error handler function receive?
Question 57
How do you manually trigger a 404 error?
Question 58
Can you handle generic Python exceptions (like `ValueError`) with `errorhandler`?
Question 59
What happens if no error handler is defined for a 500 error?
Question 60
How do you return a JSON error response instead of HTML?
