When parsing an XML document using xml.etree.ElementTree, you want to find all 'item' tags that are immediate children of the root element. Which method is most efficient for this?
You are writing data to a CSV file using csv.writer. You want to ensure that row delimiters are handled correctly across different operating systems. What should you set the 'lineterminator' parameter to?
You are writing a Python script that must parse a large XML file using xml.etree.ElementTree. The file contains many namespaces, and you need to find all elements with the local name 'price' regardless of their namespace. Which approach is most efficient and correct?
You are building a Python tool that must parse a configuration file generated by a legacy application. The file contains sections whose keys are case-sensitive and may include duplicate keys within the same section; the tool must preserve every value and detect duplicates for a warning report. Which approach using configparser is technically correct?
You are working with the os module to manipulate file paths in a cross-platform Python application. You need to perform operations that are portable across Windows and Unix-like systems. Which TWO of the following are valid and recommended uses of the os.path submodule for path manipulation? (Choose two.)
You are writing a Python script that needs to parse a JSON string containing a date in ISO 8601 format. You want to automatically convert this date string into a datetime.date object during parsing. Which of the following approaches using the json module is correct?
You are writing a Python script to monitor a directory. When a new file appears, you need to read its last line efficiently without loading the entire file into memory. The files can be very large (several gigabytes). Which approach using the os module is most appropriate?
You are using the `os` module to manipulate file paths in a cross-platform script. You need to extract the filename without extension from a path, and also check if a path is absolute. Which TWO of the following statements about the `os.path` module are correct? (Choose two.)
You are designing a class that needs to support the context manager protocol. The class manages a network connection that must be closed exactly once, even if multiple exceptions occur within the with block. Which implementation correctly ensures that the connection is closed and that any exception is propagated?
You are using xml.etree.ElementTree to parse a large XML document. You need to find all 'book' elements that are descendants of any 'section' element, but you must not include 'book' elements that are direct children of the root. Which XPath expression correctly achieves this?
You are writing a Python script that needs to read a text file and count the number of lines. The file may be very large, so you want to avoid loading it entirely into memory. Which approach using the os module is most efficient?
You are maintaining a Python 3 application that must read a JSON configuration file containing comments and trailing commas, which the standard json module cannot parse. You need to parse this file while preserving the comment information for later use. Which approach should you take?
You are using the sqlite3 module to interact with a database. You need to execute a query that inserts multiple rows efficiently and then retrieve the auto-generated primary key of the last inserted row. Which combination of methods should you use?
You are using configparser.ConfigParser to read a configuration file. You have a file where option names are case-sensitive. How do you ensure the parser respects this casing?
You need to configure the logging module to capture logs from multiple modules while ensuring that logs from third-party libraries (e.g., 'urllib3') are only captured at the WARNING level. How do you set this?
You are using the sqlite3 module to manage a database. You need to ensure that database changes are permanently saved even if your script terminates unexpectedly. Which method should you call after an INSERT operation?
You are writing to a CSV file. If your data contains the delimiter character itself (e.g., a comma in a text field), what does the csv module do by default?
You are using configparser and want to access a value that might not exist, but provide a default value if it is missing. Which method is most appropriate?
You are using the sqlite3 module to insert a large number of rows into a table. You want to maximize performance by reducing the number of disk writes. Which approach is most effective?
You are using the csv module to read a file that contains fields with embedded newlines and commas. Which TWO of the following statements about the csv.reader are correct? (Choose two.)
You are writing a Python application that must parse a configuration file where a value spans multiple lines, such as a long list of email addresses separated by newlines. You decide to use the configparser module. Which of the following correctly describes how to include a multi-line value in the configuration file?
You are building a Python script that needs to create a ZIP archive containing files from a directory. You want to preserve the directory structure inside the archive. Which approach using the zipfile module correctly adds the file '/data/reports/summary.txt' as 'reports/summary.txt' inside the archive?
You are building a configuration manager that reads an INI file. A section contains the key-value pair `timeout = 30`. You need to retrieve the value as an integer, but if the key is missing, you want to get 60 without raising an exception. Which code snippet achieves this using the `configparser` module?
You are developing a Python script that needs to determine the absolute path of a file located in a directory relative to the script's location. You decide to use the os.path module. Which of the following functions should you use to obtain the absolute path without resolving symbolic links?
You are building a Python application that uses the shelve module to store persistent data. You want to ensure that the database file is updated correctly and that you can access keys as attributes. Which TWO of the following statements about the shelve module are true? (Choose two.)
You are using the `json` module to serialize a custom object that contains a `datetime.date` attribute. When you call `json.dumps(obj)`, you get a `TypeError: Object of type date is not JSON serializable`. Which approach correctly allows serialization while preserving the date as an ISO 8601 string?
You are writing a Python script that must process a large text file line by line. The file is UTF-8 encoded, but some lines contain characters that are not representable in the default system encoding. You want to ensure that the script reads the file correctly on any platform. Which code correctly opens the file?
You are building a desktop tool with tkinter. A user clicks a button, and while a long file-compression routine runs you want a small window with a progress bar to stay visible and animated; the main application window must remain responsive to the user's clicks throughout. Which approach should you use?
You are using the datetime module to calculate the difference between two dates. You have two datetime objects, start and end, and you need to find the number of full days between them, ignoring any fractional part. Which expression correctly computes this?
You are using the logging module in a long-running service. You need to ensure that a specific logger named 'app.db' writes DEBUG and higher messages to a file, but its parent logger 'app' must not propagate these DEBUG messages to the root logger. Which configuration achieves this?
You are writing a Python utility that copies a directory tree to a network share using shutil. You must preserve file metadata and fail loudly if the destination already contains a conflicting file, rather than silently overwriting it. Which TWO shutil features satisfy these requirements? (Choose two.)
You are working with the sqlite3 module to build a small inventory application. You need to execute a parameterized INSERT statement and then retrieve the auto-generated primary key for the newly inserted row. Which two approaches are valid and safe? (Choose two.)
You are working with a large XML file and need to extract specific elements efficiently without loading the entire document into memory. You decide to use the xml.etree.ElementTree module. Which of the following methods should you use to iterate over all 'book' elements in the document?
A colleague's script parses a CSV export that uses a semicolon as the field separator and contains quoted text fields with embedded semicolons. They call csv.reader(f) without arguments and complain that rows are split incorrectly into too many fields. What is the correct fix?
You are writing a script that reads a large CSV file using csv.reader. The file uses a semicolon as the delimiter and contains quoted fields with embedded newlines. Which instantiation correctly handles this format?
You are writing a reusable module that must expose only a small public API while keeping several helper functions and constants internal. Users of the module should not import the helpers with 'from mymodule import *', but direct access such as mymodule._helper() may still be needed for tests. Which approach fits best?
You are writing a Python script that needs to create a temporary directory, write several files into it, and ensure that the directory and its contents are automatically deleted when the script exits, even if an exception occurs. Which approach using the tempfile module is most appropriate?
You are developing a Python application that must parse a large XML file and extract specific data. The file is too large to fit into memory, so you need to process it incrementally. Which approach using the xml.etree.ElementTree module is most appropriate?