QuizLander
← 所有公开测验

Python — Modules, packages & file I/O

作者 Jude · 23/05/2026 · 125 次浏览 · 0 次分享

Imports, packaging, standard library, files (text/binary), and pathlib. 20 questions.
20
题目
🇬🇧 English
语言
中等
难度
开始测验(游客或账户)
分享为图片

测验题目

  1. 1. Which file marks a regular package?
    1. __main__.py
    2. __init__.py
    3. package.json
    4. setup.cfg
  2. 2. Which idiom guards script-only code?
    1. if __name__ == "__main__":
    2. if __file__ == "__main__":
    3. if main():
    4. if __init__ == "__main__":
  3. 3. Recommended way to open a file safely?
    1. f = open("x.txt"); f.close()
    2. with open("x.txt") as f: ...
    3. file("x.txt")
    4. open("x.txt", autoclose=True)
  4. 4. What does this print?
    from pathlib import Path
    p = Path("/tmp") / "data" / "x.txt"
    print(p.suffix, p.stem)
    1. .txt x
    2. txt x.txt
    3. .txt /tmp/data/x
    4. x .txt
  5. 5. Which mode opens a file for binary appending?
    1. "a"
    2. "ab"
    3. "wb"
    4. "r+"
  6. 6. Which module reads/writes JSON?
    1. csv
    2. pickle
    3. json
    4. xml.etree
  7. 7. What does from m import * import?
    1. Everything not starting with _, or the names in __all__
    2. Only classes
    3. Only callables
    4. Nothing — syntax error
  8. 8. Difference between import foo and from foo import bar?
    1. None
    2. First binds foo; second binds only bar in the current namespace
    3. Second is faster
    4. First is deprecated
  9. 9. Modern packaging configuration file?
    1. setup.py upload
    2. pyproject.toml + build
    3. distutils
    4. pyc compiler
  10. 10. What does this print if data.txt contains Hello\nWorld\n?
    with open("data.txt") as f:
        for line in f:
            print(repr(line))
    1. 'Hello'\n'World'
    2. 'Hello\\n'\n'World\\n'
    3. 'Hello World'
    4. Nothing
  11. 11. Which path API is cross-platform and recommended over os.path in new code?
    1. pathlib.Path
    2. shutil
    3. glob
    4. tempfile
  12. 12. What does this code do?
    from pathlib import Path
    for p in Path(".").rglob("*.py"):
        ...
    1. Only top-level .py files
    2. Recursive glob: all .py files under cwd
    3. Random files
    4. Errors out
  13. 13. How do you safely write a UTF-8 text file?
    1. open(p, "w") always
    2. open(p, "w", encoding="utf-8")
    3. open(p, "wb") then encode manually
    4. open(p, "wt", newline="")
  14. 14. What does importlib.reload(mod) do?
    1. Removes the module from sys.modules
    2. Re-executes the module's top-level code and updates the module object in place
    3. Throws an error
    4. Imports a different module
  15. 15. Which is the best for atomic file writes?
    1. Write directly to the target
    2. Write to a temporary file in the same directory and os.replace when done
    3. Buffer in memory forever
    4. Use print
  16. 16. What does sys.path contain?
    1. Environment variables
    2. List of paths Python searches for modules
    3. Currently imported modules
    4. Python version info
  17. 17. What does __all__ = ["foo"] in a module mean?
    1. Lists private symbols
    2. Controls names exposed by from module import *
    3. Documents the module
    4. Lists submodules
  18. 18. What's the difference between pickle and json?
    1. None
    2. pickle serializes Python-specific types (incl. classes) but is unsafe for untrusted data; json is language-neutral and safe to parse
    3. json is faster than pickle for arbitrary objects
    4. pickle outputs human-readable text
  19. 19. What does this print?
    import os
    print(os.path.join("/a", "/b", "c"))
    1. /a/b/c
    2. /a/b/c/
    3. /b/c
    4. Error
  20. 20. What is the purpose of io.StringIO?
    1. Reading bytes from a socket
    2. An in-memory text stream that behaves like a file
    3. Wrapping print
    4. Compressing strings
答题并查看我的分数

评论

登录后即可评论。

加载中…