Given this dictionary, key_to_value,
How many keys are inside the dict?
( ) Error: You cannot use the same key twice in a dictionary.
( ) 1
( ) 3
( ) 2
2
When we define key_to_val
, it has 0 keys. We add "a"
and it gets one key, then we update the value of "a"
, and it still has one key. We add "b"
and it gets a second key.
Suppose we have a list of names:
names = ["Joe", "Bob", "Mary", "Joe", "Bob", "Bob"]
Fill in the blank so that we create a dictionary that maps a
name to the number of times it appears in our list of names.
name_to_count
Given the following code:
What does prof_to_dept["Kathi"]
evaluate to?
( ) "CS"
( ) "English"
( ) Error: Cannot change value in a dict.
"CS"
Let's say we want to keep track of the courses that we're taking this semester at Brown.
What will the line of code student_to_courses["Leo"]
evaluate to?
( ) ["CS111", "ENGL400"]
( ) ["CS127", "CS111", "ENGL400"]
( ) ["ENGL400", "CS127", "CS111"]
( ) ["CS111", "ENGL400", "CS127"]
["CS111", "ENGL400", "CS127"]
When leo_courses
is updated, so is student_to_courses["Leo"]
, because they are two names for the same list.
Let's say we want to keep track of the number of fruits in a shopping cart.
What will the line of code cart["grape"]
evaluate to?
( ) 10
( ) 2
( ) 3
( ) error: cannot change the value in a dictionary
2
The key "grape" is initially mapped to a value of 2. Even though the name grape changes to 10, it does not change the original key-value mapping of grape to 2.
Suppose we have a list of fruits:
Will the following piece of code successfully create a dictionary that maps each fruit in fruits to the number of times it appears in the list?
( ) Yes
( ) No
No
Because the code does not check whether a fruit already exists in the dictionary, it overrides each fruit value to 0 and increments it by 1 in each iteration of the loop.
Assume we have a dictionary that maps student names to their heights. Fill in the blanks in the following function that returns the maximum height in students.
BLANK1: name
BLANK2: students