D 522 Exam 2 V3 | D 522 Python for IT Automation
| Actual Q&A with Rationale (D522 Exam 2) |
Western Governors University
1. Which module is considered the modern standard for executing external system
commands and capturing their output in Python automation?
A. os
B. subprocess
C. sys
D. shutil
Correct Answer: B
Explanation: The subprocess module is designed to replace older modules like os.system
and os.spawn. It provides a more flexible interface for spawning new processes and
connecting to their input/output/error pipes. For IT automation, subprocess.run() is
preferred as it allows scripts to wait for command completion and easily capture the return
code and output.
2. When using the ‘re’ module to extract a specific pattern from a log file, which method
returns all non-overlapping matches as a list of strings?
A. re.search()
B. re.match()
,C. re.findall()
D. re.split()
Correct Answer: C
Explanation: The re.findall() function scans the entire input string and returns all
occurrences of a pattern in a list. This is particularly useful in automation for tasks like
extracting all IP addresses or timestamps from a large text file. Unlike re.search(), which
only finds the first match, findall ensures all relevant data points are collected.
3. In a try-except block, which keyword is used to execute a block of code only if no
exceptions were raised in the try block?
A. else
B. finally
C. catch
D. then
Correct Answer: A
Explanation: The else clause in a Python exception handling structure runs only when the
code in the try block completes successfully without errors. This is useful for logic that
should only occur if the initial operation, such as opening a file, succeeded. It helps separate
the main logic from the error handling code to improve readability.
, 4. Which method from the ‘csv’ module allows you to read a CSV file where each row is
mapped to a dictionary?
A. csv.reader()
B. csv.DictReader()
C. csv.write_dict()
D. csv.load_json()
Correct Answer: B
Explanation: The csv.DictReader class creates an object that operates like a regular reader
but maps the information in each row to a dict whose keys are given by the optional
fieldnames parameter. If fieldnames is omitted, the values in the first row of the file will be
used as the fieldnames. This is highly efficient for automation tasks where columns need to
be accessed by name rather than index.
5. When using ‘subprocess.run()’, which argument must be set to ‘True’ to capture standard
output and standard error so they can be accessed via the result object?
A. shell
B. capture_output
C. check
D. timeout
Correct Answer: B
| Actual Q&A with Rationale (D522 Exam 2) |
Western Governors University
1. Which module is considered the modern standard for executing external system
commands and capturing their output in Python automation?
A. os
B. subprocess
C. sys
D. shutil
Correct Answer: B
Explanation: The subprocess module is designed to replace older modules like os.system
and os.spawn. It provides a more flexible interface for spawning new processes and
connecting to their input/output/error pipes. For IT automation, subprocess.run() is
preferred as it allows scripts to wait for command completion and easily capture the return
code and output.
2. When using the ‘re’ module to extract a specific pattern from a log file, which method
returns all non-overlapping matches as a list of strings?
A. re.search()
B. re.match()
,C. re.findall()
D. re.split()
Correct Answer: C
Explanation: The re.findall() function scans the entire input string and returns all
occurrences of a pattern in a list. This is particularly useful in automation for tasks like
extracting all IP addresses or timestamps from a large text file. Unlike re.search(), which
only finds the first match, findall ensures all relevant data points are collected.
3. In a try-except block, which keyword is used to execute a block of code only if no
exceptions were raised in the try block?
A. else
B. finally
C. catch
D. then
Correct Answer: A
Explanation: The else clause in a Python exception handling structure runs only when the
code in the try block completes successfully without errors. This is useful for logic that
should only occur if the initial operation, such as opening a file, succeeded. It helps separate
the main logic from the error handling code to improve readability.
, 4. Which method from the ‘csv’ module allows you to read a CSV file where each row is
mapped to a dictionary?
A. csv.reader()
B. csv.DictReader()
C. csv.write_dict()
D. csv.load_json()
Correct Answer: B
Explanation: The csv.DictReader class creates an object that operates like a regular reader
but maps the information in each row to a dict whose keys are given by the optional
fieldnames parameter. If fieldnames is omitted, the values in the first row of the file will be
used as the fieldnames. This is highly efficient for automation tasks where columns need to
be accessed by name rather than index.
5. When using ‘subprocess.run()’, which argument must be set to ‘True’ to capture standard
output and standard error so they can be accessed via the result object?
A. shell
B. capture_output
C. check
D. timeout
Correct Answer: B