Courseiva

CCNA Library Modules Questions

38 questions · Library Modules · All types, answers revealed

1
MCQeasy

What is the purpose of the logging.StreamHandler?

A.To write logs to a file.
B.To send logs to a stream like stdout.
C.To format log messages.
D.To rotate log files.
AnswerB

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.

2
Multi-Selectmedium

Which TWO of the following are true about the logging module?

Select 2 answers
A.Logging levels are strings.
B.You cannot have multiple handlers for a single logger.
C.Loggers are organized in a hierarchy.
D.Logging is synchronous by default.
E.The logging module is thread-safe.
AnswersC, E

Loggers use dot notation to represent a hierarchy.

Why this answer

The logging module is thread-safe, and loggers are hierarchical.

3
MCQeasy

When using configparser, how do you retrieve a value as a boolean?

A.parser.get('section', 'option', type=bool)
B.parser.get_bool('section', 'option')
C.parser.getboolean('section', 'option')
D.bool(parser.get('section', 'option'))
AnswerC

This is the correct method for boolean extraction.

Why this answer

The getboolean() method converts common truthy/falsy values automatically.

4
MCQhard

You are logging events with custom levels. How do you add a custom level 'TRACE' (value 5) to the logging module?

A.logging.TRACE = 5
B.logging.setLevel('TRACE', 5)
C.logging.addLevelName(5, 'TRACE')
D.logging.define_level('TRACE', 5)
AnswerC

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.

5
MCQmedium

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?

A.root.findall('item')
B.root.iter('item')
C.root.find('item')
D.root.get('item')
AnswerA

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.

6
MCQhard

In xml.etree.ElementTree, what is the difference between find() and findall() when used with XPath support?

A.findall() returns a list, find() returns a single element.
B.find() is faster for large trees.
C.findall() requires an iterator.
D.findall() is recursive, find() is not.
AnswerA

This is the primary functional difference.

Why this answer

findall() supports full XPath syntax, whereas find() supports a more limited subset.

7
MCQeasy

In the context of the CSV module, what is a 'dialect'?

A.A specific file extension.
B.A set of parameters defining a CSV format.
C.The encoding of the file.
D.A list of column names.
AnswerB

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.

8
MCQmedium

When using configparser, what happens if you attempt to read a configuration file that does not exist using the read() method?

A.It raises a FileNotFoundError.
B.It raises a configparser.Error.
C.It creates an empty configuration object.
D.It returns an empty list.
AnswerD

read() returns a list of successfully read files.

Why this answer

The read() method returns an empty list and does not raise an exception.

9
Multi-Selectmedium

Which TWO of the following are valid ways to define a configuration option in configparser?

Select 2 answers
A.parser['section']['option'] = 'value'
B.parser.add_option('section', 'option', 'value')
C.parser.write('section', 'option', 'value')
D.parser.set('section', 'option', 'value')
E.parser.insert('section', 'option', 'value')
AnswersA, D

Direct mapping access is supported.

Why this answer

You can assign keys to a section or use the set method.

10
MCQmedium

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?

A.It raises a csv.Error.
B.It quotes the field.
C.It ignores the delimiter.
D.It escapes the character with a backslash.
AnswerB

The default quoting behavior (QUOTE_MINIMAL) wraps fields containing special characters.

Why this answer

The csv module automatically wraps such fields in quotes.

11
MCQeasy

When parsing XML with ElementTree, how do you retrieve the text content of an element?

A.element.content
B.element.value
C.element.get_text()
D.element.text
AnswerD

The text attribute accesses the tag's inner text.

Why this answer

The .text attribute of the Element object holds the text content.

12
Multi-Selectmedium

Which THREE of the following are valid parameters for the csv.writer constructor?

Select 3 answers
A.lineterminator
B.encoding
C.buffer_size
D.quotechar
E.delimiter
AnswersA, D, E

Valid parameter.

Why this answer

delimiter, quotechar, and lineterminator are valid parameters.

13
Multi-Selecteasy

Which TWO of the following are valid ways to read sections in a configparser object?

