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 re module to find a pattern at the very beginning of a string, which
function is the most appropriate to use?
A. re.search()
B. re.match()
C. re.findall()
D. re.split()
Correct Answer: B
Explanation: The re.match() function is specifically designed to check for a match only
at the beginning of a string. If the pattern is found anywhere else in the string, it will return
None. This distinguishes it from re.search(), which scans the entire string for the first
location where the pattern occurs.
2. Which parameter in subprocess.run() allows the capture of standard output and
standard error for further processing in a Python script?
A. shell=True
B. check=True
C. capture_output=True
,D. timeout=60
Correct Answer: C
Explanation: The capture_output=True parameter automatically sets stdout and
stderr to subprocess.PIPE, allowing the script to access the command results via the
stdout and stderr attributes. This is essential for automation tasks that need to parse the
output of a system command to make decisions. Without this setting, the output is typically
sent directly to the console and is not accessible by the variable capturing the process
result.
3. In the unittest framework, which method is automatically called before every individual
test method to prepare the test environment?
A. tearDown()
B. setUp()
C. init()
D. runTest()
Correct Answer: B
Explanation: The setUp() method is executed by the test runner immediately before each
test case is run. This ensures that every test starts with a clean and consistent environment,
such as a fresh database connection or a specific file structure. It is a fundamental part of
the test lifecycle within the unittest module to ensure test isolation.
, 4. What is the default mode when opening a file using the open() function in Python?
A. ‘w’ (Write)
B. ‘a’ (Append)
C. ‘r’ (Read Text)
D. ‘rb’ (Read Binary)
Correct Answer: C
Explanation: The default mode for the open() function is ‘r’, which stands for reading in
text mode. If the file does not exist, this mode will raise a FileNotFoundError rather than
creating a new file. Developers should explicitly state the mode if writing or binary
operations are required to avoid logical errors in the script.
5. Which regular expression character represents any single digit from 0 to 9?
A.
B.
C.
D.
Correct Answer: C
Explanation: The \d character class is a shorthand for [0-9] in Python’s re module. It is
frequently used in automation to extract numerical data such as IDs, phone numbers, or
quantities from log files. Conversely, \D matches any character that is not a digit.
| Actual Q&A with Rationale (D522 Exam 2) |
Western Governors University
1. When using the re module to find a pattern at the very beginning of a string, which
function is the most appropriate to use?
A. re.search()
B. re.match()
C. re.findall()
D. re.split()
Correct Answer: B
Explanation: The re.match() function is specifically designed to check for a match only
at the beginning of a string. If the pattern is found anywhere else in the string, it will return
None. This distinguishes it from re.search(), which scans the entire string for the first
location where the pattern occurs.
2. Which parameter in subprocess.run() allows the capture of standard output and
standard error for further processing in a Python script?
A. shell=True
B. check=True
C. capture_output=True
,D. timeout=60
Correct Answer: C
Explanation: The capture_output=True parameter automatically sets stdout and
stderr to subprocess.PIPE, allowing the script to access the command results via the
stdout and stderr attributes. This is essential for automation tasks that need to parse the
output of a system command to make decisions. Without this setting, the output is typically
sent directly to the console and is not accessible by the variable capturing the process
result.
3. In the unittest framework, which method is automatically called before every individual
test method to prepare the test environment?
A. tearDown()
B. setUp()
C. init()
D. runTest()
Correct Answer: B
Explanation: The setUp() method is executed by the test runner immediately before each
test case is run. This ensures that every test starts with a clean and consistent environment,
such as a fresh database connection or a specific file structure. It is a fundamental part of
the test lifecycle within the unittest module to ensure test isolation.
, 4. What is the default mode when opening a file using the open() function in Python?
A. ‘w’ (Write)
B. ‘a’ (Append)
C. ‘r’ (Read Text)
D. ‘rb’ (Read Binary)
Correct Answer: C
Explanation: The default mode for the open() function is ‘r’, which stands for reading in
text mode. If the file does not exist, this mode will raise a FileNotFoundError rather than
creating a new file. Developers should explicitly state the mode if writing or binary
operations are required to avoid logical errors in the script.
5. Which regular expression character represents any single digit from 0 to 9?
A.
B.
C.
D.
Correct Answer: C
Explanation: The \d character class is a shorthand for [0-9] in Python’s re module. It is
frequently used in automation to extract numerical data such as IDs, phone numbers, or
quantities from log files. Conversely, \D matches any character that is not a digit.