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 utilized to find all
occurrences of a pattern within a string and return them as a list?
A. re.search()
B. re.findall()
C. re.match()
D. re.split()
Answer: B
Rationale: The re.findall() function scans the entire string and returns all non-
overlapping matches of a pattern as a list of strings. This is particularly useful for extracting
data like IP addresses or dates from log files where multiple instances may occur. In
contrast, re.search() only returns the first match found, and re.match() checks only
the beginning of the string.
2. Which method from the os module is the most appropriate for checking if a specific file
exists at a given path before attempting to open it?
A. os.access()
,B. os.path.isfile()
C. os.path.exists()
D. os.mkdir()
Answer: B
Rationale: The os.path.isfile() function specifically checks if the target path points to
a regular file, ensuring that the script does not try to process a directory as a file. While
os.path.exists() confirms the path is valid, it does not distinguish between files and
directories. Using this check helps prevent FileNotFoundError exceptions during
automation tasks.
3. In the context of the csv module, what is the primary advantage of using
csv.DictReader over the standard csv.reader?
A. It processes data faster in large datasets.
B. It automatically encrypts the data during the read process.
C. It maps the information in each row to a dictionary using fieldnames.
D. It handles binary files more efficiently than text files.
Answer: C
Rationale: csv.DictReader creates an object that maps the information in each row to a
dictionary whose keys are given by the optional fieldnames parameter or inferred from the
first row. This makes the code much more readable and maintainable because values can
, be accessed via column names rather than integer indices. It reduces errors when the CSV
structure changes, such as when columns are rearranged.
4. A system administrator needs to run an external command from a Python script and
capture its output for logging. Which module is best suited for this task?
A. sys
B. subprocess
C. logging
D. platform
Answer: B
Rationale: The subprocess module allows Python to spawn new processes, connect to
their input/output/error pipes, and obtain their return codes. It is the recommended
standard for executing shell commands and replacing older functions like os.system. This
is critical for IT automation when interacting with legacy scripts or system utilities.
5. Which regular expression character is used to match the start of a string?
A. $
B. ^
C. *
D. +
Answer: B
Automation | Actual Q&A with Rationale
(D522 Exam 3) | Western Governors
University
1. When using the re module in Python, which function should be utilized to find all
occurrences of a pattern within a string and return them as a list?
A. re.search()
B. re.findall()
C. re.match()
D. re.split()
Answer: B
Rationale: The re.findall() function scans the entire string and returns all non-
overlapping matches of a pattern as a list of strings. This is particularly useful for extracting
data like IP addresses or dates from log files where multiple instances may occur. In
contrast, re.search() only returns the first match found, and re.match() checks only
the beginning of the string.
2. Which method from the os module is the most appropriate for checking if a specific file
exists at a given path before attempting to open it?
A. os.access()
,B. os.path.isfile()
C. os.path.exists()
D. os.mkdir()
Answer: B
Rationale: The os.path.isfile() function specifically checks if the target path points to
a regular file, ensuring that the script does not try to process a directory as a file. While
os.path.exists() confirms the path is valid, it does not distinguish between files and
directories. Using this check helps prevent FileNotFoundError exceptions during
automation tasks.
3. In the context of the csv module, what is the primary advantage of using
csv.DictReader over the standard csv.reader?
A. It processes data faster in large datasets.
B. It automatically encrypts the data during the read process.
C. It maps the information in each row to a dictionary using fieldnames.
D. It handles binary files more efficiently than text files.
Answer: C
Rationale: csv.DictReader creates an object that maps the information in each row to a
dictionary whose keys are given by the optional fieldnames parameter or inferred from the
first row. This makes the code much more readable and maintainable because values can
, be accessed via column names rather than integer indices. It reduces errors when the CSV
structure changes, such as when columns are rearranged.
4. A system administrator needs to run an external command from a Python script and
capture its output for logging. Which module is best suited for this task?
A. sys
B. subprocess
C. logging
D. platform
Answer: B
Rationale: The subprocess module allows Python to spawn new processes, connect to
their input/output/error pipes, and obtain their return codes. It is the recommended
standard for executing shell commands and replacing older functions like os.system. This
is critical for IT automation when interacting with legacy scripts or system utilities.
5. Which regular expression character is used to match the start of a string?
A. $
B. ^
C. *
D. +
Answer: B