0
0
Flaskframework~20 mins

Session lifetime in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Session Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Flask session lifetime default behavior
In Flask, what is the default behavior of the session lifetime if you do not explicitly set PERMANENT_SESSION_LIFETIME?
AThe session expires after 5 minutes automatically.
BThe session lasts only until the browser is closed (non-permanent session).
CThe session lasts for 31 days by default.
DThe session lasts forever until manually cleared.
Attempts:
2 left
💡 Hint
Think about what happens when you close your browser without setting anything.
state_output
intermediate
2:00remaining
Effect of setting session.permanent to True
Given this Flask code snippet, what will be the lifetime of the session cookie if PERMANENT_SESSION_LIFETIME is set to 1 day and session.permanent = True is set?

from flask import Flask, session
from datetime import timedelta

app = Flask(__name__)
app.secret_key = 'secret'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=1)

@app.route('/')
def index():
    session.permanent = True
    session['user'] = 'Alice'
    return 'Session set'
AThe session cookie will never expire.
BThe session cookie will expire when the browser is closed.
CThe session cookie will expire after 1 day, even if the browser is closed.
DThe session cookie will expire immediately after the response.
Attempts:
2 left
💡 Hint
Setting session.permanent = True changes the cookie type.
🔧 Debug
advanced
2:00remaining
Why does session not expire as expected?
A developer sets app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=5) and session.permanent = True in their Flask app. But the session still lasts longer than 5 minutes. What is the most likely reason?
AThe session cookie is not being updated on each request, so expiration is not refreshed.
BThe developer forgot to set a secret key, so sessions are not saved.
CThe server's system clock is incorrect, causing expiration times to be wrong.
DFlask does not support session expiration shorter than 10 minutes.
Attempts:
2 left
💡 Hint
Think about how session expiration is refreshed in Flask.
📝 Syntax
advanced
2:00remaining
Correct way to set session lifetime in Flask
Which of the following code snippets correctly sets the Flask session lifetime to 2 hours?
Aapp.session_lifetime = timedelta(hours=2)
Bapp.config['SESSION_LIFETIME'] = 2
Csession.lifetime = timedelta(hours=2)
Dapp.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=2)
Attempts:
2 left
💡 Hint
Check the exact config key Flask uses for session lifetime.
component_behavior
expert
3:00remaining
Session lifetime behavior with multiple requests
Consider a Flask app with PERMANENT_SESSION_LIFETIME set to 10 minutes and session.permanent = True. A user makes a request at 12:00, then another at 12:08, and then at 12:15. Assuming the session cookie is updated on each request, at what time will the session expire if the user stops making requests after 12:15?
AThe session will expire at 12:25, which is 10 minutes after the last request.
BThe session will expire at 12:10, which is 10 minutes after the first request.
CThe session will expire at 12:15, immediately after the last request.
DThe session will never expire because it is permanent.
Attempts:
2 left
💡 Hint
Think about how Flask refreshes the expiration time on each request.