WorksheetsCodeHS 5.8 HashMaps
Total questions: 10
Worksheet time: 6mins
What is a HashMap?
a bunch of key-value pairs
a type of ArrayList
a type of 2D array
a type of 3D array
How many values can each key in a HashMap be associated with?
one
depends how you initialize the HashMap
unlimited
trick question--keys aren't associated with values in a HashMap
Which of these lines of code creates a HashMap called foo that has an int for keys and a String for values?
Which line of code will add a second phone number "312-423-1234" to Charles Babbage?
phonebook.put("Charles Babbage", "312-423-1234");
phonebook.put("Charles Babbage", "312-423-1234")
phonebook.put("312-423-1234", "Charles Babbage");
trick question--you can't add a second value to a key
What will be printed to the screen when the following code is executed:
System.out.println(phonebook.get("Grace Hopper"));
"650-352-7500"
650-352-7500
+44 330 058 0058
"+44 330 058 0058"
Grace Hopper
What do you use to access every member of a HashMap?
a for loop
a for-each loop
an array
an ArrayList
What do you use to find out how many key-value pairs are in the HashMap named bar?
bar.size()
bar.length
bar.size
bar.length()
What method is used to remove a key-value pair from a HashMap?
remove(key)
delete(key)
erase(key)
clear(key)
What happens if you try to get a value in a HashMap using a key that does not exist?
Returns null
Throws an exception
Returns default value
Program crashes
Can a HashMap store multiple null values?
Yes, multiple null values are allowed
No, only one null value is allowed
No, null values are not allowed
Yes, but only if keys are not null
