Dictionary Type and Creation
Given the code snippet user_info = {'name': 'Alex', 'role': 'Developer', 'active': True}, what type of object is user_info and which syntax would NOT correctly create an equivalent dictionary?
- A) u003Cclass 'dict'u003E; {'name': 'Alex', 'role': 'Developer', 'active': True}
- B) u003Cclass 'dict'u003E; dict({'name': 'Alex', 'role': 'Developer', 'active': True})
- C) u003Cclass 'dict'u003E; dict([('name', 'Alex'), ('role', 'Developer'), ('active', True)])
- D) u003Cclass 'dict'u003E; dict(name='Alex', role='Developer', active=True)
- E) u003Cclass 'dict'u003E; dict{'name': 'Alex', 'role': 'Developer', 'active': True}
Dictionary Methods: Retrieving Keys, Values, and Items
If student_record = {'id': 101, 'grade': 'A', 'passed': True}, which of the following statements will NOT correctly return all key-value pairs as tuples?
- A) student_record.items()
- B) list(student_record.items())
- C) [(k, v) for k, v in student_record.items()]
- D) student_record.itmes()
- E) tuple(student_record.items())
Mutability and Modification
A dictionary employee = {'name': 'Maria', 'department': 'IT', 'years': 3} is created, and you run employee['years'] = 4. What does this demonstrate about dictionaries in Python?
- A) Dictionaries are immutable and this will raise an error.
- B) Only values, not keys, can be changed in a dictionary.
- C) Dictionaries are mutable; keys and values can be updated after creation.
- D) The update will only occur if the key already exists.
- E) Dictionary mutability depends on the Python version.
Key Existence and Safe Access
Given info = {'email': 'alex@example.com', 'subscribed': False}, which code safely checks if the key 'email' is present and retrieves its value without risk of a KeyError?
- A) if 'email' in info: print(info['email'])
- B) if info['email']: print(info['email'])
- C) print(info.get('e-mail', 'Not Found'))
- D) if info.contains('email'): print(info['email'])
- E) print(info['emial'])
Pop Methods and Their Effects
Suppose data = {'x': 1, 'y': 2, 'z': 3}; after executing data.popitem(), which statement most accurately describes the result?
- A) The first item ('x', 1) is always removed, order is not preserved.
- B) The last inserted key-value pair is removed, mutating the original dictionary.
- C) All key-value pairs are deleted.
- D) popitem() deletes a random item, and Python raises KeyError if the dictionary is empty.
- E) Only the value of the last key is set to None.