D 522 Exam 2 V3 | D 522 Python for IT
Automation | Actual Q&A with Rationale
(D522 Exam 2) | Western Governors
University
1. When using the subprocess module, which function is recommended for most applications
to execute a child process and wait for it to complete?
A. subprocess.run()
B. subprocess.call()
C. subprocess.popen()
D. subprocess.check_output()
Answer: A
Rationale: The subprocess.run() function was introduced in Python 3.5 and is the current
standard for executing commands. It simplifies the process by waiting for the command to
finish and returning a CompletedProcess instance. This approach is preferred over older
functions like call() because it provides a more robust interface for handling stdout and
stderr.
2. Which regular expression pattern would best identify a standard IPv4 address such as
192.168.1.1?
A. r’...’
,B. r’[a-z]{1,3}.[a-z]{1,3}’
C. r’+.+’
D. r’+’
Answer: A
Rationale: The pattern matches between one and three digits, which corresponds to the
octets in an IPv4 address. The backslash before the dot ensures the literal period is
matched rather than any character. This pattern is fundamental for network automation
scripts that parse log files or configuration outputs.
3. What is the primary benefit of using a context manager (with statement) when opening
files in Python?
A. It automatically closes the file after the block is finished.
B. It encrypts the data written to the file.
C. It increases the speed of file write operations.
D. It allows multiple processes to write to the same file simultaneously.
Answer: A
Rationale: A context manager ensures that file descriptors are released back to the
operating system even if an error occurs within the block. This prevents resource leaks
which can lead to system instability in large automation pipelines. It is a standard Pythonic
best practice that improves code readability and reliability.
, 4. Which method in the ‘os’ module should be used to create a directory if you also need to
create any missing parent directories?
A. os.mkdir()
B. os.path.create()
C. os.system(‘mkdir’)
D. os.makedirs()
Answer: D
Rationale: The os.makedirs() function is recursive, meaning it will create all intermediate-
level directories needed to contain the leaf directory. In contrast, os.mkdir() will raise a
FileNotFoundError if the parent path does not exist. This makes makedirs() highly effective
for setting up log structures or repository paths in automation scripts.
5. In the ‘unittest’ framework, which method is used to verify that two values are identical
during a test case?
A. self.assertTrue()
B. self.assertIdentical()
C. self.checkEqual()
D. self.assertEqual()
Answer: D
Automation | Actual Q&A with Rationale
(D522 Exam 2) | Western Governors
University
1. When using the subprocess module, which function is recommended for most applications
to execute a child process and wait for it to complete?
A. subprocess.run()
B. subprocess.call()
C. subprocess.popen()
D. subprocess.check_output()
Answer: A
Rationale: The subprocess.run() function was introduced in Python 3.5 and is the current
standard for executing commands. It simplifies the process by waiting for the command to
finish and returning a CompletedProcess instance. This approach is preferred over older
functions like call() because it provides a more robust interface for handling stdout and
stderr.
2. Which regular expression pattern would best identify a standard IPv4 address such as
192.168.1.1?
A. r’...’
,B. r’[a-z]{1,3}.[a-z]{1,3}’
C. r’+.+’
D. r’+’
Answer: A
Rationale: The pattern matches between one and three digits, which corresponds to the
octets in an IPv4 address. The backslash before the dot ensures the literal period is
matched rather than any character. This pattern is fundamental for network automation
scripts that parse log files or configuration outputs.
3. What is the primary benefit of using a context manager (with statement) when opening
files in Python?
A. It automatically closes the file after the block is finished.
B. It encrypts the data written to the file.
C. It increases the speed of file write operations.
D. It allows multiple processes to write to the same file simultaneously.
Answer: A
Rationale: A context manager ensures that file descriptors are released back to the
operating system even if an error occurs within the block. This prevents resource leaks
which can lead to system instability in large automation pipelines. It is a standard Pythonic
best practice that improves code readability and reliability.
, 4. Which method in the ‘os’ module should be used to create a directory if you also need to
create any missing parent directories?
A. os.mkdir()
B. os.path.create()
C. os.system(‘mkdir’)
D. os.makedirs()
Answer: D
Rationale: The os.makedirs() function is recursive, meaning it will create all intermediate-
level directories needed to contain the leaf directory. In contrast, os.mkdir() will raise a
FileNotFoundError if the parent path does not exist. This makes makedirs() highly effective
for setting up log structures or repository paths in automation scripts.
5. In the ‘unittest’ framework, which method is used to verify that two values are identical
during a test case?
A. self.assertTrue()
B. self.assertIdentical()
C. self.checkEqual()
D. self.assertEqual()
Answer: D