D 522 Final Exam V1 | D 522 Python for IT
Automation | Actual Q&A with Rationale
(D522 Final Exam) | Western Governors
University
1. When using the regular expression module in Python, which function should be used to
find every instance of a pattern within a target string?
A. re.findall()
B. re.match()
C. re.search()
D. re.finditer()
Answer: A
Rationale: The re.findall() function is designed to scan the entire string and return all non-
overlapping matches as a list of strings. This is particularly useful in IT automation for
extracting all IP addresses or error codes from a large log file. Unlike re.search(), which
only returns the first match, findall ensures comprehensive data extraction.
2. Which keyword is used to ensure that a file is properly closed after its operations are
complete, even if an exception occurs?
A. with
B. open
,C. close
D. finally
Answer: A
Rationale: The ‘with’ statement in Python creates a context manager that automatically
handles resource cleanup. In the context of IT automation, using ‘with open()’ prevents file
corruption and memory leaks by ensuring the file closes after the block finishes. This is
considered a best practice over manually calling the .close() method.
3. What is the primary purpose of the ‘unittest’ module in a Python automation workflow?
A. To automate the deployment of scripts to production.
B. To verify that individual components of the script work as intended.
C. To create graphical user interfaces for system administrators.
D. To manage external library dependencies via pip.
Answer: B
Rationale: The unittest module provides a framework for creating automated tests to
validate that code behaves correctly. IT automation requires high reliability, so testing
individual functions helps prevent regressions when scripts are updated. It allows
developers to assert that outputs match expected values across various system
environments.
, 4. In the ‘subprocess’ module, which argument should be set to True to capture the output of
a system command for further processing?
A. check
B. shell
C. capture_output
D. text
Answer: C
Rationale: The capture_output=True argument allows the subprocess.run() function to
store the stdout and stderr of the command. This is essential for scripts that need to parse
the results of a shell command to make logic-based decisions. Without this, the output is
simply printed to the console and cannot be assigned to a Python variable.
5. Which logging level should be used for events that indicate an unexpected problem,
though the script is still able to continue running?
A. DEBUG
B. INFO
C. WARNING
D. CRITICAL
Answer: C
Automation | Actual Q&A with Rationale
(D522 Final Exam) | Western Governors
University
1. When using the regular expression module in Python, which function should be used to
find every instance of a pattern within a target string?
A. re.findall()
B. re.match()
C. re.search()
D. re.finditer()
Answer: A
Rationale: The re.findall() function is designed to scan the entire string and return all non-
overlapping matches as a list of strings. This is particularly useful in IT automation for
extracting all IP addresses or error codes from a large log file. Unlike re.search(), which
only returns the first match, findall ensures comprehensive data extraction.
2. Which keyword is used to ensure that a file is properly closed after its operations are
complete, even if an exception occurs?
A. with
B. open
,C. close
D. finally
Answer: A
Rationale: The ‘with’ statement in Python creates a context manager that automatically
handles resource cleanup. In the context of IT automation, using ‘with open()’ prevents file
corruption and memory leaks by ensuring the file closes after the block finishes. This is
considered a best practice over manually calling the .close() method.
3. What is the primary purpose of the ‘unittest’ module in a Python automation workflow?
A. To automate the deployment of scripts to production.
B. To verify that individual components of the script work as intended.
C. To create graphical user interfaces for system administrators.
D. To manage external library dependencies via pip.
Answer: B
Rationale: The unittest module provides a framework for creating automated tests to
validate that code behaves correctly. IT automation requires high reliability, so testing
individual functions helps prevent regressions when scripts are updated. It allows
developers to assert that outputs match expected values across various system
environments.
, 4. In the ‘subprocess’ module, which argument should be set to True to capture the output of
a system command for further processing?
A. check
B. shell
C. capture_output
D. text
Answer: C
Rationale: The capture_output=True argument allows the subprocess.run() function to
store the stdout and stderr of the command. This is essential for scripts that need to parse
the results of a shell command to make logic-based decisions. Without this, the output is
simply printed to the console and cannot be assigned to a Python variable.
5. Which logging level should be used for events that indicate an unexpected problem,
though the script is still able to continue running?
A. DEBUG
B. INFO
C. WARNING
D. CRITICAL
Answer: C