Bird
0
0

If the environment variable SECRET_KEY is not set, what will happen with this code?

medium📝 Predict Output Q5 of 15
Django - Deployment and Production
If the environment variable SECRET_KEY is not set, what will happen with this code?
import os
SECRET_KEY = os.environ['SECRET_KEY']
ASets SECRET_KEY to default value
BSets SECRET_KEY to None
CSets SECRET_KEY to empty string
DRaises a KeyError exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand os.environ behavior

    Accessing a missing key in os.environ raises KeyError.
  2. Step 2: Check alternatives

    os.getenv returns None or default if missing, but os.environ[] does not.
  3. Final Answer:

    Raises a KeyError exception -> Option D
  4. Quick Check:

    Missing env key with os.environ[] = KeyError [OK]
Quick Trick: os.environ['KEY'] fails if key missing, use os.getenv() [OK]
Common Mistakes:
MISTAKES
  • Assuming it returns None or empty string
  • Confusing os.environ with os.getenv
  • Expecting default value without specifying

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes