Bird
0
0

Which of the following is the correct way to read an environment variable named SECRET_KEY with a default fallback in Flask?

easy📝 Syntax Q12 of 15
Flask - Deployment
Which of the following is the correct way to read an environment variable named SECRET_KEY with a default fallback in Flask?
Aos.getenv('SECRET_KEY', 'default_key')
Bos.environ['SECRET_KEY', 'default_key']
Cos.get('SECRET_KEY', 'default_key')
Dos.environ['SECRET_KEY'] or 'default_key'
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct function to get env variable with default

    Use os.getenv with two arguments: variable name and default value.
  2. Step 2: Check each option syntax

    A raises KeyError if missing despite the fallback attempt. B uses invalid tuple syntax for dict access. C uses non-existent os.get. D: os.getenv('SECRET_KEY', 'default_key') is correct.
  3. Final Answer:

    os.getenv('SECRET_KEY', 'default_key') -> Option A
  4. Quick Check:

    Use os.getenv with default [OK]
Quick Trick: Use os.getenv('VAR', 'default') to read env vars safely [OK]
Common Mistakes:
MISTAKES
  • Using os.environ['KEY'] or default which still raises KeyError if missing
  • Using non-existent os.get function
  • Using invalid tuple syntax with os.environ

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes