- A
The import statement in app.py is placed inside a function, so it is not executed.
Why wrong: Imports inside functions are executed when the function is called.
- B
The module was imported using 'from config import DATABASE_URL' which creates a separate copy.
Why wrong: Even with 'from', the module is executed once and the value is determined at that time.
- C
The environment variable is only read when the function is called, not at import time.
Why wrong: os.getenv is called at import time, so the value is set immediately.
- D
Python caches modules; config.py was imported earlier without the environment variable, and the cached version is reused.
Modules are cached in sys.modules. If config.py was imported previously, the cached version is used, and the code is not re-executed.
Quick Answer
The correct answer is that Python caches modules, so the cached version of config.py is reused even after the environment variable is set. This happens because Python stores imported modules in `sys.modules`, and when `config.py` was first imported—perhaps during test discovery—the `os.getenv('DATABASE_URL', 'localhost')` call executed before the environment variable existed, locking in the default value `'localhost'`. On the PCAP exam, this tests your understanding of module caching and the fact that top-level code in a module runs only once per interpreter session. A common trap is assuming that re-importing or setting the environment variable later will re-evaluate the `os.getenv` call, but Python’s import system simply retrieves the cached module object. To avoid this, always set environment variables before any imports that read them, or use lazy evaluation inside functions. Memory tip: “First import freezes the value—set your env before you import.”
PCAP Modules and Packages Practice Question
This PCAP practice question tests your understanding of modules and packages. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.
A developer has a module 'config.py' with the following content:
# config.py import os
DATABASE_URL = os.getenv('DATABASE_URL', 'localhost')
Another module 'app.py' imports config and uses DATABASE_URL. During testing, the environment variable is set correctly, but the import still uses the default value 'localhost'. What is the most likely reason?
Clue words in this question
Noticing these words before you look at the options changes how you read each choice.
Clue:
"most likely"Why it matters: Probability qualifier — the question wants the most probable cause or outcome, not a guaranteed one. Eliminate low-probability options.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
Python caches modules; config.py was imported earlier without the environment variable, and the cached version is reused.
Option D is correct because Python caches imported modules in `sys.modules`. If `config.py` was imported earlier in the test session (e.g., during test discovery or another import) before the environment variable `DATABASE_URL` was set, the cached module would retain the default value `'localhost'`. Subsequent imports, even after setting the environment variable, reuse the cached module, so `os.getenv('DATABASE_URL', 'localhost')` is not re-evaluated.
Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The import statement in app.py is placed inside a function, so it is not executed.
Why it's wrong here
Imports inside functions are executed when the function is called.
- ✗
The module was imported using 'from config import DATABASE_URL' which creates a separate copy.
Why it's wrong here
Even with 'from', the module is executed once and the value is determined at that time.
- ✗
The environment variable is only read when the function is called, not at import time.
Why it's wrong here
os.getenv is called at import time, so the value is set immediately.
- ✓
Python caches modules; config.py was imported earlier without the environment variable, and the cached version is reused.
Why this is correct
Modules are cached in sys.modules. If config.py was imported previously, the cached version is used, and the code is not re-executed.
Clue confirmation
The clue word "most likely" in the question point toward this answer.
Related concept
Read the scenario before looking for a memorised answer.
Common exam traps
Common exam trap: answer the scenario, not the keyword
Python Institute often tests the misconception that `from module import name` creates an independent copy, when in fact it only binds a reference to the same object, and the real issue is Python's module caching and the timing of environment variable reads.
Detailed technical explanation
How to think about this question
Python's module caching mechanism (`sys.modules`) ensures that a module is loaded only once per interpreter session. The `os.getenv` call in `config.py` is executed at module load time, binding the value to the module's global variable. If the environment variable is set after the module is cached, the cached value remains unchanged. This behavior is critical in testing frameworks like `pytest` where test discovery may import modules before environment setup, leading to stale values.
KKey Concepts to Remember
- Read the scenario before looking for a memorised answer.
- Find the constraint that changes the correct option.
- Eliminate answers that are true in general but not in this case.
TExam Day Tips
- Watch for words such as best, first, most likely and least administrative effort.
- Review why wrong options are wrong, not only why the correct option is correct.
Key takeaway
Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Real-world example
How this comes up in practice
A cloud solutions architect for a retail company is evaluating services for a new workload. The correct answer here reflects best practice for the specific scenario described — not a general cloud recommendation. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. Cloud exam questions reward reading the constraint carefully: the same technology can be right or wrong depending on the use case.
What to study next
Got this wrong? Here's your next step.
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
- →
Modules and Packages — study guide chapter
Learn the concepts, then practise the questions
- →
Modules and Packages practice questions
Targeted practice on this topic area only
- →
All PCAP questions
511 questions across all exam domains
- →
Certified Associate Python Programmer PCAP study guide
Full concept coverage aligned to exam objectives
- →
PCAP practice test guide
How to use practice tests most effectively before exam day
Related practice questions
Related PCAP practice-question pages
Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.
Modules and Packages practice questions
Practise PCAP questions linked to Modules and Packages.
Strings practice questions
Practise PCAP questions linked to Strings.
Object-Oriented Programming practice questions
Practise PCAP questions linked to Object-Oriented Programming.
Exceptions and File I/O practice questions
Practise PCAP questions linked to Exceptions and File I/O.
PCAP fundamentals practice questions
Practise PCAP questions linked to PCAP fundamentals.
PCAP scenario practice questions
Practise PCAP questions linked to PCAP scenario.
PCAP troubleshooting practice questions
Practise PCAP questions linked to PCAP troubleshooting.
Practice this exam
Start a free PCAP practice session
Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.
FAQ
Questions learners often ask
What does this PCAP question test?
Modules and Packages — This question tests Modules and Packages — Read the scenario before looking for a memorised answer..
What is the correct answer to this question?
The correct answer is: Python caches modules; config.py was imported earlier without the environment variable, and the cached version is reused. — Option D is correct because Python caches imported modules in `sys.modules`. If `config.py` was imported earlier in the test session (e.g., during test discovery or another import) before the environment variable `DATABASE_URL` was set, the cached module would retain the default value `'localhost'`. Subsequent imports, even after setting the environment variable, reuse the cached module, so `os.getenv('DATABASE_URL', 'localhost')` is not re-evaluated.
What should I do if I get this PCAP question wrong?
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
Are there clue words in this question I should notice?
Yes — watch for: "most likely". Probability qualifier — the question wants the most probable cause or outcome, not a guaranteed one. Eliminate low-probability options.
What is the key concept behind this question?
Read the scenario before looking for a memorised answer.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Last reviewed: Jun 30, 2026
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.