D 522 Exam 1 V1 | D 522 Python for IT
Automation | Actual Q&A with Rationale
(D522 Exam 1) | Western Governors
University
1. Which module in Python is most commonly used for interacting with the operating system
to perform tasks like listing directory contents?
A. sys
B. os
C. subprocess
D. shutil
Answer: B
Rationale: The os module provides a portable way of using operating system dependent
functionality. While subprocess is used for external commands, the os module is
specifically designed for file system navigation and environment interaction. This is a
fundamental concept in IT automation for managing local file structures.
2. When using the ‘csv’ module to read a file, which object type does ‘csv.DictReader’ return
for each row in the iteration?
A. Dictionary
B. Tuple
,C. List
D. String
Answer: A
Rationale: The csv.DictReader function maps the information in each row to an
OrderedDict, where keys are the column headers. This allows developers to access data by
column name rather than index, improving code readability. Using dictionaries for data
manipulation is a key skill in Python for IT Automation workflows.
3. What is the primary purpose of the ‘with’ statement when opening a file in Python?
A. To increase the speed of reading the file
B. To encrypt the file contents upon opening
C. To automatically handle file closing even if an exception occurs
D. To allow multiple users to write to the file simultaneously
Answer: C
Rationale: The ‘with’ statement simplifies exception handling by encapsulating common
preparation and cleanup tasks. It ensures that the file is properly closed after the block of
code is finished, preventing resource leaks. This practice is essential for robust automation
scripts that handle many files.
4. In a regular expression, which special character is used to match the start of a string?
A. $
, B. ^
C. .
D. []
Answer: B
Rationale: The caret character (^) anchors the search to the beginning of the string or line.
This is frequently used in log analysis to find lines starting with specific timestamps or
error levels. Mastering regex anchors is vital for precise text processing in IT environments.
5. Which method from the ‘subprocess’ module is recommended for executing a command
and waiting for it to complete in Python 3.5+?
A. subprocess.run()
B. subprocess.popen()
C. subprocess.call()
D. subprocess.check_output()
Answer: A
Rationale: subprocess.run() was introduced in Python 3.5 as the primary way to run
commands. It returns a CompletedProcess instance, which makes it easier to check return
codes and capture output. It is generally preferred over older methods like subprocess.call
for modern automation tasks.
Automation | Actual Q&A with Rationale
(D522 Exam 1) | Western Governors
University
1. Which module in Python is most commonly used for interacting with the operating system
to perform tasks like listing directory contents?
A. sys
B. os
C. subprocess
D. shutil
Answer: B
Rationale: The os module provides a portable way of using operating system dependent
functionality. While subprocess is used for external commands, the os module is
specifically designed for file system navigation and environment interaction. This is a
fundamental concept in IT automation for managing local file structures.
2. When using the ‘csv’ module to read a file, which object type does ‘csv.DictReader’ return
for each row in the iteration?
A. Dictionary
B. Tuple
,C. List
D. String
Answer: A
Rationale: The csv.DictReader function maps the information in each row to an
OrderedDict, where keys are the column headers. This allows developers to access data by
column name rather than index, improving code readability. Using dictionaries for data
manipulation is a key skill in Python for IT Automation workflows.
3. What is the primary purpose of the ‘with’ statement when opening a file in Python?
A. To increase the speed of reading the file
B. To encrypt the file contents upon opening
C. To automatically handle file closing even if an exception occurs
D. To allow multiple users to write to the file simultaneously
Answer: C
Rationale: The ‘with’ statement simplifies exception handling by encapsulating common
preparation and cleanup tasks. It ensures that the file is properly closed after the block of
code is finished, preventing resource leaks. This practice is essential for robust automation
scripts that handle many files.
4. In a regular expression, which special character is used to match the start of a string?
A. $
, B. ^
C. .
D. []
Answer: B
Rationale: The caret character (^) anchors the search to the beginning of the string or line.
This is frequently used in log analysis to find lines starting with specific timestamps or
error levels. Mastering regex anchors is vital for precise text processing in IT environments.
5. Which method from the ‘subprocess’ module is recommended for executing a command
and waiting for it to complete in Python 3.5+?
A. subprocess.run()
B. subprocess.popen()
C. subprocess.call()
D. subprocess.check_output()
Answer: A
Rationale: subprocess.run() was introduced in Python 3.5 as the primary way to run
commands. It returns a CompletedProcess instance, which makes it easier to check return
codes and capture output. It is generally preferred over older methods like subprocess.call
for modern automation tasks.