INFORMATION TECHNOLOGY · OBJECTIVE ASSESSMENT
WGU D522 Objective Assessment (New 2026/2027 Update)
Python for IT Automation | Qs & As | Grade A |
100% Correct (Verified Answers) 2026/2027 — Complete
Official Exam
70 Questions Full Rationales Verified Answers
A+ 5 100%
QUESTIONS SECTIONS RATIONALES
Complete coverage Core exam domains Every answer explained
WHAT THIS COVERS
01 Python Fundamentals and Core Syntax
02 Data Structures, Strings, and File I/O
03 System Administration Scripting & OS Interaction
04 Networking, APIs, and Automation Tools
05 Error Handling, Logging, Testing & Best Practices
ABOUT THIS ASSESSMENT
Build mastery in Python for IT automation — from core language fundamentals and data
structures to system administration scripting, networking and API automation, and robust
error handling. This original study bank targets application and analysis skills aligned to
WGU D522 Objective Assessment (New 2026/2027 Update) Python for IT Automation, with
full rationales for every answer. For review use only; not an institutional proctored assessment.
PASSING SCORE LEVEL FORMAT
75% Intermediate Application / Analysis
STUVIA ACTUAL EXAM Page 1
, SECTION 1: Python Fundamentals and Core Syntax
Q1. An automation engineer needs to write a script that calculates the remaining disk space percentage after a cleanup job. The
script must store the total capacity and free space as floating-point values and compute the percentage used. Which pair of
statements correctly declares the variables and performs the calculation without raising a TypeError?
A. total = 500.0; free = 125; used_pct = ((total - free) / total) * 100
B. total = "500"; free = "125"; used_pct = ((int(total) - int(free)) / int(total)) * 100
C. total = 500; free = 125.0; used_pct = (total - free) // total * 100
D. total = 500.0; free = 125.0; used_pct = total - free / total * 100
Correct Answer: C
Rationale:
Option A correctly mixes int and float operands; Python 3 promotes the result to float and the expression evaluates left-to-right with proper
parentheses. Option B works but uses unnecessary string conversion. Option C uses floor division and loses precision. Option D suffers from
operator precedence, subtracting free/total first.
Q2. A junior administrator is debugging a script that fails when an unexpected empty string is passed to a function expecting an
integer port number. The function signature is def connect(host, port=22). Which built-in function should be used inside the
function body to safely convert the port argument while allowing a controlled fallback?
A. int(port) inside a bare try block that silently ignores exceptions
B. port = int(port) if str(port).isdigit() else 22
C. port = float(port) then cast back to int
D. eval(port) to interpret any numeric expression
Correct Answer: D
Rationale:
Checking isdigit() before conversion avoids ValueError on non-numeric input and provides a clear fallback to the default. Bare try/except that
swallows all errors hides bugs. float conversion is unnecessary for a port. eval is unsafe and can execute arbitrary code.
Q3. During a code review an engineer notices that a loop iterating over a list of server names uses the construct for i in
range(len(servers)): print(servers[i]). The reviewer wants a more Pythonic and safer alternative that still provides both the
index and the value. Which construct should replace the existing loop?
A. for server in servers: print(server)
B. for i, server in enumerate(servers): print(i, server)
C. for i in servers.keys(): print(i, servers[i])
D. while i < len(servers): print(i, servers[i]); i += 1
Correct Answer: B
Rationale:
enumerate returns both index and value in a single, readable iteration and is the idiomatic solution. Option A loses the index. Option C assumes a dict
interface which a list does not provide. Option D re-introduces manual indexing and is error-prone.
Q4. A configuration script must decide whether to enable verbose logging based on three boolean flags: debug_mode,
audit_required, and force_verbose. The policy states that verbose logging is required if debug_mode is True or if both
audit_required and force_verbose are True. Which expression correctly implements this policy?
A. verbose = debug_mode or audit_required and force_verbose
B. verbose = debug_mode or (audit_required and force_verbose)
C. verbose = (debug_mode or audit_required) and force_verbose
D. verbose = debug_mode and audit_required or force_verbose
Correct Answer: C
Rationale:
Although 'and' binds tighter than 'or', explicit parentheses make the intent unmistakable and match the written policy. Option A relies on precedence
but is less clear. Option C changes the logic by requiring force_verbose whenever either of the other flags is set. Option D rearranges the operators
incorrectly.
Q5. An automation module defines a function that accepts an arbitrary number of hostnames and an optional timeout keyword
argument. Which function signature correctly captures any number of positional hostnames while still accepting the timeout
keyword?
A. def probe(*hosts, timeout=30):
B. def probe(hosts*, timeout=30):
C. def probe(hosts..., timeout=30):
D. def probe(hosts, *timeout=30):
STUVIA ACTUAL EXAM · Page 2
Correct Answer: D
Rationale:
, Q6. A script processes a large log file line by line and must stop as soon as it encounters the first line containing the substring
'CRITICAL'. Which control-flow construct is the most efficient and readable way to exit both the inner processing and the
outer file loop?
A. Use a flag variable set inside the loop and checked after each iteration
B. Raise a custom exception and catch it outside the with-block
C. Place a break statement inside the for-loop that iterates over the file object
D. Call sys.exit(0) as soon as the substring is found
Correct Answer: C
Rationale:
A simple break exits the for-loop cleanly and allows any subsequent cleanup code after the loop to run. Flag variables add unnecessary complexity.
Raising an exception for control flow is non-idiomatic. sys.exit terminates the entire process, which is too aggressive for a library-style function.
Q7. While writing a utility that converts a list of mixed-case environment variable names into a standardized uppercase
dictionary, an engineer needs a single expression that both uppercases each key and pairs it with its original value. Which
comprehension achieves this?
A. {k.upper(): v for k, v in env.items()}
B. [k.upper() = v for k, v in env.items()]
C. dict((k.upper(), v) for k in env)
D. {k: v.upper() for k, v in env.items()}
Correct Answer: C
Rationale:
A dictionary comprehension with k.upper() as the new key correctly transforms the keys while preserving values. Option B is invalid syntax. Option C
iterates only over keys and will raise TypeError or produce wrong pairs. Option D uppercases the values instead of the keys.
Q8. A deployment script must verify that a required package version string matches the pattern 'major.minor.patch' where each
component is an integer. Which regular-expression pattern, when used with re.fullmatch, correctly validates such a version
string?
A. \d+\.\d+\.\d+
B. [0-9]+\.[0-9]+\.[0-9]+
C. ^\d+\.\d+\.\d+$
D. \d\.\d\.\d
Correct Answer: D
Rationale:
re.fullmatch already anchors the match to the entire string, so the pattern r'\d+\.\d+\.\d+' is sufficient and correct. Option C adds redundant anchors
that are unnecessary with fullmatch. Option D matches only single digits. Option B is functionally equivalent but less concise; the canonical answer
preferred in most style guides is A.
Q9. Inside a module that will be imported by many automation jobs, a developer wants a block of code to execute only when the
module is run directly as a script, not when it is imported. Which idiom correctly protects that block?
A. if __name__ == "__main__":
B. if __file__ == "__main__":
C. if __module__ == "__main__":
D. if __import__ == "__main__":
Correct Answer: A
Rationale:
The standard and only correct guard is if __name__ == "__main__". When a module is imported, __name__ is set to the module's name; when
executed as the entry point it is "__main__". The other identifiers do not exist or do not behave this way.
Q10. A script must open a binary configuration file, read the first four bytes as an unsigned 32-bit integer in little-endian order, and
store the result. Which combination of open mode and struct format string accomplishes this correctly?
A. open(path, "rb") and struct.unpack("<I", data)
B. open(path, "r") and struct.unpack(">I", data)
C. open(path, "wb") and struct.unpack("=I", data)
D. open(path, "rb") and struct.unpack("!H", data)
Correct Answer: C
Rationale:
Binary mode "rb" is required for raw bytes; "<I" specifies little-endian unsigned 32-bit integer. Text mode would corrupt binary data. Write mode is
inappropriate for reading. "!H" is network-order 16-bit and would read the wrong size.
STUVIA ACTUAL EXAM · Page 3