Python Flask Framework Quiz

Python
0 Passed
0% acceptance

A 60-question quiz covering the Flask microframework, from app initialization and routing to templates, blueprints, and error handling.

60 Questions
~120 minutes
1

Question 1

How do you create a basic Flask application instance?

javascript

from flask import Flask
app = ____(__name__)
                
A
new Flask
B
Flask
C
create_app
D
Application
2

Question 2

What is the purpose of `if __name__ == '__main__':` in a simple Flask script?

javascript

if __name__ == '__main__':
    app.run(debug=True)
                
A
It ensures the app runs only when executed directly, not when imported.
B
It is required to connect to the database.
C
It defines the main route.
D
It initializes the WSGI server for production.
3

Question 3

What does `app.run(debug=True)` enable?

A
Faster performance.
B
The interactive debugger and automatic reloader.
C
Database migrations.
D
HTTPS support.
4

Question 4

Which variable is used to determine the root path of the application?

A
app.root_path
B
app.instance_path
C
app.base_dir
D
sys.path
5

Question 5

What is the 'Application Factory' pattern?

A
A design pattern where you create the app in a function rather than globally.
B
A factory that produces HTML.
C
A plugin for manufacturing data.
D
The default way Flask works.
6

Question 6

Which object acts as a proxy to the currently active application instance?

A
flask.g
B
flask.current_app
C
flask.request
D
flask.app
7

Question 7

How do you define a route for the root URL '/'?

javascript

____('/')
def index():
    return 'Hello'
                
A
@app.route
B
@app.path
C
@app.url
D
@index.route
8

Question 8

How do you capture a dynamic string segment from the URL?

javascript

@app.route('/user/<____>')
def profile(username):
    return f'User: {username}'
                
A
str:username
B
username
C
?P<username>
D
:username
9

Question 9

Which converter allows capturing a variable that includes slashes (e.g., a file path)?

A
<string:path>
B
<path:path>
C
<file:path>
D
<any:path>
10

Question 10

How do you restrict a route to only accept POST requests?

javascript

@app.route('/login', methods=[____])
def login(): ...
                
A
'POST'
B
POST
C
methods.POST
D
only_post=True
11

Question 11

What function generates a URL for a specific view function?

A
redirect()
B
url_for()
C
get_url()
D
route_to()
12

Question 12

What happens if two routes match the same URL?

A
Flask raises an error.
B
The one defined first takes precedence.
C
The one defined last takes precedence.
D
Both are executed.
13

Question 13

How do you access the query parameters (e.g., `?key=value`) from a URL?

A
request.form
B
request.args
C
request.query
D
request.params
14

Question 14

Where is data from a POST form stored?

A
request.data
B
request.json
C
request.form
D
request.body
15

Question 15

How do you access JSON data sent in the request body?

A
request.get_json()
B
request.json_body
C
request.read_json()
D
json.load(request)
16

Question 16

Which object is `request`?

A
A global variable that is always available.
B
A thread-local object that points to the current request.
C
A parameter passed to every view function.
D
A static class.
17

Question 17

How do you access uploaded files?

A
request.files
B
request.form
C
request.uploads
D
request.input
18

Question 18

What is the type of `request.method`?

A
Integer
B
String (e.g., 'GET', 'POST')
C
Boolean
D
Enum
19

Question 19

How do you return a simple JSON response from a view?

A
return {'key': 'value'}
B
return json.dumps({'key': 'value'})
C
return jsonify({'key': 'value'})
D
Both A and C
20

Question 20

Which function redirects the user to another endpoint?

A
return redirect('/home')
B
return route('/home')
C
return move('/home')
D
return render_template('/home')
21

Question 21

How do you set a custom status code, e.g., 404?

A
return 'Error', 404
B
return 404, 'Error'
C
return Response('Error', status=404)
D
Both A and C
22

Question 22

What does `make_response()` do?

A
It sends the response immediately.
B
It creates a Response object that you can modify (e.g., set headers/cookies) before returning.
C
It renders a template.
D
It creates a request.
23

Question 23

How do you set a cookie on a response?

javascript

resp = make_response("Hello")
resp.____('username', 'john')
return resp
                
A
add_cookie
B
set_cookie
C
cookie
D
put_cookie
24

Question 24

What is the default status code if none is specified?

A
200 OK
B
201 Created
C
302 Found
D
0
25

Question 25

Where does Flask look for templates by default?

A
In the `views` folder.
B
In the `templates` folder in the app root.
C
In the `html` folder.
D
Anywhere.
26

Question 26

Which function renders a Jinja2 template?

A
render_file()
B
render_template()
C
template()
D
view()
27

Question 27

How do you pass a variable `user` to the template?

javascript

return render_template('index.html', ____='John')
                
A
user
B
context
C
var
D
value
28

Question 28

What syntax allows you to execute Python-like logic (loops, if) in Jinja2?

