Bird
Raised Fist0
Flaskframework~20 mins

Session lifetime in Flask - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does setting session.permanent = True in a Flask app do?
easy
A. It makes the session use the permanent_session_lifetime duration.
B. It deletes the session immediately after the request ends.
C. It disables session cookies in the browser.
D. It resets the session lifetime to zero.

Solution

  1. Step 1: Understand session permanence in Flask

    Setting session.permanent = True tells Flask to keep the session alive for a set duration instead of ending it when the browser closes.
  2. Step 2: Connect to permanent_session_lifetime

    The session will then use the time defined in app.permanent_session_lifetime to decide how long it lasts.
  3. Final Answer:

    It makes the session use the permanent_session_lifetime duration. -> Option A
  4. Quick Check:

    session.permanent = True means use permanent_session_lifetime [OK]
Hint: Permanent session means use app.permanent_session_lifetime [OK]
Common Mistakes:
  • Thinking session.permanent deletes session immediately
  • Confusing permanent with disabling cookies
  • Assuming session resets lifetime to zero
2. Which of the following is the correct way to set a session lifetime of 30 minutes in Flask?
easy
A. app.permanent_session_lifetime = timedelta(minutes=30)
B. session.lifetime = 30
C. app.session_lifetime = 30
D. session.permanent_session_lifetime = 30

Solution

  1. Step 1: Identify the correct Flask attribute for session lifetime

    The Flask app uses app.permanent_session_lifetime to set how long permanent sessions last.
  2. Step 2: Use the correct data type

    This value must be a timedelta object from Python's datetime module, e.g., timedelta(minutes=30).
  3. Final Answer:

    app.permanent_session_lifetime = timedelta(minutes=30) -> Option A
  4. Quick Check:

    Use app.permanent_session_lifetime with timedelta [OK]
Hint: Use app.permanent_session_lifetime with timedelta object [OK]
Common Mistakes:
  • Setting session.lifetime instead of app.permanent_session_lifetime
  • Assigning integer instead of timedelta
  • Using session.permanent_session_lifetime which does not exist
3. Given this Flask code snippet, what will be the session lifetime if the user sets session.permanent = True and app.permanent_session_lifetime = timedelta(hours=1)?
from flask import Flask, session
from datetime import timedelta

app = Flask(__name__)
app.secret_key = 'secret'
app.permanent_session_lifetime = timedelta(hours=1)

@app.route('/')
def index():
    session.permanent = True
    session['user'] = 'Alice'
    return 'Session set'
medium
A. The session will expire immediately after the request.
B. The session will last 24 hours by default.
C. The session will last until the browser is closed.
D. The session will last 1 hour before expiring.

Solution

  1. Step 1: Check the session permanence setting

    The code sets session.permanent = True, so the session uses the app's permanent lifetime.
  2. Step 2: Identify the permanent session lifetime

    The app sets app.permanent_session_lifetime = timedelta(hours=1), so the session lasts 1 hour.
  3. Final Answer:

    The session will last 1 hour before expiring. -> Option D
  4. Quick Check:

    session.permanent True + app.permanent_session_lifetime = 1 hour [OK]
Hint: Permanent session uses app.permanent_session_lifetime duration [OK]
Common Mistakes:
  • Assuming session expires immediately after request
  • Thinking session lasts until browser closes when permanent=True
  • Assuming default 24 hours without setting
4. You wrote this Flask code but the session never expires even after 1 hour. What is the likely mistake?
from flask import Flask, session
from datetime import timedelta

app = Flask(__name__)
app.secret_key = 'secret'
app.permanent_session_lifetime = timedelta(hours=1)

@app.route('/')
def index():
    session['user'] = 'Bob'
    return 'Session set'
medium
A. The secret key is missing, so sessions don't expire.
B. You need to set app.session_lifetime instead.
C. You forgot to set session.permanent = True.
D. You must call session.save() to apply lifetime.

Solution

  1. Step 1: Check if session is marked permanent

    The code sets session['user'] but does not set session.permanent = True.
  2. Step 2: Understand session expiration behavior

    Without session.permanent = True, the session lasts only until the browser closes, ignoring permanent_session_lifetime.
  3. Final Answer:

    You forgot to set session.permanent = True. -> Option C
  4. Quick Check:

    session.permanent must be True to use permanent_session_lifetime [OK]
Hint: Set session.permanent = True to activate lifetime setting [OK]
Common Mistakes:
  • Assuming setting session key alone applies lifetime
  • Using app.session_lifetime which is invalid
  • Thinking secret key affects session expiration
  • Calling non-existent session.save() method
5. You want to create a Flask app where sessions last 2 hours, but only if the user chooses 'Remember me'. Otherwise, sessions should expire when the browser closes. Which code snippet correctly implements this?
hard
A. app.permanent_session_lifetime = timedelta(hours=2) @app.route('/login', methods=['POST']) def login(): session.permanent = True session['user'] = request.form['username'] return 'Logged in'
B. app.permanent_session_lifetime = timedelta(hours=2) @app.route('/login', methods=['POST']) def login(): if request.form.get('remember') == 'yes': session.permanent = True else: session.permanent = False session['user'] = request.form['username'] return 'Logged in'
C. app.permanent_session_lifetime = timedelta(hours=2) @app.route('/login', methods=['POST']) def login(): session.permanent = False session['user'] = request.form['username'] return 'Logged in'
D. app.session_lifetime = timedelta(hours=2) @app.route('/login', methods=['POST']) def login(): if request.form.get('remember') == 'yes': session.permanent = True session['user'] = request.form['username'] return 'Logged in'

Solution

  1. Step 1: Set the permanent session lifetime correctly

    Use app.permanent_session_lifetime = timedelta(hours=2) to define 2 hours for permanent sessions.
  2. Step 2: Control session permanence based on user choice

    Inside the login route, check if the user selected 'Remember me'. If yes, set session.permanent = True, else set it to False.
  3. Step 3: Assign the user to session and return response

    Store the username in session and return a confirmation message.
  4. Final Answer:

    app.permanent_session_lifetime = timedelta(hours=2) @app.route('/login', methods=['POST']) def login(): if request.form.get('remember') == 'yes': session.permanent = True else: session.permanent = False session['user'] = request.form['username'] return 'Logged in' -> Option B
  5. Quick Check:

    Conditional session.permanent with app.permanent_session_lifetime set [OK]
Hint: Set session.permanent True only if 'Remember me' chosen [OK]
Common Mistakes:
  • Not setting session.permanent based on user input
  • Using app.session_lifetime instead of app.permanent_session_lifetime
  • Always setting session.permanent True ignoring user choice
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard