D 522 Exam 4 V2 | D 522 Python for IT Automation
| Actual Q&A with Rationale (D522 Exam 4) |
Western Governors University
1. Which module in Python is most commonly used for executing system commands and
interacting with the shell?
A. os.path
B. sys
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 tool for running external commands
over older methods like os.system. This functionality is crucial for IT automation tasks that
require interacting with legacy shell scripts or system utilities.
2. What is the primary purpose of the ‘re’ module in a Python automation script?
A. To handle mathematical operations
B. To manage remote server connections
C. To perform regular expression matching and string manipulation
D. To create graphical user interfaces
,Correct Answer: C
Explanation: The re module provides support for regular expressions which are patterns
used to match character combinations in strings. In IT automation, this is frequently used
to parse log files, extract specific data points, and validate user input. Regular expressions
allow for complex searching and text processing that standard string methods cannot easily
handle.
3. When using the ‘requests’ library, which attribute should you check to verify if a GET
request was successful (status code 200)?
A. response.ok
B. response.content
C. response.success
D. response.status_code
Correct Answer: D
Explanation: The status_code attribute returns an integer representing the HTTP status of
the response. A value of 200 indicates success, while codes like 404 or 500 indicate errors.
Checking this value is a standard practice in automation to ensure that network
communications are functioning as expected before processing data.
4. In the context of file handling, what does the ‘with’ statement ensure?
A. The file is encrypted upon closing
B. The file is opened in read-only mode by default
, C. The file is compressed to save space
D. The file is automatically closed after the block of code executes
Correct Answer: D
Explanation: The with statement is a context manager that simplifies resource
management. It ensures that the file is properly closed even if an exception occurs during
the execution of the block. This prevents file corruption and resource leaks, which are
common issues in long-running automation scripts.
5. Which of the following regex patterns would match any single digit from 0 to 9?
A.
B.
C.
D.
Correct Answer: C
Explanation: The equence is a special character in regular expressions that represents any
decimal digit. It is equivalent to the character class [0-9]. Using this shorthand makes
scripts more readable and efficient when validating numeric identifiers or ports.
6. What does the ‘os.path.exists()’ function return if a directory path is found?
A. The size of the directory
B. A boolean True
| Actual Q&A with Rationale (D522 Exam 4) |
Western Governors University
1. Which module in Python is most commonly used for executing system commands and
interacting with the shell?
A. os.path
B. sys
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 tool for running external commands
over older methods like os.system. This functionality is crucial for IT automation tasks that
require interacting with legacy shell scripts or system utilities.
2. What is the primary purpose of the ‘re’ module in a Python automation script?
A. To handle mathematical operations
B. To manage remote server connections
C. To perform regular expression matching and string manipulation
D. To create graphical user interfaces
,Correct Answer: C
Explanation: The re module provides support for regular expressions which are patterns
used to match character combinations in strings. In IT automation, this is frequently used
to parse log files, extract specific data points, and validate user input. Regular expressions
allow for complex searching and text processing that standard string methods cannot easily
handle.
3. When using the ‘requests’ library, which attribute should you check to verify if a GET
request was successful (status code 200)?
A. response.ok
B. response.content
C. response.success
D. response.status_code
Correct Answer: D
Explanation: The status_code attribute returns an integer representing the HTTP status of
the response. A value of 200 indicates success, while codes like 404 or 500 indicate errors.
Checking this value is a standard practice in automation to ensure that network
communications are functioning as expected before processing data.
4. In the context of file handling, what does the ‘with’ statement ensure?
A. The file is encrypted upon closing
B. The file is opened in read-only mode by default
, C. The file is compressed to save space
D. The file is automatically closed after the block of code executes
Correct Answer: D
Explanation: The with statement is a context manager that simplifies resource
management. It ensures that the file is properly closed even if an exception occurs during
the execution of the block. This prevents file corruption and resource leaks, which are
common issues in long-running automation scripts.
5. Which of the following regex patterns would match any single digit from 0 to 9?
A.
B.
C.
D.
Correct Answer: C
Explanation: The equence is a special character in regular expressions that represents any
decimal digit. It is equivalent to the character class [0-9]. Using this shorthand makes
scripts more readable and efficient when validating numeric identifiers or ports.
6. What does the ‘os.path.exists()’ function return if a directory path is found?
A. The size of the directory
B. A boolean True