Docsity

WGU D522, Python for IT Automation, Python scripting, automation, file handling, regular e, Exams of Information and Communications Technology (ICT)

Toggle sidebar
bg1
WGU D522, Python for IT Automation, Python
scripting, automation, file handling, regular
expressions, API, data structures, WGU exam prep.
TITLE: WGU D522 – Python for IT Automation | Latest Exam 2026/2027 | 150 Q&A
Institution: Western Governors University (WGU)
Course: D522 – Python for IT Automation
Professor: WGU IT Faculty
Subject/Topic: Python Scripting, Automation, File I/O, Regex, APIs, Data Structures
TABLE OF CONTENTS
--
1. Python Fundamentals & Data Types (Questions 1-20)
2. Control Flow & Functions (Questions 21-35)
3. File I/O & Exception Handling (Questions 36-50)
4. Data Structures (Lists, Dictionaries, Sets, Tuples) (Questions 51-70)
5. Regular Expressions (Regex) (Questions 71-85)
6. Working with APIs & JSON (Questions 86-95)
7. Automation Scripts & Real‑World IT Tasks (Questions 96-110)
8. Modules, Libraries & Virtual Environments (Questions 111-125)
9. Debugging, Testing & Best Practices (Questions 126-135)
10. Final Review & Comprehensive Scenarios (Questions 136-150)
SECTION 1 – PYTHON FUNDAMENTALS & DATA TYPES (Questions 1-20)
Q1. Which of the following is NOT a valid Python data type?
A) int
B) float
C) rational
D) str
Correct Answer: C
Rationale: Python‘s built‑in numeric types include int, float, and complex. “rational” is not a
primitive type in Python; fractions.Fraction can be used but is not a core type.
Q2. What is the output of: print(type(42)) ?
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

Partial preview of the text

Download WGU D522, Python for IT Automation, Python scripting, automation, file handling, regular e and more Exams Information and Communications Technology (ICT) in PDF only on Docsity!

WGU D522, Python for IT Automation, Python

scripting, automation, file handling, regular

expressions, API, data structures, WGU exam prep.

TITLE: WGU D522 – Python for IT Automation | Latest Exam 2026/2027 | 150 Q&A Institution: Western Governors University (WGU) Course: D522 – Python for IT Automation Professor: WGU IT Faculty Subject/Topic: Python Scripting, Automation, File I/O, Regex, APIs, Data Structures

TABLE OF CONTENTS

  1. Python Fundamentals & Data Types (Questions 1-20)
  2. Control Flow & Functions (Questions 21-35)
  3. File I/O & Exception Handling (Questions 36-50)
  4. Data Structures (Lists, Dictionaries, Sets, Tuples) (Questions 51-70)
  5. Regular Expressions (Regex) (Questions 71-85)
  6. Working with APIs & JSON (Questions 86-95)
  7. Automation Scripts & Real‑World IT Tasks (Questions 96-110)
  8. Modules, Libraries & Virtual Environments (Questions 111-125)
  9. Debugging, Testing & Best Practices (Questions 126-135)
  10. Final Review & Comprehensive Scenarios (Questions 136-150)

SECTION 1 – PYTHON FUNDAMENTALS & DATA TYPES (Questions 1-20)

Q1. Which of the following is NOT a valid Python data type? A) int B) float C) rational D) str

Correct Answer: C

Rationale: Python‘s built‑in numeric types include int, float, and complex. “rational” is not a primitive type in Python; fractions.Fraction can be used but is not a core type.

Q2. What is the output of: print(type(42))?

A) B) C) D)

Correct Answer: A

Rationale: In Python, 42 is an integer literal, so its type is int. The type() function returns the class of the object.

Q3. Which of the following correctly converts the string “123” to an integer? A) int(“123”) B) str(“123”) C) float(“123”) D) chr(“123”)

Correct Answer: A

Rationale: The int() constructor converts a numeric string to an integer. str() returns a string, float() gives a float, and chr() is used for Unicode code points.

Q4. What is the result of 17 // 4? A) 4. B) 4 C) 5 D) 4.

Correct Answer: B

Rationale: The // operator performs floor division (integer division), discarding the remainder. 17 // 4 = 4.

Q5. Which statement checks if a variable age is between 18 and 65 inclusive? A) 18 <= age <= 65 B) age >= 18 and age =< 65 C) age in range(18, 65) D) 18 <= age < 65

Q9. What does the bool() function return for an empty string? A) True B) False C) None D) Error

Correct Answer: B

Rationale: In Python, empty sequences (including strings) are considered False in a boolean context. Non‑empty strings are True.

Q10. Which operator is used for exponentiation? A) ^ B) ** C) // D) *

Correct Answer: B

Rationale: ** is the exponentiation operator (e.g., 2**3 = 8). ^ is bitwise XOR.

