Docsity

WGU D522 – PYTHON FOR IT AUTOMATION, PYTHON, APIS, DEBUGGING AND SCRIPTING EXAM |, Exams of Nursing

Toggle sidebar
bg1
Page 1 of 91
WGU D522 – PYTHON FOR IT AUTOMATION,
PYTHON, APIS, DEBUGGING AND SCRIPTING
EXAM | 2026/2027 EDITION | VERIFIED
QUESTIONS WITH DETAILED ANSWERS AND
RATIONALES | BRAND NEW!
1. A network administrator needs to store a collection of device
hostnames that must remain immutable throughout the execution of a
configuration audit script. Which Python data structure is most
appropriate?
A. List
B. Dictionary
C. Tuple
D. Set
C. Tuple
A tuple is an ordered, immutable sequence. Since the administrator
requires the hostname collection to remain unchanged during script
execution, a tuple enforces this constraint at the data structure level.
Lists are mutable, dictionaries store key-value pairs (and are mutable),
and sets are unordered collections of unique elements.
2. A developer writes a Python script that prompts the user for a port
number and must ensure the value is stored as an integer for later
socket operations. Which code snippet correctly accomplishes this
without raising a TypeError?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b

Partial preview of the text

Download WGU D522 – PYTHON FOR IT AUTOMATION, PYTHON, APIS, DEBUGGING AND SCRIPTING EXAM | and more Exams Nursing in PDF only on Docsity!

WGU D522 – PYTHON FOR IT AUTOMATION,

PYTHON, APIS, DEBUGGING AND SCRIPTING

EXAM | 2026/2027 EDITION | VERIFIED

QUESTIONS WITH DETAILED ANSWERS AND

RATIONALES | BRAND NEW!

1. A network administrator needs to store a collection of device hostnames that must remain immutable throughout the execution of a configuration audit script. Which Python data structure is most appropriate? A. List B. Dictionary C. Tuple D. Set C. Tuple A tuple is an ordered, immutable sequence. Since the administrator requires the hostname collection to remain unchanged during script execution, a tuple enforces this constraint at the data structure level. Lists are mutable, dictionaries store key-value pairs (and are mutable), and sets are unordered collections of unique elements. 2. A developer writes a Python script that prompts the user for a port number and must ensure the value is stored as an integer for later socket operations. Which code snippet correctly accomplishes this without raising a TypeError?

A. port = input("Enter port: ") B. port = float(input("Enter port: ")) C. port = int(input("Enter port: ")) D. port = str(input("Enter port: ")) C. port = int(input("Enter port: ")) The input() function in Python 3 always returns a string. To store the value as an integer, the developer must wrap it with int(). Option A stores a string, Option B returns a float, and Option D wraps a string unnecessarily.

3. A script contains the following code: result = 17 // 5. What value is stored in result? A. 3. B. 3 C. 4 D. 2 B. 3 The // operator performs floor division, which divides the operands and returns the largest integer less than or equal to the quotient. 17 divided by 5 is 3.4, and floor division yields 3. 4. An IT automation engineer needs to extract the value associated with the key "gateway" from a dictionary named config without raising a KeyError if the key is missing. Which approach is safest?

6. A developer creates a variable named 2nd_server in a Python script and receives a SyntaxError. What is the root cause? A. The variable name is too short. B. Python identifiers cannot begin with a digit. C. The variable name contains a reserved keyword. D. Python identifiers must be lowercase. B. Python identifiers cannot begin with a digit. Python identifiers must begin with a letter (a-z, A-Z) or an underscore. Starting with a digit causes a SyntaxError. Underscores and digits are allowed after the first character. 7. A script assigns a variable inside a function but the outer scope reports it as undefined. The function does not use any global declaration. Which Python scope rule explains this? A. Block-level scoping B. Function hoisting C. LEGB rule (Local, Enclosing, Global, Built-in) D. Dynamic scoping C. LEGB rule (Local, Enclosing, Global, Built-in) Python resolves variable names using the LEGB rule. A variable assigned inside a function is local to that function unless explicitly declared global. Python does not use block-level scoping or hoisting.

