Bird
Raised Fist0

You have a module config.py with a variable setting = 5. In your main program, you do:

hard🚀 Application Q15 of Q15
Python - Modules and Code Organization
You have a module config.py with a variable setting = 5. In your main program, you do:

import config config.setting = 10 import config print(config.setting)

What will be printed and why?
A10, because the module is loaded once and changes persist.
BError, because you cannot assign to module variables.
C5, because the second import reloads the module resetting variables.
DNone, because the variable is not accessible after import.
Step-by-Step Solution
Solution:
  1. Step 1: Understand module import caching

    Python loads a module once and caches it; subsequent imports reuse the same module object.
  2. Step 2: Analyze variable assignment and import

    Changing config.setting to 10 modifies the cached module. The second import does not reload, so the change remains.
  3. Final Answer:

    10, because the module is loaded once and changes persist. -> Option A
  4. Quick Check:

    Module cached; changes persist = C [OK]
Quick Trick: Module imports cache; variable changes stay across imports [OK]
Common Mistakes:
MISTAKES
  • Assuming second import reloads module
  • Thinking module variables are immutable
  • Believing variable becomes inaccessible

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes