Lab 10: Knowledge Check

These questions check your knowledge of concepts from this lab. Use the button at the bottom to show your answers once you've attempted them.

  1. True or false: When you use a for loop with a dictionary, the loop variable will only contain a key from the dictionary, and values must be accessed separately.
    True False
  2. True or false: If you have a dictionary within a dictionary, you can access a value in the inner dictionary by using indexing the outer dictionary twice, e.g., outer[outerKey][innerKey].
    True False
  3. If we run the following code, what is final the value of the variable taxes?
    taxes = { 'income': 50000 }
    taxes['taxable'] = taxes['income']
    taxes['income'] += 10000
    taxes['tax'] = taxes['taxable'] * 0.1

    1. { 'income': 50000, 'taxable': 50000, 'tax': 5000 }
    2. { 'income': 60000, 'taxable': 50000, 'tax': 5000 }
    3. { 'income': 60000, 'taxable': 60000, 'tax': 5000 }
    4. { 'income': 60000, 'taxable': 60000, 'tax': 6000 }
  4. You are working with code for a contacts app that stores names, phone numbers, and email addresses. The data is stored as a dictionary where names are the keys, and the values are inner dictionaries that have keys like 'phone', 'email', etc. What are some issues that might cause problems with this approach? (choose all that apply) (pick one or more)
    1. There would be problems if two people have the same name as each other.
    2. There would be problems if two people had the same phone number.
    3. The outer dictionary might not be big enough to fit someone's contacts if they had a lot of contacts.
    4. The code for changing someone's name would be much more complex than the code for changing someone's phone number.
    5. If someone doesn't have a phone number, it will be impossible to create a contact entry for them.
  5. Which of the following code snippets are correct ways to loop over a dictionary D such that within the loop, key holds a key and value holds the associated value? (pick one or more)
    1. for key in D:
          value = D
          ...
    2. for value in D:
          key = D.get(value)
          ...
    3. for key in D:
          value = D[key]
          ...
    4. for key, value in D:
          ...
    5. for key, value in D.items():
          ...
  6. Which of the following statements are true about dictionaries? (pick one or more)
    1. You can use .append to add key/value pairs.
    2. You can use .insert to add key/value pairs.
    3. You can use square brackets to access the value associated with a particular key.
    4. You can use .get to access the value associated with a particular key.
    5. You can use .pop to remove a key/value pair if you only know the value.
    6. The in operator checks keys and values.
    7. You can use dict[key] = value to assign a value to a dictionary slot specified using a key, even if that key isn't in the dictionary yet.

Table of Contents