Bird
0
0

You have a module settings.py with a variable mode = 'default'. In your main script, you write:

hard📝 Application Q8 of 15
Python - Modules and Code Organization
You have a module settings.py with a variable mode = 'default'. In your main script, you write:
import settings
settings.mode = 'custom'
import settings
print(settings.mode)

What will be printed and why?
A'default' because the second import resets the module
B'custom' because the module is cached and not reloaded
CError because variable reassignment is not allowed
D'default' because import always reloads the module
Step-by-Step Solution
Solution:
  1. Step 1: First import

    The module settings is imported and mode is set to 'default'.
  2. Step 2: Variable reassignment

    The variable settings.mode is reassigned to 'custom' in the main script.
  3. Step 3: Second import

    The second import settings does not reload the module but uses the cached module object, so the reassigned value remains.
  4. Step 4: Print statement

    Printing settings.mode outputs 'custom'.
  5. Final Answer:

    'custom' because the module is cached and not reloaded -> Option B
  6. Quick Check:

    Modules are imported once and cached [OK]
Quick Trick: Module imports are cached; reassigned variables persist [OK]
Common Mistakes:
  • Assuming second import reloads module
  • Expecting original variable value after reassignment
  • Thinking reassignment causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes