Docsity

WGU D522 V2 Python for IT Automation Master Study Guide | Complete Course Notes, Network A, Exams of Nursing

Partial preview of the text

Download WGU D522 V2 Python for IT Automation Master Study Guide | Complete Course Notes, Network A and more Exams Nursing in PDF only on Docsity!

WGU D522 V2 Python for IT Automation

Master Study Guide | Complete Course

Notes, Network Automation, DNS, APIs,

Troubleshooting & PA Prep | 2026 Updated

Q3: Which data structure stores unique, unordered items and automatically

eliminates duplicates?

A) List B) Tuple C) Set D) Dictionary

Answer: C) Set

Rationale: Sets do not preserve order and automatically eliminate duplicates. They are excellent for membership testing and filtering unique values, such as extracting unique IP addresses from log files. Sets use hash-based lookup for O(1) membership testing.

Q4: Which data structure stores data as key-value pairs, allowing fast lookup

by a descriptive key?

A) List B) Tuple C) Set D) Dictionary

Answer: D) Dictionary

Rationale: Dictionaries store data as key-value pairs, allowing data to be accessed using meaningful labels rather than numeric indexes. They are ideal for representing relationships such as device names and IP addresses. Dictionaries provide O(1) average-time complexity for lookups.

Q5: Which of the following is the correct way to create a single-line comment

in Python?

A) // This is a comment B) /* This is a comment */ C) # This is a comment D) ``

Answer: C) # This is a comment

Rationale: In Python, single-line comments are created using the hash symbol (#). Option A is used in C/C++/Java, Option B is a multi-line comment in C/C++/Java, and Option D is an HTML comment syntax. Python does not use these other comment styles.

Q6: What is the output of print(5 // 2)?

A) 2. B) 2 C) 3 D) 2.

Answer: B) 2

Rationale: The floor division operator (//) returns the largest integer less than or equal to the division result. 5 // 2 equals 2 (not 2.5 or 2.0), as floor division discards the remainder and returns an integer in Python 3.

Q7: Which data type would be most appropriate for storing a collection of

unique IP addresses in Python?

A) List B) Tuple C) Set D) Dictionary

Rationale: The len() function is the built-in Python function used to return the number of items in a sequence or collection. size() is not a built-in Python function (it's used in other languages like Java), length() is not a Python built-in, and count() returns the number of occurrences of a specific element.

Q10: What will be the output of the following code?

a = [1, 2, 3] b = a b.append(4) print(a)

A) [1, 2, 3] B) [1, 2, 3, 4] C) [4] D) Error

Answer: B) [1, 2, 3, 4]

Rationale: In Python, when you assign a list to a new variable (b = a), both variables reference the same list object in memory. Modifying the list through b also affects a because they point to the same object. This is a common pitfall — to create a copy, use b = a.copy() or b = a[:].

Q11: Which method adds an item to the end of a list?

A) insert() B) extend() C) append() D) add()

Answer: C) append()

Rationale: append() adds an item to the end of a list, modifying the list in place. For adding multiple elements, use extend(). For adding at a specific position, use insert().

Q12: How do you add multiple elements from another list to the end of a list?

A) append() B) extend() C) insert() D) merge()

Answer: B) extend()

Rationale: extend() adds multiple elements from an iterable (like another list) to the end of a list. Unlike append(), which adds the entire iterable as a single element, extend() adds each element individually.

Q13: Which method returns the index of the first occurrence of a value in a

list?

A) find() B) index() C) locate() D) search()

Answer: B) index()

Rationale: The index() method searches a list and returns the first occurrence's index position. If not found, it raises a ValueError. Essential for locating elements when their position is unknown.

B) They allow values to be associated with descriptive keys C) They only store numeric data D) They consume less memory than lists

Answer: B) They allow values to be associated with descriptive keys

Rationale: Dictionaries store data as key-value pairs, allowing information to be labeled meaningfully. Values are accessed using descriptive keys rather than numeric indexes — ideal for representing device names and IP addresses.

Q17: What is the output of type(42) in Python?

A) **B)** C) **D)**

Answer: A) ``

Rationale: The type() function is used to check an object's type. type(42) returns `` because 42 is an integer.

Q18: Which function converts a string to an integer?

A) str() B) int() C) float() D) bool()

Answer: B) int()

