Skip to main content

Exercises 10.8 Multiple Choice Questions

1.

    Q-1: Which line of code correctly adds an item to the fruits dictionary with a key of ‘grapes' and a value of 15?
    fruits = {'apples': 1, 'bananas': 4, 'pears': 17, 'oranges': 14}
    
  • fruits['grapes']
  • Try again! This does not add a value.
  • fruits['grapes'] = 15
  • Correct! This line adds 'grapes' as a key with a value of 15.
  • insert 'grapes' in fruits
  • Try again! This is not correct Python.
  • fruits[15] = 'grapes'
  • Try again! This sets the key to 15 and the value to 'grapes'.

2.

    Q-2: What does the following code print?
    names = {'Janice': 5, 'Emily': 3, 'John': 7, 'Eleanor': 2}
    list_o_names = []
    for name in names:
        if names[name] > 5:
            list_o_names.append(name)
    print(list_o_names)
    
  • ['Janice', 'John']
  • Try again! This would be true if it was greater than or equal 5.
  • ['Janice', 'Emily', 'Eleanor']
  • Try again! This only adds items with a value greater than 5.
  • ['John']
  • Correct! John is the only key that has a value greater than 5.
  • ['Janice', 'Emily', 'John', 'Eleanor']
  • Try again! This only adds items with a value greater than 5.

3.

    Q-3: What does the following code print?
    names = {'Janice': 5, 'Emily': 3, 'John': 7, 'Eleanor': 2}
    list_o_names = []
    
    names['Emily'] += 10
    names['Erik'] = 22
    
    for name in names:
        if names[name] > 5:
            list_o_names.append(name)
    print(list_o_names)
    
  • ['Emily', 'John', 'Erik']
  • Correct! Erik is initialized to 22, and Emily is updated to 13. Therefore, these two entries are added to the list found in the previous question.
  • ['Janice', 'Emily', 'John']
  • Try again! Erik is set to 22, so it should be added.
  • ['Janice', 'John', 'Erik']
  • Try again! Emily is set to 13, so it should be added.
  • ['Janice', 'Emily', 'John', 'Eleanor']
  • Try again! This will not add Eleanor since the value is 2, and this will not add Janice since the value is 5.

4.

    Q-4: What is the value of counter after the code is run?
    phrase = "Cheese in Philadelphia is extraordinary according to Erik"
    
    counter = 0
    letters = {}
    for word in phrase.split():
        for letter in word:
            letter = letter.lower()
            if letter not in letters.keys():
                letters[letter] = 0
            letters[letter] += 1
    for key in letters.keys():
        if letters[key] > 2:
            counter += 1
    
  • 5
  • Try again!
  • 10
  • Try again!
  • 9
  • Correct! There are 9 letters in phrase that appear more than two times.

5.

    Q-5: Which line of code correctly gets the value of the key ‘apples' if it exists and returns 0 if it does not?
    fruits = {'bananas': 7, 'apples': 4, 'grapes': 19, 'pears': 4}
    
  • fruits.get(apples)
  • Try again! The key must be a string in this case.
  • fruits.get('apples', 0)
  • Correct! This correctly grabs the key as a string and also includes a default value in case the key is not present in the dictionary.
  • fruits.get('apple')
  • Try again! This will return None if the key does not exist.
  • fruits.get(apples, 0)
  • Try again! The key must be a string in this case.

6.

    Q-6: What value is printed when this code runs?
    word = 'brontosaurus'
    diction = {}
    for letter in word:
        if letter not in diction.keys():
            diction[letter] = 0
        diction[letter] += 1
    print(diction.get('o', 0) + 4)
    
  • 10
  • Try again!
  • 4
  • Try again!
  • 6
  • Correct! The get() method grabs the value 2 and then 4 gets added to it to get 6.
  • 2
  • Try again! This would be true if it just printed the value for the key 'o'. However, 4 is added to it.

7.

    Q-7: What order do the keys print in after the following code is run?
    counts = {'annie' : 42, 'chuck' : 1, 'jan' : 100}
    for key in counts:
        print(key)
    
  • jan, chuck, annie
  • The order is their order in the dictionary.
  • chuck, annie, jan
  • The order is their order in the dictionary.
  • annie, chuck, jan
  • Correct! The order is their order in the dictionary.
  • jan, annie, chuck
  • The order is their order in the dictionary.

8.

    Q-8: What order do the keys print in after the following code is run?
    counts = {'annie' : 42, 'chuck' : 1, 'jan' : 100}
    items = counts.items()
    out = sorted(items, reverse = True)
    for item in out:
        print(item[0])
    
  • jan, chuck, annie
  • Correct! This will sort by the keys in descending order.
  • chuck, annie, jan
  • This will sort by the keys in descending order.
  • annie, chuck, jan
  • This will sort by the keys in descending order.
  • jan, annie, chuck
  • This will sort by the keys in descending order.

9.

    Q-9: What order do the keys print in after the following code is run?
    counts = {'annie' : 42, 'chuck' : 1, 'jan' : 100}
    items = counts.items()
    out = sorted(items, key = lambda t: t[1])
    for item in out:
        print(item[0])
    
  • jan, chuck, annie
  • This will sort by the values in ascending order.
  • chuck, annie, jan
  • Correct! This will sort by the values in ascending order.
  • annie, chuck, jan
  • This will sort by the values in ascending order.
  • jan, annie, chuck
  • This will sort by the values in ascending order.

10.

    Q-10: What order do the keys print in after the following code is run?
    counts = {'annie' : 42, 'chuck' : 1, 'jan' : 100}
    items = counts.items()
    sorted(items, key = lambda t: t[1])
    for item in items:
        print(item[0])
    
  • jan, chuck, annie
  • This will print the keys in their current order since sorted returns a new list.
  • chuck, annie, jan
  • This will print the keys in their current order since sorted returns a new list.
  • annie, chuck, jan
  • Correct! This will print the keys in their current order since sorted returns a new list.
  • jan, annie, chuck
  • This will print the keys in their current order since sorted returns a new list.