8. An automation engineer needs to read a CSV file containing device inventory data with columns for hostname, IP, and model. Which Python module is specifically designed for parsing CSV files? A. json B. csv C. os D. pickle B. csv The csv module provides reader and writer objects for parsing and generating CSV data. The json module handles JSON, os handles filesystem operations, and pickle serializes Python objects. 9. A developer wants to concatenate a string message with an integer error code: "Error: " + error_code. This raises a TypeError. Which is the correct fix? A. "Error: " + int(error_code) B. "Error: " + str(error_code) C. "Error: " + error_code.decode() D. "Error: " + repr(error_code) B. "Error: " + str(error_code) Python does not allow implicit concatenation between str and int. The str() function converts the integer to its string representation, allowing the + operator to concatenate two strings.

12. An automation script must execute an operating system command and capture its output. Which Python module is designed for spawning processes and interacting with system commands? A. os B. sys C. subprocess D. shutil C. subprocess The subprocess module allows scripts to spawn new processes, connect to their input/output/error pipes, and obtain return codes. It is the standard way to run external commands from Python. 13. A developer is writing a script that connects to network devices via SSH to execute commands. Which Python library is most commonly used for this purpose in D522? A. requests B. netmiko C. csv D. socket B. netmiko Netmiko is a multi-vendor library that simplifies SSH connections to network devices, making it easier to send commands and retrieve output. Requests handles HTTP, csv handles CSV files, and socket provides low-level networking.

14. A script needs to perform a DNS zone transfer to enumerate all records in a domain. Which dnspython functions are used? A. dns.query.xfr() and dns.zone.from_xfr() B. dns.resolver.resolve() and dns.zone.load() C. dns.transfer.zone() and dns.query.parse() D. socket.gethostbyname() and dns.zone.dump() A. dns.query.xfr() and dns.zone.from_xfr() The correct approach is to use dns.query.xfr() to initiate the transfer and dns.zone.from_xfr() to parse the results. This technique should only be used with explicit authorization. 15. An IT analyst needs to parse a JSON response from a REST API. Which Python module provides the functions json.loads() and json.dumps()? A. csv B. json C. xml D. requests B. json The json module handles JSON serialization and deserialization. json.loads() parses a JSON string into a Python object, and json.dumps() converts a Python object to a JSON string. 16. A developer needs to add a single item to the end of a list. Which method is correct?

C. Dictionary D. Set C. Dictionary Dictionaries store data as key-value pairs, allowing meaningful labels (like device names) to map to values (like IP addresses). This is ideal for inventory and configuration data.

19. A script contains the following code: 7 % 3. What is the result? A. 2 B. 1 C. 2. D. 0 B. 1 The modulo operator (%) returns the remainder of division. 7 divided by 3 equals 2 with a remainder of 1, so 7 % 3 evaluates to 1. 20. A developer is building an automation script that must handle transient network failures by retrying connections. What is the purpose of a RETRIES configuration parameter? A. To define how many devices to audit B. To control how many times the script attempts to connect to a device C. To increase the number of audit commands sent to a device D. To set the number of ports to scan

B. To control how many times the script attempts to connect to a device The RETRIES parameter provides tolerance for transient failures by limiting connection attempts before giving up. This is essential for network automation in environments with occasional connectivity issues.

21. A developer needs to read a file line by line in a memory-efficient manner. Which approach is best? A. file.read().splitlines() B. Iterating over the file object directly: for line in file: C. file.readlines() D. file.read(1000) B. Iterating over the file object directly: for line in file: Iterating over a file object yields lines one at a time without loading the entire file into memory. readlines() loads all lines into a list, and read() loads the entire file. 22. An automation engineer needs to convert a string "123" to an integer. Which function is used? A. str(123) B. int("123") C. float("123") D. bool("123") B. int("123")