Rationale: Use Python's built-in conversion functions like int(), float(), str(), and bool(). int('123') converts the string '123' to the integer 123.

Q19: What is a common pitfall when using assignment in Python conditions?

A) Using == instead of = B) Using = instead of == in a condition C) Forgetting to indent D) Using != instead of <>

Answer: B) Using = instead of == in a condition

Rationale: A common pitfall is using = (assignment) instead of == (equality comparison) in a condition, which causes a syntax error. In Python, = is for assignment, while == is for comparison.

Q20: What is Python's approach to variable typing?

A) Statically typed — types must be declared B) Dynamically typed — types are inferred at runtime C) Strongly typed — types cannot change D) Weakly typed — types are ignored

Answer: B) Dynamically typed — types are inferred at runtime

Rationale: Python is dynamically typed, meaning you don't declare types explicitly. A variable can refer to data of any type and can be reassigned to a different type later.

SECTION 2: CONTROL FLOW & FUNCTIONS

Q21: What is the correct syntax for a function definition in Python?

Rationale: enumerate() is a function that generates pairs of (index, value) for each element in a list. Using enumerate allows you to iterate over a list with both the index and value simultaneously.

Q24: What will the following code print?

fruits = ["apple", "banana", "cherry"] for i, fruit in enumerate(fruits): print(i, fruit)

A) 0 apple 1 banana 2 cherry B) apple banana cherry C) (0, 'apple') (1, 'banana') (2, 'cherry') D) Error

Answer: A) 0 apple 1 banana 2 cherry

Rationale: For the list fruits = ["apple", "banana", "cherry"], using enumerate gives: 0 : apple, 1 : banana, 2 : cherry. You can give enumerate a start value to begin counting from a number other than 0, e.g., enumerate(fruits, start=1) gives indices 1, 2, 3.

Q25: What is the purpose of the continue statement in a loop?

A) Exit the loop entirely B) Skip the current iteration and move to the next C) Restart the loop from the beginning D) Pause the loop execution

Answer: B) Skip the current iteration and move to the next

Rationale: Use continue to skip certain items and move on to the next iteration. For example, in a loop, if num == 3: continue will skip printing 3.

Q26: What causes an infinite loop in Python?

A) Using a for loop with a list B) Forgetting to update the loop condition variable C) Using break in a loop D) Using continue in a loop

Answer: B) Forgetting to update the loop condition variable

Rationale: A logic error causing an infinite loop typically involves forgetting to update the loop condition variable. Always ensure your loop condition will eventually become False to avoid infinite loops.

Q27: How can you interrupt a running Python script?

A) Press Ctrl+C B) Press Ctrl+Z C) Press Ctrl+D D) Press Esc

Answer: A) Press Ctrl+C

Rationale: You can interrupt a Python script using Ctrl+C in most environments.

Q28: What does the range() function return in Python 3?

C) !=

D) <>

Answer: C) !=

Rationale: Comparison operators in Python include == (equal to), != (not equal), <, <=, >, and >=.

## SECTION 3: FILE I/O, ERROR HANDLING & MODULES

Q31: Which Python module is primarily used for interacting with the

operating system (e.g., creating directories, checking paths)?

A) sys B) os C) re D) json

Answer: B) os

Rationale: The os module is primarily used for interacting with the operating system, including creating directories, checking paths, and other file system operations.

Q32: Which Python module provides access to the BSD socket interface for

creating network connections?

A) os B) sys C) socket D) requests

Answer: C) socket

Rationale: The socket module provides access to the BSD socket interface for creating network connections (both TCP and UDP). You can create a socket using import socket; s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) for TCP connections.

Q33: What is the correct way to open a file for reading in Python?

A) open('file.txt', 'r') B) open('file.txt', 'w') C) open('file.txt', 'a') D) open('file.txt', 'x')

Answer: A) open('file.txt', 'r')

Rationale: The open() function with mode 'r' (read) opens a file for reading. Other modes include 'w' (write), 'a' (append), and 'x' (exclusive creation). Using a with statement is recommended for proper resource management.

Q34: How do you access Python's built-in documentation?

A) Using the help() function B) Using the doc() function C) Using the info() function D) Using the manual() function

Answer: A) Using the help() function

Rationale: Python's built-in documentation can be accessed using the help() function.