Mastering Python Dictionaries: The Ultimate Interview Challenge Quiz

  1. 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?

    1. A) u003Cclass 'dict'u003E; {'name': 'Alex', 'role': 'Developer', 'active': True}
    2. B) u003Cclass 'dict'u003E; dict({'name': 'Alex', 'role': 'Developer', 'active': True})
    3. C) u003Cclass 'dict'u003E; dict([('name', 'Alex'), ('role', 'Developer'), ('active', True)])
    4. D) u003Cclass 'dict'u003E; dict(name='Alex', role='Developer', active=True)
    5. E) u003Cclass 'dict'u003E; dict{'name': 'Alex', 'role': 'Developer', 'active': True}
  2. 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?

    1. A) student_record.items()
    2. B) list(student_record.items())
    3. C) [(k, v) for k, v in student_record.items()]
    4. D) student_record.itmes()
    5. E) tuple(student_record.items())
  3. 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?

    1. A) Dictionaries are immutable and this will raise an error.
    2. B) Only values, not keys, can be changed in a dictionary.
    3. C) Dictionaries are mutable; keys and values can be updated after creation.
    4. D) The update will only occur if the key already exists.
    5. E) Dictionary mutability depends on the Python version.
  4. 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?

    1. A) if 'email' in info: print(info['email'])
    2. B) if info['email']: print(info['email'])
    3. C) print(info.get('e-mail', 'Not Found'))
    4. D) if info.contains('email'): print(info['email'])
    5. E) print(info['emial'])
  5. Pop Methods and Their Effects

    Suppose data = {'x': 1, 'y': 2, 'z': 3}; after executing data.popitem(), which statement most accurately describes the result?

    1. A) The first item ('x', 1) is always removed, order is not preserved.
    2. B) The last inserted key-value pair is removed, mutating the original dictionary.
    3. C) All key-value pairs are deleted.
    4. D) popitem() deletes a random item, and Python raises KeyError if the dictionary is empty.
    5. E) Only the value of the last key is set to None.