First 50 of 200 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.
Continue in the practice tool above to load more questions, 50 at a time.
Question 1Modules and Packages
Which statement about a Python module is correct?
- A module can only be created from compiled C extensions.
- A module must contain a function named main to be importable.
- A module is a runtime object that exists only while a script executes.
- A module is a file containing Python definitions and statements whose name is the file name without the .py suffix. Correct answer.
Explanation
Any .py file is a module; its module name is the file name minus the .py extension. Importing executes the file's top-level code and binds the resulting module object. A main function is not required, and modules may be pure Python or extensions.
Question 2Modules and Packages
What does `import math` bind in the importing namespace?
- Each public function of math as a separate name.
- The name math referring to the math module object. Correct answer.
- The name math referring to the file path of the module.
- Nothing until a function from math is called.
Explanation
A plain `import math` binds only the module name in the current namespace. Individual attributes are accessed through that name, such as math.sqrt. Functions are not injected individually unless `from math import ...` is used.
Question 3Modules and Packages
Given a package directory `pkg` containing `__init__.py` and a submodule `tools.py`, which import statement makes `pkg.tools` accessible through the package attribute after execution?
- from pkg import
- import pkg.tools Correct answer.
- import tools
- from pkg.tools import
Explanation
`import pkg.tools` imports the submodule and binds the top-level name pkg, with tools available as an attribute of pkg. The other forms are syntactically incomplete or import the wrong name.
Question 4Modules and Packages
What is the purpose of the `__init__.py` file in a package?
- It stores the package version for the import system to read.
- It marks the directory as a package and runs when the package is imported. Correct answer.
- It is required only when the package contains C extensions.
- It lists every submodule that must be imported automatically.
Explanation
For regular packages, __init__.py marks the directory as a package and its code executes when the package is imported. It can be empty, and it does not automatically import submodules or store version metadata.
Question 5Modules and Packages
Which statement imports only the `sqrt` function from the `math` module?
- from math import sqrt Correct answer.
- from math import math.sqrt
- import math as sqrt
- import math.sqrt
Explanation
`from module import name` binds the named attribute directly in the current namespace. `import math.sqrt` is invalid syntax because the dotted name must resolve to a module, not a function, and aliasing with `as` binds the module.
Question 6Modules and Packages
What does `sys.path` contain?
- A set of already imported module names.
- A dictionary mapping module names to file paths.
- A tuple of built-in module names available in the interpreter.
- A list of strings that the import system searches for modules. Correct answer.
Explanation
sys.path is a list of directory and zip entries that the import system scans in order. Modifying it changes where imports can find modules. It is not a mapping, set, or tuple of names.
Question 7Modules and Packages
Which built-in variable holds the name of the current module, and equals `"__main__"` when the file is run as a script?
- __module__
- __package__
- __name__ Correct answer.
- __file__
Explanation
__name__ holds the module's name; when a file is executed directly as a script, the interpreter sets it to "__main__". This is the basis of the `if __name__ == "__main__":` guard.
Question 8Modules and Packages
A package `shop` contains `__init__.py` and `cart.py`. The file `shop/cart.py` contains `from . import models`. What does the leading dot signify?
- An import from the standard library.
- An import of a module named `models` in the current working directory.
- A relative import resolved against the current package. Correct answer.
- A deferred import executed only when models is first used.
Explanation
A leading dot in a from-import means a relative import, resolved relative to the package that contains the current module. Two dots would go up one level. It does not refer to the standard library or the working directory.
Question 9Modules and Packages
What does `importlib.reload(module)` do?
- It removes the module from sys.modules and imports a fresh copy.
- It restores the module to the version that was first imported.
- It reloads all modules that imported the given module.
- It re-executes the module's code in the existing module object's namespace. Correct answer.
Explanation
reload re-executes the module's top-level code using the existing module object's dictionary, so the object identity is preserved. It does not purge sys.modules or update dependent modules automatically.
Question 10Modules and Packages
A script does `import helpers` then `import helpers` again. How many times does the top-level code of helpers.py execute?
- Once per unique name bound in the importing namespace.
- Twice, because each import statement executes the file.
- Zero times, because only functions are imported.
- Once, because the module is cached in sys.modules. Correct answer.
Explanation
On the first import the module is executed and stored in sys.modules; later imports find the cached entry and skip re-execution. This makes imports cheap and gives modules singleton-like identity.
Question 11Modules and Packages
What does `from package import *` do when the package's `__init__.py` defines `__all__ = ["alpha", "beta"]`?
- It imports every name not starting with an underscore.
- It imports every submodule of the package.
- It imports only the names listed in __all__. Correct answer.
- It raises ImportError because packages cannot use star imports.
Explanation
When __all__ is defined, a star import binds exactly the names it lists. Without __all__, star imports bind public names (those not starting with an underscore). Star imports from packages are legal.
Question 12Modules and Packages
Which value of `__name__` is assigned to a module that is imported normally (not run as a script)?
- The module's import name, such as "pkg.tools". Correct answer.
- The name of the package that imported it.
- The string "__main__" regardless of how it is loaded.
- The absolute file path of the module.
Explanation
When a module is imported, __name__ is set to the name under which it was imported, including any package prefix. Only direct script execution sets it to "__main__".
Question 13Modules and Packages
A directory `plugins` contains `__init__.py` and `one.py`. After `import plugins.one`, what is bound in the local namespace?
- The name plugins, with one accessible as plugins.one. Correct answer.
- The name plugins.one as a single dotted identifier.
- The name one referring to the submodule.
- Both plugins and one as separate names.
Explanation
`import plugins.one` binds only the top-level package name plugins in the local namespace. The submodule is reached as an attribute, plugins.one. To bind one directly you would use `from plugins import one`.
Question 14Modules and Packages
What is the effect of a module-level `__all__` list on `from module import *`?
- It prevents the listed names from being accessed as attributes.
- It restricts the star import to the listed names. Correct answer.
- It forces every listed name to be imported eagerly at module load.
- It is documentation only and has no runtime effect.
Explanation
__all__ controls which names a star import exports. It does not change attribute access or force eager imports; it only affects the `import *` behavior.
Question 15Modules and Packages
Which statement about absolute and relative imports in Python 3 is correct?
- Relative imports search sys.path, while absolute imports resolve within the package.
- Absolute imports search sys.path, while relative imports resolve within the current package. Correct answer.
- Relative imports are only allowed in __init__.py files.
- Both absolute and relative imports always search the standard library first.
Explanation
In Python 3, absolute imports resolve against sys.path, while relative imports (leading dots) resolve relative to the current package. Relative imports are allowed in any module of a package, not only __init__.py.
Question 16Modules and Packages
What does the `__package__` attribute of a module indicate?
- The package the module belongs to, used to resolve relative imports. Correct answer.
- The directory on disk that contains the module file.
- The list of packages installed in the current environment.
- The module's parent class in its inheritance hierarchy.
Explanation
__package__ names the package context of a module and is used by the import system to resolve relative imports. For a top-level module it is typically empty or None; it is unrelated to installed packages or inheritance.
Question 17Modules and Packages
A directory named `data` with no `__init__.py` sits on sys.path. In Python 3.3+, what happens when code does `import data`?
- It raises ImportError because __init__.py is missing.
- It imports as a namespace package with no code executed. Correct answer.
- It is treated as a regular package and an empty __init__.py is generated.
- It imports every .py file in the directory as submodules.
Explanation
PEP 420 introduced namespace packages: a directory on sys.path without __init__.py can be imported as a package that has no __init__ code. No files are auto-imported and nothing is generated on disk.
Question 18Modules and Packages
Inside a module, code calls `importlib.import_module("a.b.c")`. Which name is bound in the caller's namespace?
- No name is bound, and the module object is discarded.
- The module object returned by the call, only if the caller assigns it. Correct answer.
- The name c, bound automatically.
- The top-level name a, bound automatically.
Explanation
import_module returns the module object but does not bind any name in the caller's namespace. To use it, the caller must assign the return value to a variable. Automatic binding happens only with the import statement.
Question 19Modules and Packages
A package's `__init__.py` contains `from .sub import helper`. What is a consequence of importing the package?
- The package import fails because __init__.py cannot import submodules.
- The name helper is bound only inside sub, not in the package namespace.
- The submodule sub is imported and helper becomes an attribute of the package. Correct answer.
- The submodule sub is imported only when helper is first called.
Explanation
Executing `from .sub import helper` in __init__.py imports the submodule and binds helper in the package's namespace, so pkg.helper works. This eager import runs at package import time.
Question 20Modules and Packages
Module `a` imports module `b`, and `b` imports `a` at top level. Under what condition does this circular import typically succeed?
- When sys.path lists the package directory twice.
- When the circular import is placed inside a function in both modules.
- When the names each module needs are defined before the other module is imported. Correct answer.
- When both modules use relative imports instead of absolute imports.
Explanation
Circular imports succeed if, by the time each module accesses the other, the needed names have already been bound. If access happens before definition, an AttributeError or ImportError results. Using relative imports or duplicating sys.path entries does not resolve ordering problems.
Question 21Modules and Packages
Which mechanism lets a package expose a submodule as if it were a top-level module without importing it eagerly?
- A module-level __getattr__ in __init__.py that imports on demand. Correct answer.
- A __path__ list that lists the submodule files.
- A sys.modules entry created at interpreter startup.
- A __all__ list that includes the submodule name.
Explanation
PEP 562 module-level __getattr__ is called when an attribute is not found, allowing lazy import of submodules. __all__ only affects star imports, __path__ controls package search paths, and sys.modules entries are created only after import.
Question 22Modules and Packages
What does `pkgutil.iter_modules()` yield when given a package's `__path__`?
- A list of file paths for every .pyc file in the path.
- ModuleInfo tuples describing importable modules found on that path. Correct answer.
- The names of packages that depend on the given package.
- The module objects already imported from the package.
Explanation
pkgutil.iter_modules scans the given path entries and yields ModuleInfo records (module finder, name, ispkg) for importable modules. It does not import them or return reverse dependencies.
Question 23Modules and Packages
A script run as `python app.py` contains `if __name__ == "__main__": main()`. What is the value of `__name__` inside app.py when it is instead imported by another module?
- "app" Correct answer.
- "__main__"
- "__init__"
- "main"
Explanation
When imported, __name__ is set to the module's import name, here "app", so the guard is false and main() is not called. Only direct execution sets __name__ to "__main__".
Question 24Modules and Packages
Which statement best describes how `sys.modules` differs from `sys.path`?
- Both are lists of search locations used during import.
- sys.modules maps names to already-imported modules, while sys.path lists search locations. Correct answer.
- Both are dictionaries keyed by module name.
- sys.modules lists search locations, while sys.path maps names to modules.
Explanation
sys.modules is a dictionary cache of imported modules keyed by name; sys.path is a list of locations searched when a name is not yet cached. They serve complementary roles in the import process.
Question 25Exceptions
Which base class must a user-defined exception inherit from so that it can be raised with the raise statement?
- SystemExit only
- The object class directly
- ExceptionGroup only
- BaseException or a subclass of it Correct answer.
Explanation
Any class derived from BaseException (directly or indirectly) can be raised. Exception is the conventional base for application errors, but BaseException is the root that makes raising legal.
Question 26Exceptions
What is the output of this code?
try:
x = 1 / 0
except ZeroDivisionError:
print('A')
finally:
print('B')
- A then B Correct answer.
- B only
- A then an uncaught error
- A only
Explanation
The except block handles ZeroDivisionError and prints A; the finally block always runs afterwards, printing B. So both lines appear, in that order.
Question 27Exceptions
Which statement about the else clause of a try statement is correct?
- It runs only if an exception was caught
- It runs before the try block
- It replaces the finally block
- It runs only if the try block raised no exception Correct answer.
Explanation
The else suite executes when the try block completes without raising an exception. It runs after try and before finally.
Question 28Exceptions
What does the raise statement do when used with no arguments inside an except block?
- Raises a new generic Exception
- Re-raises the exception currently being handled Correct answer.
- Raises a RuntimeError
- Clears the active exception
Explanation
A bare raise inside an exception handler re-raises the exception that is currently being handled, preserving its traceback. It is only valid while an exception is active.
Question 29Exceptions
Which built-in exception is raised when you access a dictionary key that does not exist?
- IndexError
- AttributeError
- LookupError
- KeyError Correct answer.
Explanation
Missing dictionary keys raise KeyError. IndexError is for sequences, LookupError is a base class of KeyError, and AttributeError concerns missing attributes.
Question 30Exceptions
What happens when an exception is raised inside a finally block while another exception is already propagating?
- The program silently continues
- Both exceptions are raised together
- The original exception wins and the new one is ignored
- The new exception replaces the original one Correct answer.
Explanation
An exception raised in finally aborts the pending exception and propagates in its place, with the original attached as context. The original is effectively lost unless retrieved via __context__.
Question 31Exceptions
Given this code, what is printed?
try:
raise ValueError('v')
except (TypeError, ValueError) as e:
print(type(e).__name__)
- v
- ValueError Correct answer.
- Exception
- TypeError
Explanation
The except tuple matches ValueError, so the handler runs and prints the class name of the caught exception, which is ValueError.
Question 32Exceptions
What is the purpose of the as clause in an except statement?
- It aliases the exception class
- It binds the caught exception instance to a name Correct answer.
- It selects which exception to ignore
- It renames the traceback
Explanation
The as clause binds the exception instance to a local name inside the handler. That name is deleted automatically at the end of the except block.
Question 33Exceptions
What is the value of e.__cause__ when an exception is raised with 'raise NewError from orig'?
- The NewError instance itself
- None
- The orig exception object Correct answer.
- A string with orig's message
Explanation
The 'raise ... from orig' form sets __cause__ to orig and also sets __suppress_context__ to True. This explicitly chains the new exception to the original.
Question 34Exceptions
How does implicit exception chaining differ from explicit chaining?
- Implicit chaining disables chaining entirely
- Both set only __traceback__
- Implicit chaining sets __cause__; explicit chaining sets __context__
- Implicit chaining sets __context__; explicit chaining sets __cause__ Correct answer.
Explanation
When an exception is raised while another is being handled, Python sets __context__ automatically (implicit chaining). Using 'raise ... from ...' sets __cause__ (explicit chaining).
Question 35Exceptions
What does 'raise SomeError from None' accomplish?
- It disables all tracebacks
- It makes the error non-catchable
- It converts the error to a warning
- It suppresses display of the implicit context Correct answer.
Explanation
Passing None as the cause sets __suppress_context__ to True, so the implicit context is not shown in the traceback. The exception is still fully catchable.
Question 36Exceptions
Which exception class is the recommended base for user-defined exceptions that represent application errors?
- Exception Correct answer.
- BaseException
- GeneratorExit
- KeyboardInterrupt
Explanation
User exceptions should normally subclass Exception so they are handled by typical 'except Exception' handlers and do not collide with system-exit signals.
Question 37Exceptions
What is printed?
try:
try:
raise KeyError('k')
finally:
print('inner')
except KeyError:
print('outer')
- inner then outer Correct answer.
- inner only
- outer only
- outer then inner
Explanation
The inner finally runs as the KeyError propagates out of the inner try, printing 'inner'. The outer except then catches it and prints 'outer'.
Question 38Exceptions
When an exception is not handled anywhere in the call stack, what does Python do?
- It silently ignores the exception
- It prints a traceback and terminates the program Correct answer.
- It converts the exception to a warning
- It retries the failing statement
Explanation
An unhandled exception propagates to the top level, where the interpreter prints the traceback to stderr and exits with a non-zero status.
Question 39Exceptions
What happens to the name bound by 'except E as e' after the except block finishes?
- e keeps referencing the exception
- e is rebound to the exception class
- The name e is deleted Correct answer.
- e becomes None
Explanation
Python deletes the target name at the end of the except block to break reference cycles with the traceback. Referencing it afterwards raises NameError.
Question 40Exceptions
Which exception is raised by the assert statement when its condition is false?
- AssertionError Correct answer.
- SyntaxError
- ValueError
- RuntimeError
Explanation
A failing assert raises AssertionError with an optional message. Asserts can be stripped when Python runs with optimization (-O).
Question 41Exceptions
What is the effect of running Python with the -O flag on assert statements?
- Asserts raise AssertionError more eagerly
- Asserts print a warning instead
- Asserts become try/except blocks
- Assert statements are removed from the bytecode Correct answer.
Explanation
Optimization mode compiles out assert statements, so they do not execute. Code must not rely on asserts for essential validation.
Question 42Exceptions
Which built-in exception is the base class of both KeyError and IndexError?
- ValueError
- TypeError
- RuntimeError
- LookupError Correct answer.
Explanation
LookupError is the common ancestor of exceptions raised for failed lookups, including KeyError and IndexError. Catching LookupError catches both.
Question 43Exceptions
What is printed?
def f():
return 1
try:
f()
except Exception:
print('caught')
else:
print('ok')
finally:
print('done')
- done only
- ok then done Correct answer.
- caught then done
- ok only
Explanation
No exception occurs, so the else clause runs and prints 'ok'. The finally clause always runs and prints 'done' afterwards.
Question 44Exceptions
Which exception type is raised when an operation uses an object of an inappropriate type, such as adding an int to a string?
- ValueError
- TypeError Correct answer.
- AttributeError
- NameError
Explanation
TypeError signals that an operation received an object of an unsupported type. ValueError would indicate a correct type with an invalid value.
Question 45Exceptions
How can a single except clause handle several distinct exception types?
- Chain them with the or keyword
- Separate them with commas without parentheses
- List them in a tuple after except Correct answer.
- Use one except clause per type only
Explanation
An except clause accepts a tuple of exception classes and matches if the raised exception is an instance of any of them.
Question 46Exceptions
What does the __context__ attribute of an exception hold when implicit chaining occurs?
- The message of the current exception
- The name of the function that raised it
- The traceback object
- The previously active exception being handled Correct answer.
Explanation
__context__ references the exception that was active when the new one was raised, enabling implicit chaining. __cause__ is reserved for explicit 'from' chaining.
Question 47Exceptions
What is the result of the following code?
try:
raise ValueError('x')
except Exception as e:
pass
print(e)
- It prints ValueError
- It prints x
- It prints None
- It raises NameError Correct answer.
Explanation
The name bound by 'as e' is deleted when the except block ends, so referencing e afterwards raises NameError.
Question 48Exceptions
Which statement about BaseException subclasses such as KeyboardInterrupt and SystemExit is correct?
- They cannot be raised by user code
- They are not caught by 'except Exception' Correct answer.
- They are subclasses of Exception
- They are always silently ignored
Explanation
KeyboardInterrupt and SystemExit inherit from BaseException but not from Exception, so a handler for Exception does not catch them. This lets shutdown signals bypass ordinary error handling.
Question 49Exceptions
What is printed?
def g():
try:
return 'try'
finally:
return 'finally'
print(g())
- finally Correct answer.
- A RuntimeError is raised
- try
- try then finally
Explanation
A return inside finally overrides any pending return from the try block, so the function returns 'finally'. This is a common source of subtle bugs.
Question 50Exceptions
Which built-in exception is raised when a function receives an argument whose value is of the correct type but is outside the acceptable range?
- RangeError
- ValueError Correct answer.
- IndexError
- TypeError
Explanation
ValueError signals that an argument has the right type but an unsuitable value, such as a negative number passed where a positive one is required. TypeError is reserved for wrong types, and IndexError applies to sequence indexing, not general argument validation.