Bird
0
0

Given this folder structure and files:

medium📝 Predict Output Q13 of 15
Python - Modules and Code Organization
Given this folder structure and files:
mypackage/
  __init__.py
  module.py

# __init__.py content:
print('Package imported')

# module.py content:
def greet():
    return 'Hello!'

# main.py content:
import mypackage
from mypackage import module
print(module.greet())

What will be the output when running main.py?
AHello!\nPackage imported
BPackage imported\nHello!
CHello!
DImportError
Step-by-Step Solution
Solution:
  1. Step 1: Understand import behavior

    When import mypackage runs, the code inside __init__.py executes, printing 'Package imported'.
  2. Step 2: Check subsequent import and function call

    Then from mypackage import module imports the module, and print(module.greet()) prints 'Hello!'.
  3. Final Answer:

    Package imported Hello! -> Option B
  4. Quick Check:

    Init runs first, then greet() output = A [OK]
Quick Trick: Code in __init__.py runs on package import [OK]
Common Mistakes:
  • Assuming __init__.py code does not run
  • Mixing order of printed lines
  • Expecting ImportError without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes