Lab 11: 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. 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? (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.
  4. Which of the following code snippets is the correct way to loop over a dictionary D such that within the loop, key holds a key and value holds the associated value?
    1. for key, value in D:
          ...
    2. for key in D:
          value = D
          ...
    3. for value in D:
          key = D.get(value)
          ...
    4. for key in D:
          value = D[key]
          ...

Table of Contents