D 522 Exam 4 V1 | D 522 Python for IT
Automation | Actual Q&A with Rationale
(D522 Exam 4) | Western Governors
University
1. When using the os module, which function is best suited to verify if a file exists before
attempting to read its contents?
A. os.path.exists()
B. os.path.isfile()
C. os.access()
D. os.check_path()
Answer: A
Rationale: The os.path.exists() function returns True if a file or directory at the specified
path exists. This is a fundamental safety check in automation scripts to prevent
FileNotFoundError exceptions. Using this ensures that the script logic remains robust
when dealing with dynamic file system environments.
2. In the re module, what is the primary difference between re.search() and re.match()?
A. re.search() scans the whole string for a match anywhere, while re.match() checks only at
the beginning.
B. re.match() scans the entire string for any occurrence.
,C. re.search() looks for patterns only at the beginning of a string.
D. There is no functional difference between the two methods.
Answer: A
Rationale: The re.search() function is more flexible for log analysis because it finds
patterns regardless of their position. In contrast, re.match() will fail if the pattern is not
found at the very start of the string. Understanding this distinction is critical for IT
professionals who need to parse complex data streams.
3. Which subprocess.run() argument is used to capture the standard output and standard
error of a command?
A. capture_output=True
B. shell=True
C. check=True
D. stdout=PIPE
Answer: A
Rationale: The capture_output=True argument allows a script to store the result of an
external command in the CompletedProcess object. This is essential for processing the
results of shell commands within a Python workflow. Without this, the output is printed
directly to the terminal rather than being saved for variable manipulation.
, 4. What is the significance of the sys.argv list in Python automation scripts?
A. It stores the local environment variables of the operating system.
B. It tracks the memory usage of the running process.
C. It defines the system path for importing external libraries.
D. It is used to store the command-line arguments passed to the script.
Answer: D
Rationale: The sys.argv list is the standard way to accept user inputs or flags when
running a script from the command line. The first element, sys.argv[0], always refers to the
script name itself. This allows administrators to create versatile tools that behave
differently based on the parameters provided at runtime.
5. Which unittest method is used to verify that two values are identical during a test case?
A. assertValue()
B. assertEqual()
C. assertSame()
D. assertTrue()
Answer: B
Rationale: The assertEqual() method is the core tool for checking that the output of a
function matches the expected result. If the values do not match, the test fails and provides
Automation | Actual Q&A with Rationale
(D522 Exam 4) | Western Governors
University
1. When using the os module, which function is best suited to verify if a file exists before
attempting to read its contents?
A. os.path.exists()
B. os.path.isfile()
C. os.access()
D. os.check_path()
Answer: A
Rationale: The os.path.exists() function returns True if a file or directory at the specified
path exists. This is a fundamental safety check in automation scripts to prevent
FileNotFoundError exceptions. Using this ensures that the script logic remains robust
when dealing with dynamic file system environments.
2. In the re module, what is the primary difference between re.search() and re.match()?
A. re.search() scans the whole string for a match anywhere, while re.match() checks only at
the beginning.
B. re.match() scans the entire string for any occurrence.
,C. re.search() looks for patterns only at the beginning of a string.
D. There is no functional difference between the two methods.
Answer: A
Rationale: The re.search() function is more flexible for log analysis because it finds
patterns regardless of their position. In contrast, re.match() will fail if the pattern is not
found at the very start of the string. Understanding this distinction is critical for IT
professionals who need to parse complex data streams.
3. Which subprocess.run() argument is used to capture the standard output and standard
error of a command?
A. capture_output=True
B. shell=True
C. check=True
D. stdout=PIPE
Answer: A
Rationale: The capture_output=True argument allows a script to store the result of an
external command in the CompletedProcess object. This is essential for processing the
results of shell commands within a Python workflow. Without this, the output is printed
directly to the terminal rather than being saved for variable manipulation.
, 4. What is the significance of the sys.argv list in Python automation scripts?
A. It stores the local environment variables of the operating system.
B. It tracks the memory usage of the running process.
C. It defines the system path for importing external libraries.
D. It is used to store the command-line arguments passed to the script.
Answer: D
Rationale: The sys.argv list is the standard way to accept user inputs or flags when
running a script from the command line. The first element, sys.argv[0], always refers to the
script name itself. This allows administrators to create versatile tools that behave
differently based on the parameters provided at runtime.
5. Which unittest method is used to verify that two values are identical during a test case?
A. assertValue()
B. assertEqual()
C. assertSame()
D. assertTrue()
Answer: B
Rationale: The assertEqual() method is the core tool for checking that the output of a
function matches the expected result. If the values do not match, the test fails and provides