D 522 Final Exam V2 | D 522 Python for IT
Automation | Actual Q&A with Rationale (D522
Final Exam) | Western Governors University
1. Which module in Python is most commonly used for executing external commands and
capturing their output in an automation script?
A. sys
B. os.path
C. subprocess
D. shutil
Correct Answer: C
Explanation: The subprocess module allows you to spawn new processes and connect to
their input/output/error pipes. It is the recommended replacement for older functions like
os.system. Using subprocess.run() provides a high-level interface for most automation
tasks requiring command-line execution.
2. In the context of regular expressions, which character is used to match one or more
occurrences of the preceding character?
A. ?
B. Plus (+)
C. {
,D. .
Correct Answer: B
Explanation: The plus sign (+) is a quantifier in regular expressions that matches one or
more repetitions. It ensures that the preceding element appears at least once in the target
string. This is distinct from the asterisk (*), which matches zero or more occurrences.
3. What does the ‘sys.argv[0]’ represent when a Python script is executed from the command
line?
A. The first argument passed to the script
B. The current working directory
C. The total number of arguments
D. The name of the script itself
Correct Answer: D
Explanation: In Python, sys.argv is a list containing the command-line arguments passed
to the script. The element at index 0 is always the path or filename of the script being run.
Subsequent arguments provided by the user occupy indices 1 and higher.
4. Which method is used in the ‘unittest’ framework to verify that a specific exception is
raised by a piece of code?
A. expectException()
B. assertError()
, C. assertRaises()
D. checkFailure()
Correct Answer: C
Explanation: The assertRaises() method is used within unit tests to confirm that a block of
code triggers a designated exception. It is often used as a context manager with the ‘with’
statement for clarity. Testing for expected failures is a critical component of robust
automation scripting.
5. When opening a file for logging purposes where new entries should be added to the end
without deleting existing content, which mode should be used?
A. ‘w’
B. ‘r+’
C. ‘a’
D. ‘x’
Correct Answer: C
Explanation: The ‘a’ mode stands for ‘append,’ which opens a file and places the cursor at
the end for writing. If the file does not exist, it will be created automatically. This is
essential for log files to ensure previous data is preserved while adding new information.
Automation | Actual Q&A with Rationale (D522
Final Exam) | Western Governors University
1. Which module in Python is most commonly used for executing external commands and
capturing their output in an automation script?
A. sys
B. os.path
C. subprocess
D. shutil
Correct Answer: C
Explanation: The subprocess module allows you to spawn new processes and connect to
their input/output/error pipes. It is the recommended replacement for older functions like
os.system. Using subprocess.run() provides a high-level interface for most automation
tasks requiring command-line execution.
2. In the context of regular expressions, which character is used to match one or more
occurrences of the preceding character?
A. ?
B. Plus (+)
C. {
,D. .
Correct Answer: B
Explanation: The plus sign (+) is a quantifier in regular expressions that matches one or
more repetitions. It ensures that the preceding element appears at least once in the target
string. This is distinct from the asterisk (*), which matches zero or more occurrences.
3. What does the ‘sys.argv[0]’ represent when a Python script is executed from the command
line?
A. The first argument passed to the script
B. The current working directory
C. The total number of arguments
D. The name of the script itself
Correct Answer: D
Explanation: In Python, sys.argv is a list containing the command-line arguments passed
to the script. The element at index 0 is always the path or filename of the script being run.
Subsequent arguments provided by the user occupy indices 1 and higher.
4. Which method is used in the ‘unittest’ framework to verify that a specific exception is
raised by a piece of code?
A. expectException()
B. assertError()
, C. assertRaises()
D. checkFailure()
Correct Answer: C
Explanation: The assertRaises() method is used within unit tests to confirm that a block of
code triggers a designated exception. It is often used as a context manager with the ‘with’
statement for clarity. Testing for expected failures is a critical component of robust
automation scripting.
5. When opening a file for logging purposes where new entries should be added to the end
without deleting existing content, which mode should be used?
A. ‘w’
B. ‘r+’
C. ‘a’
D. ‘x’
Correct Answer: C
Explanation: The ‘a’ mode stands for ‘append,’ which opens a file and places the cursor at
the end for writing. If the file does not exist, it will be created automatically. This is
essential for log files to ensure previous data is preserved while adding new information.