A
{{ ... }}
B
{% ... %}
C
{# ... #}
D
<% ... %>
29

Question 29

How do you safely output a variable that might contain HTML characters (auto-escaping)?

A
Jinja2 auto-escapes by default.
B
You must use the `|escape` filter manually.
C
You must use `safe`.
D
It is not possible.
30

Question 30

Which block is typically used to define the main content area in a base template?

javascript

{% ____ 'content' %}{% endblock %}
                
A
define
B
section
C
block
D
area
31

Question 31

Which folder is used for static files (CSS, JS, images) by default?

A
assets
B
public
C
static
D
resources
32

Question 32

How do you generate the URL for a static file 'style.css' in a template?

A
{{ url_for('static', filename='style.css') }}
B
{{ static('style.css') }}
C
/static/style.css
D
{{ url('static/style.css') }}
33

Question 33

Can you serve static files from a different folder?

A
No, it must be 'static'.
B
Yes, by passing `static_folder` to the Flask constructor.
C
Yes, by changing the database settings.
D
Only in production.
34

Question 34

What is the purpose of the `static_url_path` argument?

A
It changes the URL prefix (e.g., from /static to /assets).
B
It changes the folder on disk.
C
It disables static files.
D
It sets the file permissions.
35

Question 35

Why should you use `url_for` for static files instead of hardcoding paths?

A
It is faster.
B
It handles caching (cache busting) and path changes automatically.
C
It encrypts the file.
D
It compresses the file.
36

Question 36

Does Flask serve static files in production?

A
Yes, it is high performance.
B
It can, but it is recommended to use a dedicated web server (Nginx/Apache) for better performance.
C
No, it crashes.
D
Only images.
37

Question 37

Which variable is automatically available in all templates without passing it explicitly?

A
db
B
request
C
admin
D
models
38

Question 38

How do you add a custom function to be available in all templates?

A
@app.template_global()
B
@app.route()
C
@app.context_processor
D
Both A and C
39

Question 39

What does `g` represent in the template context?

A
Global application config.
B
A namespace for holding data during a single request cycle.
C
Google API.
D
Graphics.
40

Question 40

How do you access the `session` object in a template?

A
{{ session['key'] }}
B
{{ request.session['key'] }}
C
{{ cookie['key'] }}
D
It is not available.
41

Question 41

What is a 'filter' in Jinja2?

A
A function that transforms a variable, used via pipe `|`.
B
A database query.
C
A security feature.
D
A way to hide elements.
42

Question 42

How do you register a custom filter?

javascript

@app.____('reverse')
def reverse_filter(s):
    return s[::-1]
                
A
filter
B
template_filter
C
add_filter
D
jinja_filter
43

Question 43

What is the primary purpose of Blueprints in Flask?

A
To draw UI designs.
B
To organize a large application into smaller, reusable modules.
C
To print the code.
D
To handle database migrations.
44

Question 44

How do you create a Blueprint?

javascript

from flask import Blueprint
bp = Blueprint('auth', __name__, url_prefix='/auth')
                
A
It creates a new Flask app.
B
It creates a component that records operations to be executed later when registered on an app.
C
It creates a database table.
D
It creates a template.
45

Question 45

How do you register a blueprint with the application?

A
app.add(bp)
B
app.register_blueprint(bp)
C
bp.register(app)
D
app.include(bp)
46

Question 46

If a blueprint has `url_prefix='/admin'`, and defines a route `/login`, what is the full URL?

A
/login
B
/admin/login
C
/adminlogin
D
/login/admin
47

Question 47

How do you refer to a route named 'login' inside a blueprint named 'auth' using `url_for`?

A
url_for('login')
B
url_for('auth.login')
C
url_for('/auth/login')
D
url_for('auth_login')
48

Question 48

Can Blueprints have their own templates and static folders?

A
No, they must share the app's folders.
B
Yes, using `template_folder` and `static_folder` arguments.
C
Only static folders.
D
Only templates.
49

Question 49

Which attribute of the `app` object holds the configuration?

A
app.settings
B
app.conf
C
app.config
D
app.cfg
50

Question 50

How do you load configuration from a Python file?

A
app.config.from_pyfile('config.py')
B
app.config.load('config.py')
C
app.load_config('config.py')
D
import config
51

Question 51

What is the convention for configuration variable names?

A
camelCase
B
snake_case
C
UPPERCASE
D
kebab-case
52

Question 52

Why is `SECRET_KEY` important?

A
It is the database password.
B
It is used to sign session cookies and other cryptographic data.
C
It is the admin login.
D
It encrypts the HTML.
53

Question 53

How do you access a config variable inside a view?

A
app.config['KEY']
B
current_app.config['KEY']
C
Both A and B
D
config.get('KEY')
54

Question 54

Which method allows loading config from environment variables?

A
app.config.from_envvar()
B
app.config.from_os()
C
app.config.get_env()
D
It happens automatically.
55

Question 55

How do you register a custom error handler for 404 Not Found?

javascript

@app.____(404)
def page_not_found(e):
    return render_template('404.html'), 404
                
A
route
B
errorhandler
C
handle_error
D
catch
56

Question 56

What argument does an error handler function receive?

A
The request object.
B
The exception or error object.
C
Nothing.
D
The response object.
57

Question 57

How do you manually trigger a 404 error?

A
return 404
B
raise 404
C
abort(404)
D
stop(404)
58

Question 58

Can you handle generic Python exceptions (like `ValueError`) with `errorhandler`?

A
Yes, `@app.errorhandler(ValueError)`.
B
No, only HTTP codes.
C
Only if debug is True.
D
Only custom exceptions.
59

Question 59

What happens if no error handler is defined for a 500 error?

A
The server crashes.
B
Flask shows a generic 'Internal Server Error' page.
C
It redirects to home.
D
It shows a blank page.
60

Question 60

How do you return a JSON error response instead of HTML?

A
It is automatic.
B
You must check `request.accept_mimetypes` or use a custom error handler that returns `jsonify()`.
C
Set `app.config['JSON_ERRORS'] = True`.
D
Use `@app.json_errorhandler`.

QUIZZES IN Python