D 522 Exam 1 V3 | D 522 Python for IT Automation
| Actual Q&A with Rationale (D522 Exam 1) |
Western Governors University
1. When using the subprocess.run() function, which parameter is required to capture the
output of a command so it can be used later in the script?
A. capture_output=True
B. check=True
C. shell=True
D. timeout=30
Correct Answer: A
Explanation: The capture_output parameter is used to indicate that the standard output
and standard error should be captured. By setting this to True, the script can access the
command result via the stdout attribute. This is essential for automation tasks that need to
parse results from shell commands.
2. Which method should be used to retrieve a value from a dictionary while providing a
default value if the key does not exist?
A. dict.get()
B. dict.items()
C. dict.pop()
,D. dict.keys()
Correct Answer: A
Explanation: The get() method allows you to look up a key and specify a default return
value if the key is missing. This prevents the script from raising a KeyError and stopping
execution. It is a best practice in Python for safely accessing dictionary data.
3. In a Python script, what does sys.argv[0] represent?
A. The first command-line argument provided by the user
B. The name of the script being executed
C. The number of arguments passed to the script
D. The Python interpreter version
Correct Answer: B
Explanation: In the sys.argv list, the first element at index 0 is always the name of the
script itself. Subsequent elements contain the actual arguments provided by the user.
Understanding this indexing is vital for correctly parsing command-line inputs in IT
automation.
4. Which regular expression character is used to match the start of a string?
A. ^
B. *
C. $
, D. +
Correct Answer: A
Explanation: The caret symbol (^) is a boundary anchor that matches the beginning of a
line or string. It ensures that the pattern search starts specifically at the first character. This
is commonly used in log parsing to identify lines starting with specific timestamps or error
levels.
5. What is the purpose of the ‘with’ statement when opening a file in Python?
A. To ensure the file is automatically closed after the block is finished
B. To speed up the file reading process
C. To encrypt the file contents while reading
D. To allow multiple scripts to write to the file at once
Correct Answer: A
Explanation: The with statement acts as a context manager that handles resource
allocation and deallocation. It guarantees that the file will be closed even if an exception
occurs during the execution of the block. This practice prevents memory leaks and file
corruption in long-running automation tasks.
6. Which of the following functions from the ‘os’ module is used to list all files and directories
in a given path?
A. os.path.exists()
| Actual Q&A with Rationale (D522 Exam 1) |
Western Governors University
1. When using the subprocess.run() function, which parameter is required to capture the
output of a command so it can be used later in the script?
A. capture_output=True
B. check=True
C. shell=True
D. timeout=30
Correct Answer: A
Explanation: The capture_output parameter is used to indicate that the standard output
and standard error should be captured. By setting this to True, the script can access the
command result via the stdout attribute. This is essential for automation tasks that need to
parse results from shell commands.
2. Which method should be used to retrieve a value from a dictionary while providing a
default value if the key does not exist?
A. dict.get()
B. dict.items()
C. dict.pop()
,D. dict.keys()
Correct Answer: A
Explanation: The get() method allows you to look up a key and specify a default return
value if the key is missing. This prevents the script from raising a KeyError and stopping
execution. It is a best practice in Python for safely accessing dictionary data.
3. In a Python script, what does sys.argv[0] represent?
A. The first command-line argument provided by the user
B. The name of the script being executed
C. The number of arguments passed to the script
D. The Python interpreter version
Correct Answer: B
Explanation: In the sys.argv list, the first element at index 0 is always the name of the
script itself. Subsequent elements contain the actual arguments provided by the user.
Understanding this indexing is vital for correctly parsing command-line inputs in IT
automation.
4. Which regular expression character is used to match the start of a string?
A. ^
B. *
C. $
, D. +
Correct Answer: A
Explanation: The caret symbol (^) is a boundary anchor that matches the beginning of a
line or string. It ensures that the pattern search starts specifically at the first character. This
is commonly used in log parsing to identify lines starting with specific timestamps or error
levels.
5. What is the purpose of the ‘with’ statement when opening a file in Python?
A. To ensure the file is automatically closed after the block is finished
B. To speed up the file reading process
C. To encrypt the file contents while reading
D. To allow multiple scripts to write to the file at once
Correct Answer: A
Explanation: The with statement acts as a context manager that handles resource
allocation and deallocation. It guarantees that the file will be closed even if an exception
occurs during the execution of the block. This practice prevents memory leaks and file
corruption in long-running automation tasks.
6. Which of the following functions from the ‘os’ module is used to list all files and directories
in a given path?
A. os.path.exists()