D 522 Final Exam V3 | D 522 Python for IT
Automation | Actual Q&A with Rationale
(D522 Final Exam) | Western Governors
University
1. Which function from the ‘os’ module is used to check if a specific file or directory path
currently exists on the system?
A. os.path.isfile()
B. os.path.exists()
C. os.access()
D. os.path.isdir()
Answer: B
Rationale: The os.path.exists() function is the primary tool for verifying the presence of a
file or directory at a given path. It returns True if the path is valid and False if the location
does not exist. This is a fundamental check in automation to prevent execution errors when
resources are missing.
2. In the ‘sys’ module, what information is typically stored at index 0 of the ‘sys.argv’ list?
A. The first command-line argument provided by the user
B. The name of the script being executed
C. The total number of arguments passed to the script
,D. The current working directory of the process
Answer: B
Rationale: In Python, sys.argv[0] contains the name of the script that is currently running.
All subsequent elements in the list represent the additional arguments passed via the
terminal. Understanding this indexing is crucial for parsing user input correctly in CLI
automation tools.
3. When using the ‘re’ module, which method should be used 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.finditer()
Answer: B
Rationale: The re.findall() method scans the entire string and extracts every substring that
matches the provided regular expression. Unlike re.search(), which only finds the first
match, findall() compiles every instance into a list. This is particularly useful for log parsing
when multiple entries need to be analyzed.
4. Which parameter in ‘subprocess.run()’ must be set to True to capture the output of a
command so it can be stored in a variable?
A. shell
, B. check
C. text
D. capture_output
Answer: D
Rationale: The capture_output parameter tells Python to redirect standard output and
standard error into the CompletedProcess object. By setting this to True, developers can
access the result using the .stdout attribute. Without this parameter, the command output
would simply print to the console instead of being saved.
5. What is the result of opening a file in ‘a’ mode (append mode) and writing data to it?
A. The existing contents of the file are deleted and replaced
B. A new file is created only if the file does not already exist
C. The file is opened for reading and writing simultaneously
D. The new data is added to the end of the existing file content
Answer: D
Rationale: Append mode (‘a’) ensures that any data written to the file is placed at the very
end of the existing content. Unlike write mode (‘w’), it does not truncate or clear the file
before writing. This is the standard mode used for generating continuous logs in
automation workflows.
Automation | Actual Q&A with Rationale
(D522 Final Exam) | Western Governors
University
1. Which function from the ‘os’ module is used to check if a specific file or directory path
currently exists on the system?
A. os.path.isfile()
B. os.path.exists()
C. os.access()
D. os.path.isdir()
Answer: B
Rationale: The os.path.exists() function is the primary tool for verifying the presence of a
file or directory at a given path. It returns True if the path is valid and False if the location
does not exist. This is a fundamental check in automation to prevent execution errors when
resources are missing.
2. In the ‘sys’ module, what information is typically stored at index 0 of the ‘sys.argv’ list?
A. The first command-line argument provided by the user
B. The name of the script being executed
C. The total number of arguments passed to the script
,D. The current working directory of the process
Answer: B
Rationale: In Python, sys.argv[0] contains the name of the script that is currently running.
All subsequent elements in the list represent the additional arguments passed via the
terminal. Understanding this indexing is crucial for parsing user input correctly in CLI
automation tools.
3. When using the ‘re’ module, which method should be used 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.finditer()
Answer: B
Rationale: The re.findall() method scans the entire string and extracts every substring that
matches the provided regular expression. Unlike re.search(), which only finds the first
match, findall() compiles every instance into a list. This is particularly useful for log parsing
when multiple entries need to be analyzed.
4. Which parameter in ‘subprocess.run()’ must be set to True to capture the output of a
command so it can be stored in a variable?
A. shell
, B. check
C. text
D. capture_output
Answer: D
Rationale: The capture_output parameter tells Python to redirect standard output and
standard error into the CompletedProcess object. By setting this to True, developers can
access the result using the .stdout attribute. Without this parameter, the command output
would simply print to the console instead of being saved.
5. What is the result of opening a file in ‘a’ mode (append mode) and writing data to it?
A. The existing contents of the file are deleted and replaced
B. A new file is created only if the file does not already exist
C. The file is opened for reading and writing simultaneously
D. The new data is added to the end of the existing file content
Answer: D
Rationale: Append mode (‘a’) ensures that any data written to the file is placed at the very
end of the existing content. Unlike write mode (‘w’), it does not truncate or clear the file
before writing. This is the standard mode used for generating continuous logs in
automation workflows.