D 522 Exam 3 V1 | D 522 Python for IT Automation
| Actual Q&A with Rationale (D522 Exam 3) |
Western Governors University
1. When using the ‘re’ module in Python, which function should be used to find all
occurrences of a pattern and return them as a list of strings?
A. re.search()
B. re.match()
C. re.findall()
D. re.finditer()
Correct Answer: C
Explanation: The re.findall() function is specifically designed to scan the entire input
string and return all non-overlapping matches. Unlike re.search(), which only returns the
first match found, findall provides a comprehensive list of all occurrences. This is
particularly useful in IT automation when extracting multiple data points like IP addresses
or error codes from log files.
2. Which subprocess method is recommended in Python 3.5+ to execute a command and wait
for it to finish while capturing the output?
A. subprocess.call()
B. subprocess.popen()
,C. subprocess.check_call()
D. subprocess.run()
Correct Answer: D
Explanation: The subprocess.run() method was introduced as a more flexible and
powerful replacement for older functions like call() or check_call(). It allows users to
capture stdout and stderr easily by setting the capture_output parameter to True. It is the
current standard for executing external system commands within a Python script.
3. In the context of the unittest module, which method is automatically called to prepare the
test fixture before each individual test method is run?
A. initialize()
B. prepareTest()
C. tearDown()
D. setUp()
Correct Answer: D
Explanation: The setUp() method is a core component of the unittest.TestCase class that
runs before every test case to ensure a clean state. This is useful for tasks such as opening
database connections or creating temporary files needed for testing. By using setUp,
developers can reduce code duplication across multiple test methods within the same class.
, 4. Which character is used in a Regular Expression to indicate that a pattern must appear at
the very beginning of a string?
A. $
B. *
C. ^
D. ?
Correct Answer: C
Explanation: The caret (^) symbol is known as an anchor in regular expressions that
matches the start of the string or line. If the multiline flag is not used, it strictly ensures the
match begins at the absolute start of the data. This is essential for parsing structured text
where specific identifiers are expected at the beginning of each record.
5. Which module would an IT professional use to copy a directory tree from one location to
another, including all subdirectories and files?
A. shutil
B. os
C. sys
D. pathlib
Correct Answer: A
| Actual Q&A with Rationale (D522 Exam 3) |
Western Governors University
1. When using the ‘re’ module in Python, which function should be used to find all
occurrences of a pattern and return them as a list of strings?
A. re.search()
B. re.match()
C. re.findall()
D. re.finditer()
Correct Answer: C
Explanation: The re.findall() function is specifically designed to scan the entire input
string and return all non-overlapping matches. Unlike re.search(), which only returns the
first match found, findall provides a comprehensive list of all occurrences. This is
particularly useful in IT automation when extracting multiple data points like IP addresses
or error codes from log files.
2. Which subprocess method is recommended in Python 3.5+ to execute a command and wait
for it to finish while capturing the output?
A. subprocess.call()
B. subprocess.popen()
,C. subprocess.check_call()
D. subprocess.run()
Correct Answer: D
Explanation: The subprocess.run() method was introduced as a more flexible and
powerful replacement for older functions like call() or check_call(). It allows users to
capture stdout and stderr easily by setting the capture_output parameter to True. It is the
current standard for executing external system commands within a Python script.
3. In the context of the unittest module, which method is automatically called to prepare the
test fixture before each individual test method is run?
A. initialize()
B. prepareTest()
C. tearDown()
D. setUp()
Correct Answer: D
Explanation: The setUp() method is a core component of the unittest.TestCase class that
runs before every test case to ensure a clean state. This is useful for tasks such as opening
database connections or creating temporary files needed for testing. By using setUp,
developers can reduce code duplication across multiple test methods within the same class.
, 4. Which character is used in a Regular Expression to indicate that a pattern must appear at
the very beginning of a string?
A. $
B. *
C. ^
D. ?
Correct Answer: C
Explanation: The caret (^) symbol is known as an anchor in regular expressions that
matches the start of the string or line. If the multiline flag is not used, it strictly ensures the
match begins at the absolute start of the data. This is essential for parsing structured text
where specific identifiers are expected at the beginning of each record.
5. Which module would an IT professional use to copy a directory tree from one location to
another, including all subdirectories and files?
A. shutil
B. os
C. sys
D. pathlib
Correct Answer: A