Bird
Raised Fist0

What will be the output of this code if the environment variable USER is set to alice?

medium📝 Predict Output Q13 of Q15
Python - Standard Library Usage
What will be the output of this code if the environment variable USER is set to alice?
import os
name = os.getenv('USER', 'guest')
print(name)
Aalice
BUSER
Cnull
Dguest
Step-by-Step Solution
Solution:
  1. Step 1: Understand os.getenv behavior

    os.getenv('USER', 'guest') returns the value of USER if set, else 'guest'.
  2. Step 2: Apply given environment variable value

    Since USER is set to 'alice', the function returns 'alice'.
  3. Final Answer:

    alice -> Option A
  4. Quick Check:

    os.getenv returns env var value if set [OK]
Quick Trick: os.getenv returns env var value or default [OK]
Common Mistakes:
MISTAKES
  • Assuming default is always returned
  • Printing the variable name instead of its value
  • Confusing null with default value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes