D 522 Exam 3 V2 | 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 an IT professional use to find
all occurrences of a pattern within a string?
A. re.search()
B. re.findall()
C. re.match()
D. re.split()
Answer: B
Rationale: The re.findall() function is designed to scan the entire string and return all non-
overlapping matches of a pattern as a list of strings. In contrast, re.search() only identifies
the first occurrence of the pattern. Using findall ensures that every instance of the data,
such as IP addresses in a log file, is captured for processing.
2. An automation script needs to check if a specific command-line argument was provided.
Which index of ‘sys.argv’ contains the name of the script itself?
A. sys.argv.name
B. sys.argv[1]
,C. sys.argv[-1]
D. sys.argv[0]
Answer: D
Rationale: The sys.argv list is the standard way to access command-line arguments in
Python scripts. The element at index 0 is always the name of the script being executed by
the interpreter. To access actual user-provided arguments, the script must look at index 1
and beyond.
3. Which subprocess method is recommended for executing a command and waiting for it to
complete in Python 3.5+?
A. subprocess.call()
B. subprocess.exec()
C. subprocess.popen()
D. subprocess.run()
Answer: D
Rationale: The subprocess.run() function was introduced to simplify the process of
starting a process and waiting for it to finish. It returns a CompletedProcess instance which
includes information about the exit status and output. While subprocess.call exists for
backward compatibility, run() is the current standard for basic automation tasks.
, 4. What is the purpose of the ‘capture_output=True’ argument in the subprocess.run()
function?
A. It displays the output directly to the user’s terminal.
B. It saves the output to a text file on the local disk.
C. It prevents the command from generating any output at all.
D. It allows the script to store stdout and stderr in the returned object.
Answer: D
Rationale: By setting capture_output to True, the script instructs the subprocess to
redirect standard output and standard error to pipes. These streams can then be accessed
via the stdout and stderr attributes of the returned CompletedProcess object. This is
essential for scripts that need to parse the results of a system command to make decisions.
5. Which regular expression metacharacter is used to match the start of a string?
A. $
B. *
C. ^
D. +
Answer: C
Rationale: The caret symbol (^) is an anchor in regular expressions that specifies the
match must occur at the beginning of the string. This is useful for identifying log entries
Automation | Actual Q&A with Rationale
(D522 Exam 3) | Western Governors
University
1. When using the ‘re’ module in Python, which function should an IT professional use to find
all occurrences of a pattern within a string?
A. re.search()
B. re.findall()
C. re.match()
D. re.split()
Answer: B
Rationale: The re.findall() function is designed to scan the entire string and return all non-
overlapping matches of a pattern as a list of strings. In contrast, re.search() only identifies
the first occurrence of the pattern. Using findall ensures that every instance of the data,
such as IP addresses in a log file, is captured for processing.
2. An automation script needs to check if a specific command-line argument was provided.
Which index of ‘sys.argv’ contains the name of the script itself?
A. sys.argv.name
B. sys.argv[1]
,C. sys.argv[-1]
D. sys.argv[0]
Answer: D
Rationale: The sys.argv list is the standard way to access command-line arguments in
Python scripts. The element at index 0 is always the name of the script being executed by
the interpreter. To access actual user-provided arguments, the script must look at index 1
and beyond.
3. Which subprocess method is recommended for executing a command and waiting for it to
complete in Python 3.5+?
A. subprocess.call()
B. subprocess.exec()
C. subprocess.popen()
D. subprocess.run()
Answer: D
Rationale: The subprocess.run() function was introduced to simplify the process of
starting a process and waiting for it to finish. It returns a CompletedProcess instance which
includes information about the exit status and output. While subprocess.call exists for
backward compatibility, run() is the current standard for basic automation tasks.
, 4. What is the purpose of the ‘capture_output=True’ argument in the subprocess.run()
function?
A. It displays the output directly to the user’s terminal.
B. It saves the output to a text file on the local disk.
C. It prevents the command from generating any output at all.
D. It allows the script to store stdout and stderr in the returned object.
Answer: D
Rationale: By setting capture_output to True, the script instructs the subprocess to
redirect standard output and standard error to pipes. These streams can then be accessed
via the stdout and stderr attributes of the returned CompletedProcess object. This is
essential for scripts that need to parse the results of a system command to make decisions.
5. Which regular expression metacharacter is used to match the start of a string?
A. $
B. *
C. ^
D. +
Answer: C
Rationale: The caret symbol (^) is an anchor in regular expressions that specifies the
match must occur at the beginning of the string. This is useful for identifying log entries