Peer Instruction: Dictionaries Multiple Choice QuestionsΒΆ
- The order in which people finish a race
- Incorrect! A list sorted in increasing order can used for this.
- The ingredients necessary for a recipe
- Incorrect! A list can be used for listing down the ingredients for a recipe
- The names of world countries and their capital cities
- Correct! This requires a key-value pair i.e. country-capital and is thus best suited for dictionary.
- 50 random integers
- Incorrect! A list would be more suitable for 50 random integers.
Q-1: Which of the following is best suited for a dictionary instead of a list?
- counts.append(0)
- Correct! Since a new bird sighting has been appended into "kinds", its count has to be appended to the end of "counts" too. The last two lines irrespective of the if condition increment the corresponding count of a sighting by 1. So, the missing code needs to append 0 to "counts".
- counts.append(1)
- Incorrect! Since a new bird sighting has been appended into "kinds", its count has to be appended to the end of "counts" too. The last two lines irrespective of the if condition increment the corresponding count of a sighting by 1. So, the missing code needs to append 0 to "counts".
- counts.append(kind)
- Incorrect! Since a new bird sighting has been appended into "kinds", its count has to be appended to the end of "counts" too. The last two lines irrespective of the if condition increment the corresponding count of a sighting by 1. So, the missing code needs to append 0 to "counts".
- No code necessary there
- Incorrect! Since a new bird sighting has been appended into "kinds", its count has to be appended to the end of "counts" too. The last two lines irrespective of the if condition increment the corresponding count of a sighting by 1. So, the missing code needs to append 0 to "counts".
Q-2: What code should go in place of the missing code?
def new_sighting(kinds, counts, sighting):
'''(list of str, list of int, str) -> NoneType
Add new sighting to parallel lists kinds and counts.
'''
if sighting not in kinds:
kinds.append(sighting)
... missing code
ind = kinds.index(sighting)
counts[ind] = counts[ind] + 1
- {3:4, 5:8, 4:9}
- Incorrect! Both d[5] = and d[4] = adds new keys to the dictionary with values returned by d.get(). d.get(4, 8) = 8 as there exists no key = '4' in dictionary d. d.get(3,9) returns 4 as this value corresponds to key = '3'.
- {3:4, 5:8, 4:4}
- Correct! Both d[5] = and d[4] = adds new keys to the dictionary with values returned by d.get(). d.get(4, 8) = 8 as there exists no key = '4' in dictionary d. d.get(3,9) returns 4 as this value corresponds to key = '3'.
- {3:4, 5:4, 4:3}
- Incorrect! Both d[5] = and d[4] = adds new keys to the dictionary with values returned by d.get(). d.get(4, 8) = 8 as there exists no key = '4' in dictionary d. d.get(3,9) returns 4 as this value corresponds to key = '3'.
- Error caused by get
- Incorrect! Both d[5] = and d[4] = adds new keys to the dictionary with values returned by d.get(). d.get(4, 8) = 8 as there exists no key = '4' in dictionary d. d.get(3,9) returns 4 as this value corresponds to key = '3'.
Q-3: What is dictionary d created by the following code?
d = {3:4}
d[5] = d.get(4, 8)
d[4] = d.get(3, 9)
- {1:5, 2:5, 4:7}
- Correct! Both d[2] = and d[4] = adds new keys to the dictionary with values returned by d.get(). d.get(1,6) returns 5 as this value corresponds to key = '1'. d.get(3, 7) = 7 as there exists no key = '3' in dictionary d.
- {1:5, 2:6, 4:7}
- Incorrect! Both d[2] = and d[4] = adds new keys to the dictionary with values returned by d.get(). d.get(1,6) returns 5 as this value corresponds to key = '1'. d.get(3, 7) = 7 as there exists no key = '3' in dictionary d.
- {1:5, 2:1, 4:2}
- Incorrect! Both d[2] = and d[4] = adds new keys to the dictionary with values returned by d.get(). d.get(1,6) returns 5 as this value corresponds to key = '1'. d.get(3, 7) = 7 as there exists no key = '3' in dictionary d.
- Error caused by get
- Incorrect! Both d[2] = and d[4] = adds new keys to the dictionary with values returned by d.get(). d.get(1,6) returns 5 as this value corresponds to key = '1'. d.get(3, 7) = 7 as there exists no key = '3' in dictionary d.
Q-4: What is dictionary d created by the following code?
d = {1:5}
d[2] = d.get(1, 6)
d[4] = d.get(3, 7)
- List elements cannot be mutable, but dictionary values can be mutable
- Incorrect!
- Assigning to an index that does not exist in a list is an error, but assigning a value to a key that does not exist in a dictionary is not
- Correct!
- A list can contain a dictionary as one of its elements, but a dictionary cannot contain a list as one of its values
- Incorrect!
- There is a dict constructor that creates a dictionary from a suitable object, but there is no list constructor that similarly creates lists
- Incorrect!
Q-5: Which of the following is a difference between lists and dictionaries?
- {4:1, 5:[2,4]}
- Incorrect! Options A and C are equally good as 4:1 can also be represented as 4:[1].
- {4:[1], 5:[4,2]}
- Incorrect! Options A and C are equally good. 5:[4,2] is not correct as dictionaries are ordered and 2 appears before 4.
- {4:[1], 5:[2,4]}
- Incorrect! Options A and C are equally good as 4:1 can also be represented as 4:[1].
- Two of the above are equally good
- Correct! Options A and C are equally good as 4:1 can also be represented as 4:[1].
- All of the above are equally good
- Incorrect! Options A and C are equally good. 5:[4,2] is not correct as dictionaries are ordered and 2 appears before 4.
Q-6: What is the best inversion of this dictionary?
{1:4, 2:5, 4:5}
- December
- Incorrect! July has the most number of days when someone has a birthday i.e. 3. December has just 1 such day.
- July
- Correct! July has the most number of days when someone has a birthday i.e. 3. December has just 1 such day.
- Both July and December are equally covered
- Incorrect! July has the most number of days when someone has a birthday i.e. 3. December has just 1 such day.
Q-7: Here is a birthday month dictionary. Which month has the most coverage I.e., the month with the most days on which someone has a birthday?
{"December" : {24 : ["Dan", "Joe", "Steph"]},
"July" : {17 : ["Angelo"],
16 : ["Chris"],
1 : ["Canada"]
}
}
- December
- Incorrect! July and December are equally covered i.e. 2 days.
- July
- Incorrect! July and December are equally covered i.e. 2 days.
- Both July and December are equally covered
- Correct! July and December are equally covered i.e. 2 days.
Q-8: Here is a birthday month dictionary. Which month has the most coverage I.e., the month with the most days on which someone has a birthday?
{"December" : {24 : ["Dan", "Joe"], 23 : ["Steph"]},
"July" : {17 : ["Angelo"], 16 : ["Chris"]}}
- {'':'this', 'this':'is', 'is':'this', 'this':'was'}
- Incorrect! Keys in a dictionary must be unique.
- {'':['this'], 'this':['is', 'was'], 'is':['this']}
- Correct! 'this' is followed by either of 'is' and 'was' and 'is' could be followed by a 'this'.
- {'':['this'], 'this':['is', 'was'], 'is':['was']}
- Incorrect! 'this' is followed by either of 'is' and 'was' but 'is' cannot be followed by a 'was'.
- {'':'this', 'this':'is', 'is':'this'}
- Incorrect! No 'was' in this dictionary.
Q-9: What is the dictionary that should be created for the text: this is this was
- {'':'this', 'this':'is', 'is':'was', 'was':'this'}
- Incorrect! It's better to use a list for the words (values) which can follow a particular word (keys)
- {'':['this'], 'this':['is', 'was'], 'is':['was']}
- Incorrect! 'this' is not immediately followed by 'was'
- {'':['this'], 'this':['is'], 'is':['was'], 'was':['this']}
- Correct! 'this' is be followed by 'is' which is then followed by 'was' which is then finally followed by 'this'.
- {'':['this'], 'is':['was'], 'was':['this']}
- Incorrect! No 'this' key in this dictionary.
Q-10: What is the dictionary that should be created for the text: this is was this