D 522 Exam 4 V3 | D 522 Python for IT Automation
| Actual Q&A with Rationale (D522 Exam 4) |
Western Governors University
1. When using the ‘re’ module to validate a configuration file, which function should be used
to find all non-overlapping matches of a pattern in a string?
A. re.search()
B. re.split()
C. re.match()
D. re.findall()
Correct Answer: D
Explanation: The re.findall() function is designed to return all non-overlapping matches of
a pattern in a string as a list of strings. This is particularly useful in IT automation when
extracting multiple instances of data like IP addresses or error codes from logs. Unlike
re.search(), which only finds the first occurrence, findall provides a comprehensive
collection of all matches encountered.
2. An automation script needs to check if a specific directory exists before creating a log file.
Which method from the ‘os’ module is most appropriate?
A. os.path.isfile()
B. os.path.isdir()
,C. os.mkdir()
D. os.getcwd()
Correct Answer: B
Explanation: The os.path.isdir() function returns True if the specified path is an existing
directory. Using this check prevents the script from crashing when trying to access or
create files in non-existent locations. It is a fundamental safety practice in script
development to verify environment states before executing file I/O operations.
3. Which method is used to send a specific signal to a process using the ‘subprocess’ module?
A. Popen.kill()
B. Popen.terminate()
C. Popen.send_signal()
D. Popen.wait()
Correct Answer: C
Explanation: The Popen.send_signal() method allows a script to send a specific signal, such
as SIGTERM or SIGKILL, to a running subprocess. This provides granular control over
process management and lifecycle within an automation workflow. Relying on this method
ensures that external tasks can be managed dynamically based on system requirements.
4. In Python, how is the ‘with’ statement beneficial when handling file operations for IT logs?
A. It speeds up the file reading process significantly.
, B. It automatically closes the file after the block of code is executed.
C. It encrypts the log data to ensure security.
D. It allows the file to be edited by multiple users simultaneously.
Correct Answer: B
Explanation: The ‘with’ statement acts as a context manager that ensures resources are
properly cleaned up. Specifically, it guarantees that a file is closed immediately after the
nested block finishes, even if an exception occurs. This is vital in automation to prevent
resource leaks and file locking issues on production servers.
5. Which regular expression character is used to match the start of a string in a log entry?
A. ^
B. *
C. $
D. +
Correct Answer: A
Explanation: The caret symbol (^) is a regex anchor that matches the position at the very
beginning of a string or line. It is commonly used in IT automation to ensure that a pattern,
such as a timestamp, appears at the start of a log line. Without this anchor, the regex might
return false positives from the middle of the text.
| Actual Q&A with Rationale (D522 Exam 4) |
Western Governors University
1. When using the ‘re’ module to validate a configuration file, which function should be used
to find all non-overlapping matches of a pattern in a string?
A. re.search()
B. re.split()
C. re.match()
D. re.findall()
Correct Answer: D
Explanation: The re.findall() function is designed to return all non-overlapping matches of
a pattern in a string as a list of strings. This is particularly useful in IT automation when
extracting multiple instances of data like IP addresses or error codes from logs. Unlike
re.search(), which only finds the first occurrence, findall provides a comprehensive
collection of all matches encountered.
2. An automation script needs to check if a specific directory exists before creating a log file.
Which method from the ‘os’ module is most appropriate?
A. os.path.isfile()
B. os.path.isdir()
,C. os.mkdir()
D. os.getcwd()
Correct Answer: B
Explanation: The os.path.isdir() function returns True if the specified path is an existing
directory. Using this check prevents the script from crashing when trying to access or
create files in non-existent locations. It is a fundamental safety practice in script
development to verify environment states before executing file I/O operations.
3. Which method is used to send a specific signal to a process using the ‘subprocess’ module?
A. Popen.kill()
B. Popen.terminate()
C. Popen.send_signal()
D. Popen.wait()
Correct Answer: C
Explanation: The Popen.send_signal() method allows a script to send a specific signal, such
as SIGTERM or SIGKILL, to a running subprocess. This provides granular control over
process management and lifecycle within an automation workflow. Relying on this method
ensures that external tasks can be managed dynamically based on system requirements.
4. In Python, how is the ‘with’ statement beneficial when handling file operations for IT logs?
A. It speeds up the file reading process significantly.
, B. It automatically closes the file after the block of code is executed.
C. It encrypts the log data to ensure security.
D. It allows the file to be edited by multiple users simultaneously.
Correct Answer: B
Explanation: The ‘with’ statement acts as a context manager that ensures resources are
properly cleaned up. Specifically, it guarantees that a file is closed immediately after the
nested block finishes, even if an exception occurs. This is vital in automation to prevent
resource leaks and file locking issues on production servers.
5. Which regular expression character is used to match the start of a string in a log entry?
A. ^
B. *
C. $
D. +
Correct Answer: A
Explanation: The caret symbol (^) is a regex anchor that matches the position at the very
beginning of a string or line. It is commonly used in IT automation to ensure that a pattern,
such as a timestamp, appears at the start of a log line. Without this anchor, the regex might
return false positives from the middle of the text.