CS100-ArrayListHashMap.docx

advertisement

2/13/2012 Handout

Your name ________________________________________________________

Your netid ______________________________________________

At the end of the class, please return this form where you got it in a neat pile Circle the number that represents your answer.

Question 1: At the end of function runTests, what it contained in myWords?

1.

It is an array of 1000 elements, all null

2.

It is an array of 1000 elements, starting with "word0", "word1", "word2", etc.

3.

It is an array of 100000 elements, all null

4.

It is array of 100000 elements, starting with "word0", "word1", "word2", etc.

Question 2: What does add do when myArray is already full and a new word needs to be added?

1.

It uses an arraylist

2.

It uses the function expandArray

3.

It copies all the elements from the existing array to a new array of size one larger, then replaces newArray with that

4.

It makes a new array to hold the new elements then links the old array to the new array

Question 3: Let's say I start with an array of size 1, and I add n elements using the add function. The runtime of arraycopy is linear in the number of elements copied (that is, O(k) where k is the number of elements copied). What is the big O of adding all n elements one at a time.

1.

O(1)

2.

O(n)

3.

O(n log n)

4.

O(n^2)

5.

O(n^3)

Question 4: In HashMain we use the ArrayListHash which is like HashMap but only allows one type of keys and one type of values. What types does it store?

1.

The keys are strings and the values are Strings

2.

The keys are ints and the values are ints

3.

The keys are ints and the values are Strings

4.

The keys are Strings and the values are ints

Question 4: What does the code do with the data file you pass in?

1.

It determines how many unique words are in the file

2.

It determines if the file can be read the same backwards and forwards

3.

It counts how many times each word occurs in the file

4.

It maps each word in the file to a "code"

Question 5: What happens if two different (not equal) strings happen to have the same hashcode?

1.

One will replace the other in the map

2.

This can't happen: it violates one of the rules of hashcode

3.

The different words are stored in an ArrayList

4.

The different counts for each word with the same hashcode are kept in an array

Question 6: Imagine there was a bug in String where all strings started hashing to the value 13. If that happened, what would the runtime of looking something up in this hashmap be (n is the number of different elements in the HashMap)?

1.

O(1)

2.

O(n)

3.

O(log n)

4.

O(n^2)

Question 7: Imagine there were as bug in string where two equal string objects had different hashcodes. Explain why this would not work:

String string1 = new String("Ninja");

String string2 = new String("Ninja");

// two different strings, normally they would have the same hashcode but

// pretend they don't

// but you can assume that string1.equals(string2) returns true still myMap.put(string1,3); myMap.get(string2); //does not return the right value. Why?!?!?

Express your answer as a short sentence or two.

Download