Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

CodeHS 5.8 HashMaps

Total questions: 10

Worksheet time: 6mins

Name
Class
Date
1.

What is a HashMap?

a)

a bunch of key-value pairs

b)

a type of ArrayList

c)

a type of 2D array

d)

a type of 3D array

2.

How many values can each key in a HashMap be associated with?

a)

one

b)

depends how you initialize the HashMap

c)

unlimited

d)

trick question--keys aren't associated with values in a HashMap

3.

Which of these lines of code creates a HashMap called foo that has an int for keys and a String for values?

a)
b)
c)
d)
4.

Which line of code will add a second phone number "312-423-1234" to Charles Babbage?

a)

phonebook.put("Charles Babbage", "312-423-1234");

b)

phonebook.put("Charles Babbage", "312-423-1234")

c)

phonebook.put("312-423-1234", "Charles Babbage");

d)

trick question--you can't add a second value to a key

5.

What will be printed to the screen when the following code is executed:

System.out.println(phonebook.get("Grace Hopper"));

a)

"650-352-7500"

b)

650-352-7500

c)

+44 330 058 0058

d)

"+44 330 058 0058"

e)

Grace Hopper

6.

What do you use to access every member of a HashMap?

a)

a for loop

b)

a for-each loop

c)

an array

d)

an ArrayList

7.

What do you use to find out how many key-value pairs are in the HashMap named bar?

a)

bar.size()

b)

bar.length

c)

bar.size

d)

bar.length()

8.

What method is used to remove a key-value pair from a HashMap?

a)

remove(key)

b)

delete(key)

c)

erase(key)

d)

clear(key)

9.

What happens if you try to get a value in a HashMap using a key that does not exist?

a)

Returns null

b)

Throws an exception

c)

Returns default value

d)

Program crashes

10.

Can a HashMap store multiple null values?

a)

Yes, multiple null values are allowed

b)

No, only one null value is allowed

c)

No, null values are not allowed

d)

Yes, but only if keys are not null