D 522 Exam 3 V3 | D 522 Python for IT Automation
| Actual Q&A with Rationale (D522 Exam 3) |
Western Governors University
1. When using the ‘re’ module in Python, which function should a developer use to find all
non-overlapping occurrences of a pattern in a string and return them as a list?
A. re.findall()
B. re.match()
C. re.search()
D. re.finditer()
Correct Answer: A
Explanation: The re.findall() function scans the target string from left to right and extracts
every instance that matches the pattern. It returns a list of strings if there are no groups in
the pattern, whereas re.search only returns the first match object. This behavior is ideal for
parsing log files where multiple entries of interest may occur.
2. In the context of Python file operations, what is the primary advantage of using a ‘with’
statement when opening a file?
A. It automatically converts the file contents into a dictionary.
B. It optimizes the read speed by loading the file into cache.
C. It ensures the file is properly closed even if an exception is raised.
,D. It prevents other users from reading the file simultaneously.
Correct Answer: C
Explanation: The ‘with’ statement serves as a context manager that streamlines resource
management. By using this construct, the file’s close() method is invoked automatically at
the end of the block. This prevents resource leaks and file corruption issues that occur
when a program crashes before a manual close operation.
3. Which standard library module provides tools for manipulating the system’s path and
interacting with the underlying operating system files and directories?
A. sys
B. os
C. math
D. re
Correct Answer: B
Explanation: The ‘os’ module is the standard interface for interacting with the operating
system in Python. It allows developers to perform tasks such as creating directories, listing
files, and modifying environment variables. While ‘sys’ provides access to variables used by
the interpreter, ‘os’ is essential for file-system level automation.
4. A developer needs to convert a Python dictionary into a JSON-formatted string to send
over a network. Which method from the ‘json’ module is most appropriate?
A. json.dumps()
, B. json.dump()
C. json.loads()
D. json.load()
Correct Answer: A
Explanation: The ‘json.dumps()’ function stands for ‘dump string’ and is used to serialize a
Python object into a JSON formatted string. Conversely, ‘json.dump()’ is used to write JSON
directly to a file-like object. Using the correct serialization method is crucial for data
interchange between heterogeneous systems in IT automation.
5. What will be the output of the following code snippet?
list1 = [1, 2, 3] list2 = list1 list2.append(4) print(list1)
A. [1, 2, 3, 4]
B. [1, 2, 3]
C. Error: List is immutable
D. [1, 2, 3, [4]]
Correct Answer: A
Explanation: In Python, assigning one list to another variable does not create a copy;
instead, it creates a new reference to the same list object in memory. Because lists are
mutable, any operation performed on ‘list2’ directly impacts the object referenced by ‘list1’.
To create an independent list, one should use the copy() method or slicing syntax.
| Actual Q&A with Rationale (D522 Exam 3) |
Western Governors University
1. When using the ‘re’ module in Python, which function should a developer use to find all
non-overlapping occurrences of a pattern in a string and return them as a list?
A. re.findall()
B. re.match()
C. re.search()
D. re.finditer()
Correct Answer: A
Explanation: The re.findall() function scans the target string from left to right and extracts
every instance that matches the pattern. It returns a list of strings if there are no groups in
the pattern, whereas re.search only returns the first match object. This behavior is ideal for
parsing log files where multiple entries of interest may occur.
2. In the context of Python file operations, what is the primary advantage of using a ‘with’
statement when opening a file?
A. It automatically converts the file contents into a dictionary.
B. It optimizes the read speed by loading the file into cache.
C. It ensures the file is properly closed even if an exception is raised.
,D. It prevents other users from reading the file simultaneously.
Correct Answer: C
Explanation: The ‘with’ statement serves as a context manager that streamlines resource
management. By using this construct, the file’s close() method is invoked automatically at
the end of the block. This prevents resource leaks and file corruption issues that occur
when a program crashes before a manual close operation.
3. Which standard library module provides tools for manipulating the system’s path and
interacting with the underlying operating system files and directories?
A. sys
B. os
C. math
D. re
Correct Answer: B
Explanation: The ‘os’ module is the standard interface for interacting with the operating
system in Python. It allows developers to perform tasks such as creating directories, listing
files, and modifying environment variables. While ‘sys’ provides access to variables used by
the interpreter, ‘os’ is essential for file-system level automation.
4. A developer needs to convert a Python dictionary into a JSON-formatted string to send
over a network. Which method from the ‘json’ module is most appropriate?
A. json.dumps()
, B. json.dump()
C. json.loads()
D. json.load()
Correct Answer: A
Explanation: The ‘json.dumps()’ function stands for ‘dump string’ and is used to serialize a
Python object into a JSON formatted string. Conversely, ‘json.dump()’ is used to write JSON
directly to a file-like object. Using the correct serialization method is crucial for data
interchange between heterogeneous systems in IT automation.
5. What will be the output of the following code snippet?
list1 = [1, 2, 3] list2 = list1 list2.append(4) print(list1)
A. [1, 2, 3, 4]
B. [1, 2, 3]
C. Error: List is immutable
D. [1, 2, 3, [4]]
Correct Answer: A
Explanation: In Python, assigning one list to another variable does not create a copy;
instead, it creates a new reference to the same list object in memory. Because lists are
mutable, any operation performed on ‘list2’ directly impacts the object referenced by ‘list1’.
To create an independent list, one should use the copy() method or slicing syntax.