Select 2 answers
A.for section in parser.get_all():
B.for section in parser.keys():
C.for section in parser.read():
D.for section in parser.sections():
E.for section in parser:
AnswersD, E

The sections() method returns a list of sections.

Why this answer

You can iterate over the object or use the sections() method.

14
Multi-Selecteasy

Which THREE of the following are components of the logging module?

Select 3 answers
A.Formatter
B.Dispatcher
C.Logger
D.Manager
E.Handler
AnswersA, C, E

Core component.

Why this answer

Loggers, Handlers, and Formatters are core components.

15
MCQmedium

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?

A.parser.get('section', 'option', fallback='val')
B.parser.get('section', 'option', default='val')
C.parser.get_or_default('section', 'option', 'val')
D.parser.exists('section', 'option') ? ...
E.parser.get('section', 'option') or 'val'
AnswerA

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.

16
MCQmedium

When executing a SQL query with parameters in sqlite3, why should you use placeholders (e.g., ?) rather than f-strings?

A.It is required for SELECT queries only.
B.It enables automatic commit behavior.
C.It is faster to execute.
D.It prevents SQL injection vulnerabilities.
AnswerD

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.

17
MCQeasy

Which class in xml.etree.ElementTree should you use to parse an XML file from a file object?

A.ElementTree.parse()
B.ElementTree.XML()
C.ElementTree.load()
D.ElementTree.read()
AnswerA

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.

18
MCQeasy

What is the result of using '?' as a placeholder in sqlite3 for a parameter that is a Python list?

A.It raises a ProgrammingError.
B.It automatically converts the list to a comma-separated string.
C.It works as expected.
D.It only takes the first element of the list.
AnswerA

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.

19
Multi-Selecthard

Which THREE of the following are valid methods of the xml.etree.ElementTree.Element class?

Select 3 answers
A.find()
B.write()
C.append()
D.set()
E.to_string()
AnswersA, C, D

Valid method for searching children.

Why this answer

The methods find(), set(), and append() are part of the Element API.

20
MCQmedium

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?

A.connection.save()
B.cursor.flush()
C.connection.close()
D.connection.commit()
AnswerD

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.

21
Multi-Selecteasy

Which TWO of the following are valid ways to create a connection in the sqlite3 module?

Select 2 answers
A.sqlite3.connect(':memory:')
B.sqlite3.Connection('my_db.sqlite')
C.sqlite3.connect('my_db.sqlite')
D.sqlite3.new_db('my_db.sqlite')
E.sqlite3.open('my_db.sqlite')
AnswersA, C

Creates an in-memory database.

Why this answer

You can connect using a path string or the special ':memory:' string.

22
MCQmedium

In the csv module, how do you specify that the CSV file uses a semicolon (;) as a delimiter?

A.csv.reader(f, char=';')
B.csv.reader(f, sep=';')
C.csv.reader(f, quotechar=';')
D.csv.reader(f, delimiter=';')
AnswerD

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.

23
MCQmedium

Which logging configuration would effectively suppress all log messages?

A.logging.disable(logging.CRITICAL + 1)
B.logging.basicConfig(level=0)
C.logging.disable(logging.CRITICAL)
D.logging.getLogger().setLevel(logging.NOTSET)
AnswerA

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.

24
MCQmedium

Which method in the xml.dom.minidom module is used to retrieve the value of a specific attribute of an element?

A.element.attr('attr_name')
B.element.getAttribute('attr_name')
C.element.getAttributeNode('attr_name').value
D.element.value('attr_name')
E.element['attr_name']
AnswerB

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.

25
MCQmedium

You want to execute multiple SQL statements at once in sqlite3. Which method should you use?

A.cursor.executescript()
B.connection.execute_all()
C.cursor.execute_script()
D.cursor.execute_many()
AnswerA

executescript() executes a script containing multiple SQL statements.

Why this answer

The executescript() method allows executing multiple SQL statements separated by semicolons.

26
Multi-Selectmedium

Which THREE of the following are valid states/modes for the csv.QUOTE_* constants?

