D 522 Exam 1 V2 | D 522 Python for IT Automation
| Actual Q&A with Rationale (D522 Exam 1) |
Western Governors University
1. A system administrator needs to check if a specific configuration file exists before
attempting to read it. Which function from the ‘os.path’ module is most appropriate for this
task?
A. os.path.exists()
B. os.path.isdir()
C. os.path.isfile()
D. os.path.join()
Correct Answer: A
Explanation: The os.path.exists() function is specifically designed to return True if a file or
directory at the specified path exists. This check is a fundamental step in defensive
programming to prevent FileNotFoundError exceptions during script execution. By
verifying the path before operations, the script ensures stability and allows for graceful
error handling.
2. When using the ‘subprocess’ module to run a bash command and capture its output, which
argument must be set to ‘True’ in the ‘subprocess.run()’ function to return a string instead of
bytes?
A. shell
,B. capture_output
C. check
D. text
Correct Answer: D
Explanation: The ‘text’ argument (or ‘universal_newlines’ in older versions) tells the
subprocess to handle the standard input, output, and error as strings. If this is not set, the
output is returned in byte format, which usually requires manual decoding using UTF-8.
Setting text=True simplifies data processing within the Python script significantly.
3. In Python, which built-in data type is immutable and stores an ordered sequence of
elements?
A. List
B. Dictionary
C. Tuple
D. Set
Correct Answer: C
Explanation: A tuple is defined as an immutable sequence, meaning its contents cannot be
altered once it is created. This makes tuples faster than lists and suitable for protecting
data that should not change throughout a script. They use parentheses for definition,
distinguishing them from square-bracketed lists.
, 4. A developer wants to extract all email addresses from a large log file. Which ‘re’ module
function is best suited to find all non-overlapping matches of a pattern?
A. re.search()
B. re.match()
C. re.split()
D. re.findall()
Correct Answer: D
Explanation: The re.findall() function scans the entire input string and returns a list
containing all matches of the provided regex pattern. Unlike re.search(), which only returns
the first occurrence, findall() is ideal for data scraping and log analysis. It ensures that
every instance of the pattern is captured for further processing.
5. What will be the output of the following code snippet? ‘print(“Automation”.strip(“n”))’
A. Automa
B. Automation
C. Automatio
D. tomatio
Correct Answer: C
| Actual Q&A with Rationale (D522 Exam 1) |
Western Governors University
1. A system administrator needs to check if a specific configuration file exists before
attempting to read it. Which function from the ‘os.path’ module is most appropriate for this
task?
A. os.path.exists()
B. os.path.isdir()
C. os.path.isfile()
D. os.path.join()
Correct Answer: A
Explanation: The os.path.exists() function is specifically designed to return True if a file or
directory at the specified path exists. This check is a fundamental step in defensive
programming to prevent FileNotFoundError exceptions during script execution. By
verifying the path before operations, the script ensures stability and allows for graceful
error handling.
2. When using the ‘subprocess’ module to run a bash command and capture its output, which
argument must be set to ‘True’ in the ‘subprocess.run()’ function to return a string instead of
bytes?
A. shell
,B. capture_output
C. check
D. text
Correct Answer: D
Explanation: The ‘text’ argument (or ‘universal_newlines’ in older versions) tells the
subprocess to handle the standard input, output, and error as strings. If this is not set, the
output is returned in byte format, which usually requires manual decoding using UTF-8.
Setting text=True simplifies data processing within the Python script significantly.
3. In Python, which built-in data type is immutable and stores an ordered sequence of
elements?
A. List
B. Dictionary
C. Tuple
D. Set
Correct Answer: C
Explanation: A tuple is defined as an immutable sequence, meaning its contents cannot be
altered once it is created. This makes tuples faster than lists and suitable for protecting
data that should not change throughout a script. They use parentheses for definition,
distinguishing them from square-bracketed lists.
, 4. A developer wants to extract all email addresses from a large log file. Which ‘re’ module
function is best suited to find all non-overlapping matches of a pattern?
A. re.search()
B. re.match()
C. re.split()
D. re.findall()
Correct Answer: D
Explanation: The re.findall() function scans the entire input string and returns a list
containing all matches of the provided regex pattern. Unlike re.search(), which only returns
the first occurrence, findall() is ideal for data scraping and log analysis. It ensures that
every instance of the pattern is captured for further processing.
5. What will be the output of the following code snippet? ‘print(“Automation”.strip(“n”))’
A. Automa
B. Automation
C. Automatio
D. tomatio
Correct Answer: C