D 522 Exam 2 V1 | 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 in subprocess.run() is used to
capture output that would normally be sent to the console?
A. output_stream
B. capture_output
C. save_stdout
D. log_results
Correct Answer: B
Explanation: The capture_output parameter is set to True to capture both stdout and
stderr. By default, subprocess.run() displays output to the screen without storing it. Setting
this to True allows the developer to access the data via the stdout and stderr attributes of
the returned object.
2. Which regular expression symbol is used to match the start of a string in Python’s re
module?
A. $
B. ^
C. *
,D. &
Correct Answer: B
Explanation: The caret symbol (^) serves as an anchor in regular expressions to signify the
beginning of a line or string. It ensures that the pattern match only occurs if it is found at
the very start. This is commonly used in log analysis to find entries that begin with specific
timestamps or levels.
3. In the context of Python unit testing, what is the primary purpose of the setUp() method?
A. To configure the test environment before each individual test method
B. To define the sequence in which tests are executed
C. To clean up resources after all tests run
D. To generate the final test report
Correct Answer: A
Explanation: The setUp() method is part of the unittest.TestCase class and runs
automatically before every test method. It is used to initialize variables, open files, or
establish database connections needed for testing. This ensures that every test starts with a
fresh, consistent environment.
4. Which value does a successful subprocess return code typically equate to in a Linux-based
environment?
A. 1
, B. 0
C. -1
D. None
Correct Answer: B
Explanation: A return code of 0 indicates that the process finished successfully without
errors. Any non-zero return code is generally interpreted as a failure or a specific error
state. IT automation scripts frequently check this return code to decide if subsequent tasks
should proceed.
5. What is the result of using the re.search() function if no match is found in the target string?
A. It returns None
B. It returns an empty string
C. It returns a False boolean
D. It raises a MatchNotFoundError
Correct Answer: A
Explanation: When re.search() fails to find a pattern, it returns the Python None object
rather than raising an exception. Developers must check if the result is not None before
attempting to access groups or match data. This behavior allows for simple conditional
logic in search loops.
| Actual Q&A with Rationale (D522 Exam 2) |
Western Governors University
1. When using the subprocess module, which parameter in subprocess.run() is used to
capture output that would normally be sent to the console?
A. output_stream
B. capture_output
C. save_stdout
D. log_results
Correct Answer: B
Explanation: The capture_output parameter is set to True to capture both stdout and
stderr. By default, subprocess.run() displays output to the screen without storing it. Setting
this to True allows the developer to access the data via the stdout and stderr attributes of
the returned object.
2. Which regular expression symbol is used to match the start of a string in Python’s re
module?
A. $
B. ^
C. *
,D. &
Correct Answer: B
Explanation: The caret symbol (^) serves as an anchor in regular expressions to signify the
beginning of a line or string. It ensures that the pattern match only occurs if it is found at
the very start. This is commonly used in log analysis to find entries that begin with specific
timestamps or levels.
3. In the context of Python unit testing, what is the primary purpose of the setUp() method?
A. To configure the test environment before each individual test method
B. To define the sequence in which tests are executed
C. To clean up resources after all tests run
D. To generate the final test report
Correct Answer: A
Explanation: The setUp() method is part of the unittest.TestCase class and runs
automatically before every test method. It is used to initialize variables, open files, or
establish database connections needed for testing. This ensures that every test starts with a
fresh, consistent environment.
4. Which value does a successful subprocess return code typically equate to in a Linux-based
environment?
A. 1
, B. 0
C. -1
D. None
Correct Answer: B
Explanation: A return code of 0 indicates that the process finished successfully without
errors. Any non-zero return code is generally interpreted as a failure or a specific error
state. IT automation scripts frequently check this return code to decide if subsequent tasks
should proceed.
5. What is the result of using the re.search() function if no match is found in the target string?
A. It returns None
B. It returns an empty string
C. It returns a False boolean
D. It raises a MatchNotFoundError
Correct Answer: A
Explanation: When re.search() fails to find a pattern, it returns the Python None object
rather than raising an exception. Developers must check if the result is not None before
attempting to access groups or match data. This behavior allows for simple conditional
logic in search loops.