D 522 Exam 2 V2 | D 522 Python for IT
Automation | Actual Q&A with Rationale
(D522 Exam 2) | Western Governors
University
1. When using the subprocess module, which parameter of the run() function is required to
capture the standard output of a command so it can be accessed in the script?
A. shell=True
B. check=True
C. capture_output=True
D. timeout=10
Answer: C
Rationale: The capture_output parameter is essential when the script needs to process the
result of a system command. When set to True, both stdout and stderr are captured and
stored in the returned CompletedProcess object. This allows the developer to read the
command’s response rather than just seeing it printed to the terminal.
2. In the context of Regular Expressions, what is the purpose of the ‘?’ quantifier?
A. Matches zero or more occurrences
B. Matches one or more occurrences
C. Matches exactly one occurrence
,D. Matches zero or one occurrence
Answer: D
Rationale: The question mark in a regex pattern signifies that the preceding character or
group is optional. This means it matches either zero or one instance of that specific
element. It is frequently used for handling optional characters like the ‘s’ in ‘https’ or
optional punctuation.
3. Which function from the ‘os’ module should be used to combine directory paths in a way
that is portable across different operating systems?
A. os.path.combine()
B. os.path.merge()
C. os.path.join()
D. os.path.add()
Answer: C
Rationale: The os.path.join function intelligently concatenates path segments using the
correct separator for the host operating system. This prevents errors that occur when
hardcoding forward or backward slashes into script strings. Utilizing this function ensures
that automation scripts remain functional on both Linux and Windows environments.
4. What is the primary difference between the ‘r+’ and ‘w+’ file opening modes in Python?
A. ‘r+’ creates a new file if it does not exist, while ‘w+’ does not.
, B. ‘w+’ truncates the file to zero length if it exists, while ‘r+’ does not.
C. ‘r+’ is for binary files and ‘w+’ is for text files.
D. There is no difference between the two modes.
Answer: B
Rationale: The ‘r+’ mode opens a file for both reading and writing without erasing the
existing content. In contrast, ‘w+’ also allows reading and writing but will overwrite any
existing file content immediately upon opening. Understanding this distinction is critical
for data integrity when automating file updates.
5. When writing unit tests using the ‘unittest’ framework, which method is used to verify that
two values are the same?
A. self.checkEqual()
B. self.assertMatch()
C. self.verify()
D. self.assertEqual()
Answer: D
Rationale: The assertEqual method is the standard way to check for equality within a test
case class. If the two provided arguments are not equal, the test will fail and report the
discrepancy. This is a fundamental tool for verifying that automation logic produces the
expected output values.
Automation | Actual Q&A with Rationale
(D522 Exam 2) | Western Governors
University
1. When using the subprocess module, which parameter of the run() function is required to
capture the standard output of a command so it can be accessed in the script?
A. shell=True
B. check=True
C. capture_output=True
D. timeout=10
Answer: C
Rationale: The capture_output parameter is essential when the script needs to process the
result of a system command. When set to True, both stdout and stderr are captured and
stored in the returned CompletedProcess object. This allows the developer to read the
command’s response rather than just seeing it printed to the terminal.
2. In the context of Regular Expressions, what is the purpose of the ‘?’ quantifier?
A. Matches zero or more occurrences
B. Matches one or more occurrences
C. Matches exactly one occurrence
,D. Matches zero or one occurrence
Answer: D
Rationale: The question mark in a regex pattern signifies that the preceding character or
group is optional. This means it matches either zero or one instance of that specific
element. It is frequently used for handling optional characters like the ‘s’ in ‘https’ or
optional punctuation.
3. Which function from the ‘os’ module should be used to combine directory paths in a way
that is portable across different operating systems?
A. os.path.combine()
B. os.path.merge()
C. os.path.join()
D. os.path.add()
Answer: C
Rationale: The os.path.join function intelligently concatenates path segments using the
correct separator for the host operating system. This prevents errors that occur when
hardcoding forward or backward slashes into script strings. Utilizing this function ensures
that automation scripts remain functional on both Linux and Windows environments.
4. What is the primary difference between the ‘r+’ and ‘w+’ file opening modes in Python?
A. ‘r+’ creates a new file if it does not exist, while ‘w+’ does not.
, B. ‘w+’ truncates the file to zero length if it exists, while ‘r+’ does not.
C. ‘r+’ is for binary files and ‘w+’ is for text files.
D. There is no difference between the two modes.
Answer: B
Rationale: The ‘r+’ mode opens a file for both reading and writing without erasing the
existing content. In contrast, ‘w+’ also allows reading and writing but will overwrite any
existing file content immediately upon opening. Understanding this distinction is critical
for data integrity when automating file updates.
5. When writing unit tests using the ‘unittest’ framework, which method is used to verify that
two values are the same?
A. self.checkEqual()
B. self.assertMatch()
C. self.verify()
D. self.assertEqual()
Answer: D
Rationale: The assertEqual method is the standard way to check for equality within a test
case class. If the two provided arguments are not equal, the test will fail and report the
discrepancy. This is a fundamental tool for verifying that automation logic produces the
expected output values.