D 522 Exam 1 V2 | D 522 Python for IT
Automation | Actual Q&A with Rationale
(D522 Exam 1) | Western Governors
University
1. When executing a Python script in a Linux environment, which shebang line ensures the
script uses the version of Python 3 installed in the user’s current environment path?
A. #!/bin/python
B. #!/usr/bin/python
C. #!/usr/bin/env python3
D. #/usr/local/python3
Answer: C
Rationale: The shebang line #!/usr/bin/env python3 is the most portable way to locate
the Python interpreter. It searches the user’s PATH environment variable to find the
executable rather than relying on a hardcoded path. This practice is essential for
automation scripts that may run across different Linux distributions or virtual
environments.
2. Which method should be used within the ‘os’ module to check if a specific directory exists
before attempting to create a new folder within it?
A. os.path.isdir()
,B. os.path.exists()
C. os.mkdir()
D. os.path.isfile()
Answer: B
Rationale: The os.path.exists() function returns True if the path exists, regardless of
whether it is a file or a directory. Using this check prevents the script from raising an
OSError when attempting to manipulate a non-existent path. In IT automation, verifying
resource existence is a critical step in error handling and script stability.
3. An IT professional needs to extract all email addresses from a large log file. Which ‘re’
module function is best suited to return all matches as a list of strings?
A. re.search()
B. re.match()
C. re.split()
D. re.findall()
Answer: D
Rationale: The re.findall() function iterates through the entire input string and returns all
non-overlapping matches of a pattern. This is particularly useful for parsing logs where
multiple instances of data, such as emails or IP addresses, need to be captured
, simultaneously. Other functions like re.search() only return the first occurrence
encountered in the text.
4. What is the primary advantage of using the ‘with’ statement when opening a file in Python
for reading or writing?
A. It ensures the file is properly closed even if an exception occurs.
B. It encrypts the file data during the I/O process.
C. It automatically converts the file content into a list.
D. It increases the execution speed of the script by bypassing the OS buffer.
Answer: A
Rationale: The ‘with’ statement acts as a context manager that handles the opening and
closing of file descriptors. This prevents resource leaks which can occur if a script crashes
before reaching an explicit .close() call. In automation, managing file handles correctly is
vital for scripts that process thousands of files.
5. Which command-line argument property in the ‘sys’ module contains the name of the
script being executed?
A. sys.path[0]
B. sys.argv[1]
C. sys.argv[0]
D. sys.cmd
Automation | Actual Q&A with Rationale
(D522 Exam 1) | Western Governors
University
1. When executing a Python script in a Linux environment, which shebang line ensures the
script uses the version of Python 3 installed in the user’s current environment path?
A. #!/bin/python
B. #!/usr/bin/python
C. #!/usr/bin/env python3
D. #/usr/local/python3
Answer: C
Rationale: The shebang line #!/usr/bin/env python3 is the most portable way to locate
the Python interpreter. It searches the user’s PATH environment variable to find the
executable rather than relying on a hardcoded path. This practice is essential for
automation scripts that may run across different Linux distributions or virtual
environments.
2. Which method should be used within the ‘os’ module to check if a specific directory exists
before attempting to create a new folder within it?
A. os.path.isdir()
,B. os.path.exists()
C. os.mkdir()
D. os.path.isfile()
Answer: B
Rationale: The os.path.exists() function returns True if the path exists, regardless of
whether it is a file or a directory. Using this check prevents the script from raising an
OSError when attempting to manipulate a non-existent path. In IT automation, verifying
resource existence is a critical step in error handling and script stability.
3. An IT professional needs to extract all email addresses from a large log file. Which ‘re’
module function is best suited to return all matches as a list of strings?
A. re.search()
B. re.match()
C. re.split()
D. re.findall()
Answer: D
Rationale: The re.findall() function iterates through the entire input string and returns all
non-overlapping matches of a pattern. This is particularly useful for parsing logs where
multiple instances of data, such as emails or IP addresses, need to be captured
, simultaneously. Other functions like re.search() only return the first occurrence
encountered in the text.
4. What is the primary advantage of using the ‘with’ statement when opening a file in Python
for reading or writing?
A. It ensures the file is properly closed even if an exception occurs.
B. It encrypts the file data during the I/O process.
C. It automatically converts the file content into a list.
D. It increases the execution speed of the script by bypassing the OS buffer.
Answer: A
Rationale: The ‘with’ statement acts as a context manager that handles the opening and
closing of file descriptors. This prevents resource leaks which can occur if a script crashes
before reaching an explicit .close() call. In automation, managing file handles correctly is
vital for scripts that process thousands of files.
5. Which command-line argument property in the ‘sys’ module contains the name of the
script being executed?
A. sys.path[0]
B. sys.argv[1]
C. sys.argv[0]
D. sys.cmd