Hash Maps for Data Lookup Quiz Quiz

  1. Key Lookup Time

    Which time complexity best describes searching for a value by key in a well-implemented hash map (dictionary) with minimal collisions?

    1. O(1)
    2. O(n)
    3. O(log n)
    4. O(n log n)
    5. O(2^n)
  2. Duplicate Keys

    In a hash map or dictionary, what happens if you insert a new value with a key that already exists?

    1. The old value is overwritten by the new value
    2. Both values are stored under the same key
    3. An error is thrown
    4. A warning is issued but both values are kept
    5. The new value is discarded
  3. Python Hash Map Syntax

    In Python, which syntax correctly accesses the value associated with the key 'age' in the dictionary person = {'name': 'Sam', 'age': 30}?

    1. person['age']
    2. person.age
    3. person-u003E'age'
    4. person.age()
    5. person['Age']
  4. Dictionary Keys

    Which of the following can NOT be used as a key in a Python dictionary?

    1. A list
    2. A tuple
    3. An integer
    4. A string
    5. A floating-point number
  5. Checking Key Existence

    Suppose data = {'a': 1, 'b': 2, 'c': 3}. What’s the most efficient way to check if 'b' exists as a key?

    1. 'b' in data
    2. data.has_key('b')
    3. data.contains('b')
    4. data['b'] == True
    5. data.key('b')
  6. Updating Values

    Given my_dict = {'x': 1, 'y': 2}, which line will update the value of 'y' to 99?

    1. my_dict['y'] = 99
    2. update('y', 99)
    3. my_dict.update('y': 99)
    4. my_dict('y') = 99
    5. my_dict['y'] == 99
  7. Successful Use Case

    Which scenario best benefits from using a hash map (dictionary)?

    1. Looking up a user's email by their username
    2. Sorting a large list of numbers
    3. Iterating through numbers from 1 to 100,000
    4. Reversing a string
    5. Calculating the sum of a list
  8. Collision Handling

    What is the primary purpose of a hash function in a dictionary or hash map?

    1. To map keys to indices in the underlying array
    2. To sort keys alphabetically
    3. To convert all values to strings
    4. To encrypt the data stored
    5. To count the number of keys
  9. Removing Elements

    Given d = {'cat': 1, 'dog': 2}, which code removes the key 'dog'?

    1. d.pop('dog')
    2. delete d['dog']
    3. d.remove('dog')
    4. remove d['dog']
    5. pop(d, 'dog')
  10. Typos and Similar Keys

    What will happen if you try to access my_map['Adress'] when my_map = {'Address': '123 Main St', 'Email': 'john@example.com'}?

    1. A KeyError will be raised
    2. It will return the value for 'Address'
    3. It will return None
    4. It will automatically correct the typo
    5. It will raise a TypeError