Softenant
Technologies
Python User-Defined Modules MCQs (25) — Answers at the End

Python User-Defined Modules MCQs (25)

Python Training in Vizag (Softenant)

1) Creating a module

# file: mathx.py
def add(a,b): return a+b
A. This file is a package
B. This file is a module
C. Not importable
D. Needs __init__.py

2) Simple import

# file: main.py
import mathx
print(mathx.add(2,3))
A. 5
B. NameError
C. ImportError
D. AttributeError

3) From-import binds name directly

from mathx import add
print(add(1,2))
A. 3
B. NameError: mathx
C. AttributeError: add
D. ImportError

4) Aliasing

import mathx as mx
print(mx.add(3,3))
A. 6
B. NameError: mathx
C. SyntaxError
D. ImportError

5) Module top-level code executes once per process

# file: countermod.py
print("loaded")
x = 1
# file: main.py
import countermod
import countermod
A. prints “loaded” twice
B. prints “loaded” once
C. never prints
D. ImportError

6) __name__ guard

# file: tool.py
def run(): print("run")
if __name__ == "__main__":
    run()
A. run() executes on import
B. run() executes only when run as script
C. SyntaxError
D. Always silent

7) Where Python looks for modules first?

A. Current working directory / script dir
B. Standard library only
C. Site-packages first
D. Internet

8) Role of sys.path

import sys
print(type(sys.path).__name__)
A. tuple of paths
B. list of paths
C. dict of paths
D. set of paths

9) Packages vs modules

A. Package is a directory (may have __init__.py); module is a single .py file
B. Module is a directory; package is a file
C. They’re identical
D. Only stdlib can be packages

10) Namespace package (PEP 420)

A. Requires __init__.py
B. Works without __init__.py
C. Only on Python 2
D. Not importable

11) Relative import

# inside pkg/sub/mod.py
from .util import helper
A. Absolute import
B. Relative import inside a package
C. SyntaxError
D. Only works from interactive REPL

12) __all__ controls star-import

# file: m.py
__all__ = ["foo"]
def foo(): pass
def bar(): pass
from m import *
print('foo' in dir(), 'bar' in dir())
A. True True
B. True False
C. False True
D. False False

13) Circular import symptom

A. Always harmless
B. Often raises AttributeError at import time
C. Always SyntaxError
D. Prevented by Python

14) Module cache

import sys, mathx
print('mathx' in sys.modules)
A. False
B. True
C. KeyError
D. Depends on OS

15) Reloading a module

import importlib, mathx
importlib.reload(mathx)
A. Re-executes module code
B. Duplicates module
C. Deletes sys.modules
D. Not allowed

16) __file__ attribute

import mathx
print(hasattr(mathx, "__file__"))
A. Usually True for filesystem modules
B. Always False
C. Only on Windows
D. Only for builtins

17) __package__ value

# in pkg/sub/mod.py
print(__package__)
A. “pkg.sub”
B. “pkg”
C. “”
D. None always

18) What goes in __pycache__?

A. Source files
B. Bytecode .pyc files
C. Logs
D. Virtualenvs

19) Wildcard imports are discouraged because…

A. They’re very slow
B. They pollute the namespace and hide origins
C. They delete variables
D. They disable reload

20) Importing a module runs its top-level code…

A. Every time you call a function
B. Once per interpreter process (until reloaded)
C. Never
D. Only if in __main__

21) Import package attribute

# pkg/__init__.py defines VERSION = "1.0"
import pkg
print(pkg.VERSION)
A. Prints 1.0
B. AttributeError
C. ImportError
D. None

22) Import order preference

A. Builtins → stdlib → site-packages → script dir
B. sys.meta_path finders resolve using sys.path order (script dir is early)
C. Random
D. Site-packages first always

23) Editing a module file after import

Changes are picked automatically
B. Must use importlib.reload (or restart)
C. Python watches files
D. Use del mathx to reimport

24) Importing package submodule

import pkg.sub.mod as m
A. Imports only pkg
B. Imports pkg, then pkg.sub, then pkg.sub.mod
C. Fails unless __all__ has ‘mod’
D. Equivalent to from pkg.sub import mod

25) Export control with __all__ in package __init__.py

# pkg/__init__.py
__all__ = ["api"]
# pkg/api.py exists; pkg/internal.py exists
from pkg import *
print("api" in dir(), "internal" in dir())
A. True True
B. True False
C. False True
D. False False

Answer Key

1) B

2) A

3) A

4) A

5) B

6) B

7) A

8) B

9) A

10) B

11) B

12) B

13) B

14) B

15) A

16) A

17) A

18) B

19) B

20) B

21) A

22) B

23) B

24) B

25) B