Select 3 answers
A.csv.QUOTE_NONE
B.csv.QUOTE_ALL
C.csv.QUOTE_EVERYTHING
D.csv.QUOTE_AUTO
E.csv.QUOTE_MINIMAL
AnswersA, B, E

Valid constant.

Why this answer

QUOTE_ALL, QUOTE_MINIMAL, and QUOTE_NONE are valid constants.

27
MCQhard

When using configparser, how do you handle multiline values?

A.Use a backslash at the end of each line.
B.Indent the subsequent lines.
C.Wrap them in triple quotes ('''...''').
D.Use the '\n' escape character.
AnswerB

Indentation is the standard way to signify continuation of a value.

Why this answer

You indent lines following the initial line within the option.

28
MCQeasy

When using sqlite3.Row, what is the primary benefit over using a standard tuple?

A.It automatically commits transactions.
B.It provides dictionary-like access to columns by name.
C.It is faster to execute.
D.It supports multi-threading natively.
AnswerB

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.

29
MCQmedium

You are configuring a logging FileHandler. What does the 'mode' parameter control?

A.The log format.
B.The rotation interval.
C.The file opening mode.
D.The character encoding.
AnswerC

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).

30
MCQhard

You want to retrieve a list of all section names from a ConfigParser object. How do you do this?

A.list(parser.sections())
B.parser.sections()
C.parser.keys()
D.parser.get_sections()
AnswerB

This returns a list of section names.

Why this answer

The sections() method returns a list containing all section names except the DEFAULT section.

31
Multi-Selectmedium

Which TWO of the following are required to successfully write a row to a CSV file?

Select 2 answers
A.A list of strings to write.
B.An instance of csv.writer.
C.A call to csv.close().
D.A predefined dialect.
E.A file object opened in write mode.
AnswersB, E

The writer object handles the formatting.

Why this answer

You need an open file object and a csv.writer instance.

32
MCQhard

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?

A.parser = ConfigParser(case_sensitive=True)
B.parser.set_case(True)
C.parser.optionxform = lambda x: x
D.parser.read(file, case='preserve')
AnswerC

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.

33
MCQhard

You are configuring logging using a dictConfig. You want to send logs to a RotatingFileHandler. Which key must you define in the 'handlers' section?

A.destination
B.type
C.handler_type
D.class
AnswerD

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'.

34
MCQmedium

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?

A.logging.getLogger('urllib3').setLevel(logging.WARNING)
B.logging.config.dictConfig({'urllib3': 'WARNING'})
C.logging.basicConfig(level=logging.WARNING)
D.logging.setLevel('urllib3', logging.WARNING)
AnswerA

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.

35
Multi-Selecteasy

Which THREE of the following are valid logging levels?

Select 3 answers
A.DEBUG
B.WARNING
C.NOTICE
D.FATAL
E.INFO
AnswersA, B, E

Valid level.

Why this answer

DEBUG, INFO, and WARNING are valid levels.

36
MCQmedium

You are processing a CSV file using csv.DictReader. If your input file lacks a header row, how do you provide the field names?

A.csv.DictReader(f, headers=['a', 'b'])
B.csv.DictReader(f).map(['a', 'b'])
C.csv.DictReader(f, fieldnames=['a', 'b'])
D.csv.DictReader(f, columns=['a', 'b'])
AnswerC

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.

37
MCQmedium

You are using sqlite3 and need to enforce foreign key constraints. How do you enable them?

A.It is enabled by default.
B.connection.execute('PRAGMA foreign_keys = ON')
C.connection.execute('SET FOREIGN_KEYS ON')
D.connection.enable_foreign_keys()
AnswerB

This is the correct SQLite pragma command.

Why this answer

You must execute the command 'PRAGMA foreign_keys = ON;' on the connection.

38
Multi-Selecthard

Which TWO of the following are true about the sqlite3.Cursor object?

Select 2 answers
A.It can be used as an iterator to fetch rows.
B.It is used to execute SQL statements.
C.It automatically creates a transaction for every SELECT.
D.It is limited to one active query.
E.It keeps track of the connection state.
AnswersA, B

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.