Bird
0
0

Identify the error in this code snippet that tries to read an environment variable:

medium📝 Debug Q14 of 15
Python - Standard Library Usage
Identify the error in this code snippet that tries to read an environment variable:
import os
api_key = os.getenv('API_KEY')
print(api_key.upper())
Assume API_KEY is not set in the environment.
AAttributeError because api_key is null
BSyntaxError due to missing parentheses
CNo error, code runs fine
DNameError because os is not imported
Step-by-Step Solution
Solution:
  1. Step 1: Check os.getenv return when variable missing

    When API_KEY is not set, os.getenv returns null.
  2. Step 2: Understand method call on null

    Calling upper() on null causes an AttributeError because null has no such method.
  3. Final Answer:

    AttributeError because api_key is null -> Option A
  4. Quick Check:

    null.upper() causes AttributeError [OK]
Quick Trick: Check for null before calling string methods [OK]
Common Mistakes:
  • Assuming os.getenv returns empty string if missing
  • Ignoring that null has no string methods
  • Thinking code runs without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes