← All public quizzes
Start the quiz (as a guest or with an account)
Python — Modules, packages & file I/O
by Jude · 23/05/2026 · 121 views · shares
Imports, packaging, standard library, files (text/binary), and
pathlib. 20 questions.
20
Questions
🇬🇧 English
Language
Medium
Difficulty
Questions in this quiz
-
1. Which file marks a regular package?
__main__.py__init__.pypackage.jsonsetup.cfg
-
2. Which idiom guards script-only code?
if __name__ == "__main__":if __file__ == "__main__":if main():if __init__ == "__main__":
-
3. Recommended way to open a file safely?
f = open("x.txt"); f.close()with open("x.txt") as f: ...file("x.txt")open("x.txt", autoclose=True)
-
4. What does this print?
from pathlib import Path p = Path("/tmp") / "data" / "x.txt" print(p.suffix, p.stem)- .txt x
- txt x.txt
- .txt /tmp/data/x
- x .txt
-
5. Which mode opens a file for binary appending?
- "a"
- "ab"
- "wb"
- "r+"
-
6. Which module reads/writes JSON?
csvpicklejsonxml.etree
-
7. What does
from m import *import?- Everything not starting with
_, or the names in__all__ - Only classes
- Only callables
- Nothing — syntax error
- Everything not starting with
-
8. Difference between
import fooandfrom foo import bar?- None
- First binds
foo; second binds onlybarin the current namespace - Second is faster
- First is deprecated
-
9. Modern packaging configuration file?
setup.py uploadpyproject.toml+builddistutilspyccompiler
-
10. What does this print if
data.txtcontainsHello\nWorld\n?with open("data.txt") as f: for line in f: print(repr(line))- 'Hello'\n'World'
- 'Hello\\n'\n'World\\n'
- 'Hello World'
- Nothing
-
11. Which path API is cross-platform and recommended over
os.pathin new code?pathlib.Pathshutilglobtempfile
-
12. What does this code do?
from pathlib import Path for p in Path(".").rglob("*.py"): ...- Only top-level
.pyfiles - Recursive glob: all
.pyfiles under cwd - Random files
- Errors out
- Only top-level
-
13. How do you safely write a UTF-8 text file?
open(p, "w")alwaysopen(p, "w", encoding="utf-8")open(p, "wb")then encode manuallyopen(p, "wt", newline="")
-
14. What does
importlib.reload(mod)do?- Removes the module from
sys.modules - Re-executes the module's top-level code and updates the module object in place
- Throws an error
- Imports a different module
- Removes the module from
-
15. Which is the best for atomic file writes?
- Write directly to the target
- Write to a temporary file in the same directory and
os.replacewhen done - Buffer in memory forever
- Use
print
-
16. What does
sys.pathcontain?- Environment variables
- List of paths Python searches for modules
- Currently imported modules
- Python version info
-
17. What does
__all__ = ["foo"]in a module mean?- Lists private symbols
- Controls names exposed by
from module import * - Documents the module
- Lists submodules
-
18. What's the difference between
pickleandjson?- None
pickleserializes Python-specific types (incl. classes) but is unsafe for untrusted data;jsonis language-neutral and safe to parsejsonis faster thanpicklefor arbitrary objectspickleoutputs human-readable text
-
19. What does this print?
import os print(os.path.join("/a", "/b", "c"))- /a/b/c
- /a/b/c/
- /b/c
- Error
-
20. What is the purpose of
io.StringIO?- Reading bytes from a socket
- An in-memory text stream that behaves like a file
- Wrapping
print - Compressing strings