What is the purpose of the logging.StreamHandler?
StreamHandler directs log output to a specified stream.
Why this answer
It sends log messages to a stream, such as sys.stderr or sys.stdout.
38 questions · Library Modules · All types, answers revealed
What is the purpose of the logging.StreamHandler?
StreamHandler directs log output to a specified stream.
Why this answer
It sends log messages to a stream, such as sys.stderr or sys.stdout.
Which TWO of the following are true about the logging module?
Loggers use dot notation to represent a hierarchy.
Why this answer
The logging module is thread-safe, and loggers are hierarchical.
When using configparser, how do you retrieve a value as a boolean?
This is the correct method for boolean extraction.
Why this answer
The getboolean() method converts common truthy/falsy values automatically.
You are logging events with custom levels. How do you add a custom level 'TRACE' (value 5) to the logging module?
This registers the new numerical level and its name.
Why this answer
You use logging.addLevelName() to associate a numerical value with a level name.
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?
findall('item') retrieves direct children matching the tag name.
Why this answer
The findall() method with a direct path is the standard way to retrieve specific children.
In xml.etree.ElementTree, what is the difference between find() and findall() when used with XPath support?
This is the primary functional difference.
Why this answer
findall() supports full XPath syntax, whereas find() supports a more limited subset.
In the context of the CSV module, what is a 'dialect'?
A dialect encapsulates settings like delimiter and quoting behavior.
Why this answer
A dialect is a grouping of parameters (delimiter, quotechar, etc.) that defines the format of a CSV file.
When using configparser, what happens if you attempt to read a configuration file that does not exist using the read() method?
read() returns a list of successfully read files.
Why this answer
The read() method returns an empty list and does not raise an exception.
Which TWO of the following are valid ways to define a configuration option in configparser?
Direct mapping access is supported.
Why this answer
You can assign keys to a section or use the set method.
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?
The default quoting behavior (QUOTE_MINIMAL) wraps fields containing special characters.
Why this answer
The csv module automatically wraps such fields in quotes.
When parsing XML with ElementTree, how do you retrieve the text content of an element?
The text attribute accesses the tag's inner text.
Why this answer
The .text attribute of the Element object holds the text content.
Which THREE of the following are valid parameters for the csv.writer constructor?
Valid parameter.
Why this answer
delimiter, quotechar, and lineterminator are valid parameters.
Which TWO of the following are valid ways to read sections in a configparser object?
The sections() method returns a list of sections.
Why this answer
You can iterate over the object or use the sections() method.
Which THREE of the following are components of the logging module?
Core component.
Why this answer
Loggers, Handlers, and Formatters are core components.
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?
The fallback parameter handles missing options gracefully.
Why this answer
The get() method accepts a 'fallback' argument to return a default value if the option is missing.
When executing a SQL query with parameters in sqlite3, why should you use placeholders (e.g., ?) rather than f-strings?
Placeholders ensure that input is treated as data, not executable code.
Why this answer
Using placeholders prevents SQL injection attacks and allows the library to handle type conversions.
Which class in xml.etree.ElementTree should you use to parse an XML file from a file object?
parse() reads the file into an ElementTree object.
Why this answer
The parse() function is the standard way to read XML from a file object.
What is the result of using '?' as a placeholder in sqlite3 for a parameter that is a Python list?
Passing a list where a single value is expected (or using a single placeholder for multiple values) causes a mapping error.
Why this answer
sqlite3 does not automatically expand lists; you must use a tuple of values or construct the SQL dynamically.
Which THREE of the following are valid methods of the xml.etree.ElementTree.Element class?
Valid method for searching children.
Why this answer
The methods find(), set(), and append() are part of the Element API.
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?
The commit() method persists changes to the database.
Why this answer
The commit() method on a connection object is required to save changes permanently in sqlite3.
Which TWO of the following are valid ways to create a connection in the sqlite3 module?
Creates an in-memory database.
Why this answer
You can connect using a path string or the special ':memory:' string.
In the csv module, how do you specify that the CSV file uses a semicolon (;) as a delimiter?
This sets the correct field delimiter.
Why this answer
The delimiter parameter in the csv.reader or csv.writer function is used for this purpose.
Which logging configuration would effectively suppress all log messages?
This disables all logs below the provided level.
Why this answer
Setting the level to logging.CRITICAL + 1 (or any value higher than the highest defined level) suppresses all logs.
Which method in the xml.dom.minidom module is used to retrieve the value of a specific attribute of an element?
This is the correct method for accessing attributes in DOM nodes.
Why this answer
The getAttribute() method is used to retrieve the value of an attribute by its name.
You want to execute multiple SQL statements at once in sqlite3. Which method should you use?
executescript() executes a script containing multiple SQL statements.
Why this answer
The executescript() method allows executing multiple SQL statements separated by semicolons.
Which THREE of the following are valid states/modes for the csv.QUOTE_* constants?
Valid constant.
Why this answer
QUOTE_ALL, QUOTE_MINIMAL, and QUOTE_NONE are valid constants.
When using configparser, how do you handle multiline values?
Indentation is the standard way to signify continuation of a value.
Why this answer
You indent lines following the initial line within the option.
When using sqlite3.Row, what is the primary benefit over using a standard tuple?
This feature makes code more readable and robust against schema changes.
Why this answer
sqlite3.Row allows accessing column values by name (string keys) instead of just numerical indices.
You are configuring a logging FileHandler. What does the 'mode' parameter control?
The mode parameter specifies how the file is opened (e.g., 'a', 'w').
Why this answer
The 'mode' parameter controls the file opening mode (e.g., 'a' for append, 'w' for write).
You want to retrieve a list of all section names from a ConfigParser object. How do you do this?
This returns a list of section names.
Why this answer
The sections() method returns a list containing all section names except the DEFAULT section.
Which TWO of the following are required to successfully write a row to a CSV file?
The writer object handles the formatting.
Why this answer
You need an open file object and a csv.writer instance.
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?
Setting optionxform to a function that returns the input string prevents lowercase conversion.
Why this answer
By default, ConfigParser converts keys to lowercase. You must override the optionxform method of the parser instance.
You are configuring logging using a dictConfig. You want to send logs to a RotatingFileHandler. Which key must you define in the 'handlers' section?
The 'class' key identifies the handler implementation.
Why this answer
The 'class' key is required to specify the handler type, e.g., 'logging.handlers.RotatingFileHandler'.
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?
This explicitly sets the log level for the specified library logger.
Why this answer
You can configure individual loggers using getLogger() and setting their specific level attribute.
Which THREE of the following are valid logging levels?
Valid level.
Why this answer
DEBUG, INFO, and WARNING are valid levels.
You are processing a CSV file using csv.DictReader. If your input file lacks a header row, how do you provide the field names?
This correctly maps columns to the provided field names.
Why this answer
You pass the 'fieldnames' argument to the constructor to define the headers explicitly.
You are using sqlite3 and need to enforce foreign key constraints. How do you enable them?
This is the correct SQLite pragma command.
Why this answer
You must execute the command 'PRAGMA foreign_keys = ON;' on the connection.
Which TWO of the following are true about the sqlite3.Cursor object?
Cursors can iterate over result sets.
Why this answer
The cursor is used to execute SQL and retrieve results.
Ready to test yourself?
Try a timed practice session using only Library Modules questions.