0
0
Flaskframework~20 mins

Flask session object - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Session Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask session code?
Consider this Flask route that sets and reads session data. What will be the printed output when a user visits the route twice in the same browser session?
Flask
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'secret'

@app.route('/')
def index():
    if 'visits' in session:
        session['visits'] += 1
    else:
        session['visits'] = 1
    print(f"Visits: {session['visits']}")
    return f"You have visited {session['visits']} times."
AVisits: 1 on first visit, Visits: 2 on second visit
BKeyError on second visit
CVisits: 1 on both visits
DVisits: 0 on first visit, Visits: 1 on second visit
Attempts:
2 left
💡 Hint
Think about how session data persists across requests in the same browser session.
📝 Syntax
intermediate
1:30remaining
Which option correctly sets a value in Flask session?
You want to store the username 'alice' in the Flask session. Which code snippet does this correctly?
Asession.set('username', 'alice')
Bsession['username'] = 'alice'
Csession.add('username', 'alice')
Dsession.username = 'alice'
Attempts:
2 left
💡 Hint
Remember, Flask session behaves like a dictionary.
🔧 Debug
advanced
2:00remaining
What error occurs with this Flask session code?
This Flask route tries to increment a session counter but raises an error. What is the error and why?
Flask
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'secret'

@app.route('/')
def index():
    session['count'] += 1
    return f"Count is {session['count']}"
AKeyError because 'count' key does not exist initially
BTypeError because session is not subscriptable
CRuntimeError because secret_key is missing
DNo error, outputs 'Count is 1' on first visit
Attempts:
2 left
💡 Hint
What happens if you try to increment a key that is not yet in the session?
state_output
advanced
2:30remaining
What is the session content after these requests?
Given this Flask app, what is the content of session after visiting /set and then /increment?
Flask
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'secret'

@app.route('/set')
def set_value():
    session['counter'] = 5
    return 'Counter set to 5'

@app.route('/increment')
def increment():
    session['counter'] += 1
    return f"Counter incremented to {session['counter']}"
A{'counter': 5}
B{} (empty session)
CKeyError on /increment
D{'counter': 6}
Attempts:
2 left
💡 Hint
Think about the order of requests and how session data persists.
🧠 Conceptual
expert
3:00remaining
Why must Flask app have a secret_key to use session?
Flask sessions store data client-side in cookies. Why is setting app.secret_key required for sessions to work securely?
Asecret_key stores the session data on the server securely
Bsecret_key encrypts the session data so users cannot read it
Csecret_key is used to cryptographically sign session cookies to prevent tampering
Dsecret_key is only needed for HTTPS connections
Attempts:
2 left
💡 Hint
Think about how Flask protects session data stored in cookies.