Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 3 out of 29 pages
Exam (elaborations)

D 522 Exam 1 V2 | D 522 Python for IT Automation | Actual Q&A with Rationale (D522 Exam 1) | Western Governors University

Document preview thumbnail
Preview 3 out of 29 pages

D 522 Exam 1 V2 | D 522 Python for IT Automation | Actual Q&A with Rationale (D522 Exam 1) | Western Governors University

Content preview

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

Document information

Uploaded on
July 16, 2026
Number of pages
29
Written in
2025/2026
Type
Exam (elaborations)
Contains
Questions & answers
$16.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
ScholarsAscend
3.7
(77)
Sold
453
Followers
39
Items
29361
Last sold
11 hours ago




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions