Assessment (OA) Study Guide | 100+ Practice Questions & Answers |
Comprehensive D522 Exam Prep | Python for IT Automation Fundamentals,
Scripting, Functions, Data Types, Control Flow, File Handling, Automation
Concepts & Troubleshooting | Detailed Explanations & Solutions | High-Yield
Review Material | Complete D522 Study Guide | Prepare Smarter & Pass
With Confidence
QUESTION 1: What is the primary advantage of using dictionaries in Python?
Answer:
Dictionaries allow you to associate values with descriptive keys, providing a convenient
way to represent relationships between data.
Rationale: Unlike lists that use numeric indices, dictionaries use descriptive keys, making
code more readable and data retrieval more intuitive for IT automation tasks.
QUESTION 2: Which Python code snippet correctly demonstrates the creation of a
dictionary with key-value pairs?
Answer:
devices = {"Router1": "192.168.1.2", "Switch2": "10.0.0.2"}
Rationale: Dictionaries are created using curly braces {} with key-value pairs separated by
colons. Keys are typically strings, and values can be any data type.
QUESTION 3: Which Python code snippet correctly demonstrates the creation of a
dictionary with various data types for both keys and values?
Answer:
data_types = {"string_key": [1, 2, 3], "double_key": 4, "boolean_key": True}
,Rationale: Python dictionaries support multiple data types for both keys (must be
immutable) and values (can be any type). This flexibility is crucial for IT automation.
QUESTION 4: What is the output of the following code?
python
network_devices = {"router": "192.168.1.1", "switch": "192.168.1.2"}
print(network_devices["router"])
Answer:
192.168.1.1
Rationale: Dictionary values are accessed using square bracket notation with the key.
"router" maps to "192.168.1.1".
QUESTION 5: Which method is used to add a new key-value pair to an existing
dictionary?
Answer:
dictionary["new_key"] = "new_value"
Rationale: Unlike other languages, Python allows direct assignment to a new key to add it
to the dictionary. No separate "add" method is required.
QUESTION 6: How do you check if a key exists in a Python dictionary?
Answer:
if "key_name" in dictionary:
Rationale: The in operator checks membership in dictionary keys. This is more Pythonic
than using .has_key() or other methods.
,QUESTION 7: Which method returns all keys in a dictionary as a view object?
Answer:
dictionary.keys()
Rationale: The .keys() method returns a dynamic view object that reflects changes to the
dictionary. This is useful for iterating through all keys.
QUESTION 8: Which method returns all values in a dictionary as a view object?
Answer:
dictionary.values()
Rationale: The .values() method returns a view of all values in the dictionary. This is
useful for collecting IP addresses, hostnames, or other value data.
QUESTION 9: What is the output of the following code?
python
servers = {"web": "10.0.0.1", "db": "10.0.0.2", "mail": "10.0.0.3"}
print(len(servers))
Answer:
3
Rationale: The len() function returns the number of key-value pairs in the dictionary. This
dictionary contains three pairs.
QUESTION 10: Which method removes a key-value pair from a dictionary and
returns its value?
Answer:
dictionary.pop("key")
, Rationale: The .pop() method removes the specified key and returns its value. This is
useful when you need to retrieve and remove an entry in one operation.
QUESTION 11: What is the output of the following code?
python
config = {"host": "server01", "port": 8080, "ssl": True}
config.pop("port")
print(config)
Answer:
{"host": "server01", "ssl": True}
Rationale: The .pop() method removes the specified key ("port") and its associated
value. The resulting dictionary contains only the remaining pairs.
QUESTION 12: Which method retrieves a value from a dictionary and returns a
default if the key is not found?
Answer:
dictionary.get("key", "default_value")
Rationale: The .get() method is safer than direct access because it returns a default value
(or None) when the key doesn't exist, preventing KeyError exceptions.
QUESTION 13: What is the output of the following code?
python
device = {"name": "Router", "ip": "192.168.1.1"}
print(device.get("location", "Unknown"))
Answer:
Unknown