25. A developer wants to check the type of an object in Python. Which built-in function is used? A. typeof() B. type() C. class() D. instance() B. type() The type() function returns the type of an object. For example, type(42) returns . This is useful for debugging and input validation. 26. A script needs to handle exceptions that might occur during a network connection attempt. Which Python construct is used to catch and handle exceptions? A. if/else B. try/except C. for/while D. with/as B. try/except The try/except block is Python's primary exception handling mechanism. Code that might raise an exception goes in the try block, and handling logic goes in the except block.

27. A developer wants to ensure a block of code executes regardless of whether an exception occurs. Which clause is used with try/except? A. else B. finally C. catch D. ensure B. finally The finally block is guaranteed to execute whether or not an exception occurs. It is commonly used for cleanup operations like closing files or network connections. 28. An automation script needs to read a CSV file and validate that required fields are present. Which approach best follows professional coding practices? A. Assume all fields are present and parse directly B. Validate field presence before processing and log missing fields C. Ignore missing fields and continue D. Raise an unhandled exception B. Validate field presence before processing and log missing fields Professional automation scripts validate input and handle malformed or missing fields gracefully. Logging missing fields provides visibility while allowing the script to continue processing valid records. 29. A developer needs to merge two sets of IP addresses while ensuring uniqueness. Which set method is used?

B. strip() The strip() method removes leading and trailing whitespace from a string. It does not remove whitespace within the string. lstrip() and rstrip() remove from one side only.

32. An automation script connects to a REST API. What HTTP status code indicates a successful request? A. 200 B. 404 C. 500 D. 301 A. 200 HTTP 200 OK indicates a successful request. 404 means not found, 500 means internal server error, and 301 is a permanent redirect. 33. A developer needs to add multiple items to a set from another iterable. Which method is used? A. add() B. update() C. append() D. extend() B. update() The update() method adds multiple elements from an iterable to a set. add() adds a single element. append() and extend() are list methods.

34. A script uses subprocess.run(["ping", "host"]). What does this function return? A. A string containing the command output B. A CompletedProcess object with return code and output C. A boolean indicating success D. The exit code as an integer B. A CompletedProcess object with return code and output subprocess.run() returns a CompletedProcess object containing the return code, stdout, and stderr. The return code is accessed via .returncode, and output via .stdout. 35. A developer wants to check if a number is even using the modulo operator. Which expression correctly tests this? A. num % 2 = 0 B. num % 2 == 0 C. num / 2 == 0 D. num // 2 == 0 B. num % 2 == 0 The modulo operator returns the remainder. If a number is even, dividing by 2 yields a remainder of 0. The comparison operator == is used (not =, which is assignment).

A. int() B. str() C. float() D. repr() B. str() The str() function converts a value to its string representation. For a float like 3.14, str(3.14) returns "3.14". repr() provides a more detailed representation but is not typically used for logging.

39. An automation script must create a backup of a file before modifying it. Which Python module provides high-level file operations like copy and move? A. os B. sys C. shutil D. pathlib C. shutil The shutil module (shell utilities) provides high-level operations including shutil.copy(), shutil.move(), and shutil.rmtree(). The os module provides lower-level operations. 40. A developer wants to ensure a script only runs when executed directly, not when imported as a module. Which construct is used? A. if name == "main": B. if main == "name":

C. def main(): D. if imported == False: A. if name == "main": This guard checks whether the script is being run as the main program ( name equals " main ") or imported as a module ( name equals the module name). It prevents code from executing on import.

41. A developer needs to perform a DNS lookup using the socket module. Which function provides a basic A-record lookup? A. socket.resolve() B. socket.gethostbyname() C. socket.dns_lookup() D. socket.getaddrinfo() B. socket.gethostbyname() socket.gethostbyname() performs a basic A-record lookup, returning the IPv4 address for a hostname. While less flexible than dnspython, it is built into the standard library. 42. A script contains my_list = [1, 2, 3]. What does my_list[1] return? A. 1 B. 2 C. 3 D. IndexError B. 2