Q11. What is the output of print(3 * “Hi”)? A) HiHiHi B) 3Hi C) Hi D) TypeError

Correct Answer: A

Rationale: The * operator repeats a string when used with an integer. “Hi” * 3 = “HiHiHi”.

Q12. Which function returns the length of a list? A) length() B) count() C) len() D) size()

Correct Answer: C

Rationale: len() is the built‑in function to return the number of items in a container (list, tuple, string, dict, etc.).

Q13. What data type is produced by input(“Enter age: “)? A) int B) float C) str D) bool

Correct Answer: C

Rationale: The input() function always returns a string, regardless of what the user types. Use int() or float() to convert.

Q14. Which of the following statements will cause a NameError? A) print(x) where x = 5 defined earlier B) print(y) where y has not been defined C) print(“hello”) D) print(5)

Correct Answer: B

Rationale: A NameError occurs when a variable name is used but has not been defined. The others are valid.

Q15. Which escape sequence represents a tab? A) \t B) \n C) \r D) \b

Correct Answer: A

Rationale: \t is the escape sequence for a tab character. \n is newline, \r is carriage return, \b is backspace.

D) list = set()

Correct Answer: B

Rationale: [] creates an empty list. () creates a tuple, {} creates a dictionary, set() creates an empty set.

Q20. What is the type of None? A) NoneType B) NullType C) None D) undefined

Correct Answer: A

Rationale: None is a singleton object of type NoneType, used to represent absence of a value.

SECTION 2 – CONTROL FLOW & FUNCTIONS (Questions 21-35)

Q21. What is the output of the following code? x = 5 if x > 10: print(“A”) elif x > 3: print(“B”) else: print(“C”) A) A B) B C) C D) Nothing

Correct Answer: B

Rationale: x = 5, so the condition x > 3 is True, and the elif block executes, printing “B”. The first condition x > 10 is False.

Q22. Which loop is guaranteed to execute at least once? A) for loop

B) while loop C) do‑while loop D) Python does not have a built‑in do‑while loop

Correct Answer: D

Rationale: Python does not have a native do‑while loop. A while loop can execute zero times if the condition is initially False.

Q23. What is the result of for i in range(3): print(i, end=“ ”)? A) 1 2 3 B) 0 1 2 C) 0 1 2 3 D) 1 2

Correct Answer: B

Rationale: range(3) generates 0, 1, 2. The loop prints each number separated by a space (end=“ ”).

Q24. Which keyword is used to define a function in Python? A) def B) func C) define D) function

Correct Answer: A

Rationale: Functions are defined with the def keyword, followed by the function name and parentheses.

Q25. What is the output of: def add(a, b): return a + b print(add(2) + add(3,4)) A) 9 B) TypeError C) 7

Rationale: The for loop is designed for iterating over sequences (lists, strings, ranges, etc.) when the number of iterations is known or the sequence is iterable.

Q29. What is the output of: x = 10 while x > 5: print(x, end=“ ”) x -= 2 A) 10 8 6 B) 10 9 8 7 6 C) 10 8 6 4 D) 10 8 6 4 2

Correct Answer: A

Rationale: x starts at 10, prints 10, then becomes 8; prints 8, becomes 6; prints 6, becomes 4 (loop stops because 4 is not >5). Output is “10 8 6”.

Q30. Which statement is used to skip the current iteration of a loop? A) continue B) break C) pass D) skip

Correct Answer: A

Rationale: continue jumps to the next iteration of the loop, skipping the remaining code in the current iteration.

Q31. What is the purpose of the pass statement? A) Do nothing (placeholder) B) Exit the loop C) Raise an error D) Return a value

Correct Answer: A

Rationale: pass is a null operation used when syntax requires a statement but no action is needed (e.g., in empty functions or classes).

Q32. Which of the following creates an infinite loop? A) for i in range(10): B) while True: C) for i in range(1, 1): D) while i < 10:

Correct Answer: B

Rationale: while True: will loop forever unless a break or return is encountered inside the loop.

Q33. What is the output of: def outer(): x = 5 def inner(): print(x) return inner my_func = outer() my_func() A) 5 B) Error (x not defined) C) None D)

Correct Answer: A

Rationale: This demonstrates a closure. The inner function captures the variable x from the outer function‘s scope, so when my_func() is called, it prints 5.

Q34. Which function returns the number of arguments passed to it? A) len() B) count() C) There is no built‑in function; use len(sys.argv) for command line D) argc()

Correct Answer: C

Rationale: f.read() without arguments reads the entire file content as a single string. The with statement ensures the file is properly closed.

Q38. Which method reads a single line from a file? A) readline() B) read() C) readlines() D) line()

Correct Answer: A

Rationale: readline() reads the next line from the file, including the newline character. readlines() returns a list of all lines.

Q39. What exception is raised when trying to open a non‑existent file for reading? A) IOError B) FileNotFoundError C) PermissionError D) OSError

Correct Answer: B

Rationale: In Python 3, FileNotFoundError (a subclass of OSError) is raised when a file does not exist and is opened in read mode.

Q40. Which block is guaranteed to execute regardless of whether an exception occurs? A) try B) except C) else D) finally

Correct Answer: D

Rationale: The finally block always executes after the try/except blocks, even if an exception is not caught or if a return statement is encountered.

Q41. Which keyword is used to raise an exception manually? A) throw B) raise C) except D) error

Correct Answer: B

Rationale: raise is used to raise an exception in Python (e.g., raise ValueError(“Invalid input”)).

Q42. What is the output? try: x = int(“abc”) except ValueError: print(“Error”) else: print(“Success”) A) Error B) Success C) SyntaxError D) Nothing

Correct Answer: A

Rationale: int(“abc”) raises a ValueError, so the except block executes, printing “Error”. The else block is skipped.

Q43. Which mode would you use to append data to an existing file? A) “w” B) “a” C) “r+” D) “x”

Correct Answer: B

Rationale: “a” (append) opens the file for writing at the end; if the file does not exist, it is created.

A) It executes if no exception occurs B) It executes if an exception occurs C) It always executes D) It defines a custom exception

Correct Answer: A

Rationale: The else block runs only if the try block completes without raising an exception.

Q48. Which function checks if a file exists? A) os.path.exists() B) file.exists() C) exists() D) os.exists()

Correct Answer: A

Rationale: os.path.exists(path) returns True if the path refers to an existing file or directory.

Q49. What is the output? f = open(“test.txt”, “w”) f.write(“Hello”) f.close() f = open(“test.txt”, “r”) print(f.read()) A) Hello B) Hello (then error) C) Nothing D) Error (file not found)

Correct Answer: A

Rationale: The file is created and written, then reopened for reading. The content “Hello” is printed.

Q50. Which statement is true about the with statement? A) It automatically closes the file after the block B) It is used only for reading files

C) It cannot handle exceptions D) It writes data to the file

Correct Answer: A

Rationale: The with statement ensures that the file is properly closed when the block exits, even if an exception occurs.

SECTION 4 – DATA STRUCTURES (LISTS, DICTIONARIES, SETS, TUPLES) (Questions 51-70)

Q51. Which of the following creates a tuple? A) [1, 2, 3] B) {1, 2, 3} C) (1, 2, 3) D) {“a”:1, “b”:2}

Correct Answer: C

Rationale: Tuples are defined with parentheses ( ) and are immutable. Lists use [ ], sets use { }, dicts use {key:value}.

Q52. What is the output? my_list = [1, 2, 3, 4] print(my_list[1:3]) A) [2, 3] B) [2, 3, 4] C) [1, 2, 3] D) [2]

Correct Answer: A

Rationale: Slicing [start:end] includes start index (1) and excludes end index (3), so elements at indices 1 and 2 are returned: [2, 3].

Q53. Which method adds an element to the end of a list? A) append() B) extend() C) insert()

Rationale: get() returns the value for the key if it exists; otherwise, it returns the default (0). No KeyError is raised.

Q57. Which method removes and returns the last element of a list? A) pop() B) remove() C) delete() D) discard()

Correct Answer: A

Rationale: pop() with no argument removes and returns the last element. pop(i) removes and returns the element at index i.

Q58. What is the output? t = (1, 2, 3) t[0] = 10 A) (10, 2, 3) B) TypeError C) (1, 2, 3) D) None

Correct Answer: B

Rationale: Tuples are immutable, so attempting to assign a new value to an element raises a TypeError.

Q59. Which list method sorts the list in place? A) sorted() B) sort() C) order() D) arrange()

Correct Answer: B

Rationale: list.sort() sorts the list in place and returns None. sorted() returns a new sorted list without modifying the original.

Q60. What is the output? s = {1, 2, 3} s.add(3) print(s) A) {1, 2, 3} B) {1, 2, 3, 3} C) {1, 2} D) {1, 2, 3, 3} (order may vary)

Correct Answer: A

Rationale: Sets do not allow duplicate elements. Adding an existing element has no effect.

Q61. Which expression returns the number of key‑value pairs in a dictionary? A) len(dict) B) dict.count() C) dict.size() D) length(dict)

Correct Answer: A

Rationale: len() works on any container, including dictionaries, returning the number of keys.

Q62. What is the output? a = [1, 2, 3] b = a b.append(4) print(a) A) [1, 2, 3] B) [1, 2, 3, 4] C) [1, 2, 3, 4] (but a is unchanged) D) None

Correct Answer: B

Rationale: Lists are mutable and assignment copies the reference, not the object. Both a and b refer to the same list, so modifying b also modifies a.