C212 CORRECT FINAL EXAMS QUESTIONS AND
ANSWERS SURE A+
✔✔11. Why are set iterators different from list iterators - ✔✔Sets do not have an
ordering, so it doesn't make sense to add an element at a particular iterator position, or
to traverse a set backwards.
✔✔12. Write a loop that prints all elements that are in both Set<String> s and
Set<String> t. - ✔✔for (String str :s)
{ if (t.contains(str))
{ System.out.println(str); }
}
✔✔13. How do you find all keys and values in a map - ✔✔iterate through the key set
and find the values that correspond to the keys
✔✔14. Consider the types HashMap and TreeMap. What do they have in common -
✔✔they both implement the Map interface
✔✔15. What is the difference between a set and a map - ✔✔A set stores elements. A
map stores associations between keys and values.
,✔✔16. Why is the collection of the keys of a map a set and not a list - ✔✔The ordering
does not matter, and you cannot have duplicates.
✔✔17. Why is the collection of the values of a map not a set - ✔✔Because it might have
duplicates.
✔✔18. Suppose you want to track how many times each word occurs in a document.
Declare a suitable map variable. - ✔✔Map<String, Integer> wordFrequency;
(note: you can not use a Map<String, int> because you can not use primitive types as
type parameters in Java.
✔✔19. What is a Map<String, HashSet<String>> (Give a possible use for such a
structure) - ✔✔It associates strings with sets of strings. One application would be a
thesaurus that lists synonyms for a given word.
✔✔20. What is a hash function? What is a good hash function? - ✔✔A hash function
computes an integer value from an object. A good hash function minimizes collisions -
identical hash codes for different objects.
✔✔Define: stack - ✔✔a collection of elements with "last-in", "first-out" retrieval.
✔✔Define: queue - ✔✔a collection of elements with "first-in", "first-out" retrieval.
✔✔Define: priority queue - ✔✔unlike a regular queue, the priority queue does not
maintain a first-in, first-out discipline. Instead, elements are retrieved according to their
priority. Whenever an item is removed, it is the item with the most urgent priority.
✔✔22. Why would you want to declare a variable as Queue<String> q = new
LinkedList<>() instead of simply declaring it as a linked list - ✔✔This way, we can
ensure that only queue operations can be can be invoked on the q object.
✔✔23. Why wouldn't you want to use an array list for implementing a queue -
✔✔Depending on whether you consider the 0 position the head or the tail of the queue,
you would either either add or remove elements at that position. Both are inefficient
operations because all other elements need to be moved.
✔✔24. Why wouldn't you want to use a stack to manage print jobs - ✔✔Stacks use a
"last-in" "first-out" discipline. If you are the first one to submit a print job and lots of
people add print jobs before the printer has a chance to deal with your job, they get their
printouts first, and you have to wait until all other jobs are completed.
✔✔25. What does this code print?
Queue<String> q = new LinkedList<>();
, q.add("A");
q.add("B");
q.add("C");
while (q.size() > 0) { System.out.print (q.remove() + " " ); } - ✔✔A B C
✔✔26. What is the value of the reverse Polish notation expression 2 3 4 + 5 * * -
✔✔Stack Unread expression
Empty 2 3 4 + 5 * *
234+5**
234+5**
234+5**
275**
275**
2 35 *
70
✔✔27. What steps does the selection sort algorithm go through to sort the sequence 6
5 4 3 2 1 - ✔✔1 | 5 4 3 2 6
12|4356
123456
✔✔28. Define: selection sort algorithm - ✔✔The selection sort algorithm sorts an array
by repeatedly finding the smallest element of the unsorted tail region and moving it to
the front.
✔✔29. Define: big-Oh notation - ✔✔big-Oh notation describes the growth rate of a
function
✔✔30. Selection sort is an O(n2) algorithm. What does that mean - ✔✔Doubling the
data set means a fourfold increase in the processing time.
✔✔31. How large does n need to be so that ½ * n^2 is bigger than 5/2 * n - 3 - ✔✔If n is
4, then ½ n^2 is 8 and 5/2 n - 3 is 7.
✔✔32. Define: merge sort algorithm - ✔✔The merge sort algorithm sorts an array by
cutting the array in half, recursively sorting each half, and then merging the sorted
halves.
✔✔33. Manually run the merge sort algorithm on the array
8 7 6 5 4 3 2 1. - ✔✔First sort 8 7 6 5:
Recursively, first sort 8. It's sorted.
Sort 7. It's sorted.
Merge them: 7 8
Do the same with 6 5 to get 5 6.
Merge them to get 5 6 7 8.
ANSWERS SURE A+
✔✔11. Why are set iterators different from list iterators - ✔✔Sets do not have an
ordering, so it doesn't make sense to add an element at a particular iterator position, or
to traverse a set backwards.
✔✔12. Write a loop that prints all elements that are in both Set<String> s and
Set<String> t. - ✔✔for (String str :s)
{ if (t.contains(str))
{ System.out.println(str); }
}
✔✔13. How do you find all keys and values in a map - ✔✔iterate through the key set
and find the values that correspond to the keys
✔✔14. Consider the types HashMap and TreeMap. What do they have in common -
✔✔they both implement the Map interface
✔✔15. What is the difference between a set and a map - ✔✔A set stores elements. A
map stores associations between keys and values.
,✔✔16. Why is the collection of the keys of a map a set and not a list - ✔✔The ordering
does not matter, and you cannot have duplicates.
✔✔17. Why is the collection of the values of a map not a set - ✔✔Because it might have
duplicates.
✔✔18. Suppose you want to track how many times each word occurs in a document.
Declare a suitable map variable. - ✔✔Map<String, Integer> wordFrequency;
(note: you can not use a Map<String, int> because you can not use primitive types as
type parameters in Java.
✔✔19. What is a Map<String, HashSet<String>> (Give a possible use for such a
structure) - ✔✔It associates strings with sets of strings. One application would be a
thesaurus that lists synonyms for a given word.
✔✔20. What is a hash function? What is a good hash function? - ✔✔A hash function
computes an integer value from an object. A good hash function minimizes collisions -
identical hash codes for different objects.
✔✔Define: stack - ✔✔a collection of elements with "last-in", "first-out" retrieval.
✔✔Define: queue - ✔✔a collection of elements with "first-in", "first-out" retrieval.
✔✔Define: priority queue - ✔✔unlike a regular queue, the priority queue does not
maintain a first-in, first-out discipline. Instead, elements are retrieved according to their
priority. Whenever an item is removed, it is the item with the most urgent priority.
✔✔22. Why would you want to declare a variable as Queue<String> q = new
LinkedList<>() instead of simply declaring it as a linked list - ✔✔This way, we can
ensure that only queue operations can be can be invoked on the q object.
✔✔23. Why wouldn't you want to use an array list for implementing a queue -
✔✔Depending on whether you consider the 0 position the head or the tail of the queue,
you would either either add or remove elements at that position. Both are inefficient
operations because all other elements need to be moved.
✔✔24. Why wouldn't you want to use a stack to manage print jobs - ✔✔Stacks use a
"last-in" "first-out" discipline. If you are the first one to submit a print job and lots of
people add print jobs before the printer has a chance to deal with your job, they get their
printouts first, and you have to wait until all other jobs are completed.
✔✔25. What does this code print?
Queue<String> q = new LinkedList<>();
, q.add("A");
q.add("B");
q.add("C");
while (q.size() > 0) { System.out.print (q.remove() + " " ); } - ✔✔A B C
✔✔26. What is the value of the reverse Polish notation expression 2 3 4 + 5 * * -
✔✔Stack Unread expression
Empty 2 3 4 + 5 * *
234+5**
234+5**
234+5**
275**
275**
2 35 *
70
✔✔27. What steps does the selection sort algorithm go through to sort the sequence 6
5 4 3 2 1 - ✔✔1 | 5 4 3 2 6
12|4356
123456
✔✔28. Define: selection sort algorithm - ✔✔The selection sort algorithm sorts an array
by repeatedly finding the smallest element of the unsorted tail region and moving it to
the front.
✔✔29. Define: big-Oh notation - ✔✔big-Oh notation describes the growth rate of a
function
✔✔30. Selection sort is an O(n2) algorithm. What does that mean - ✔✔Doubling the
data set means a fourfold increase in the processing time.
✔✔31. How large does n need to be so that ½ * n^2 is bigger than 5/2 * n - 3 - ✔✔If n is
4, then ½ n^2 is 8 and 5/2 n - 3 is 7.
✔✔32. Define: merge sort algorithm - ✔✔The merge sort algorithm sorts an array by
cutting the array in half, recursively sorting each half, and then merging the sorted
halves.
✔✔33. Manually run the merge sort algorithm on the array
8 7 6 5 4 3 2 1. - ✔✔First sort 8 7 6 5:
Recursively, first sort 8. It's sorted.
Sort 7. It's sorted.
Merge them: 7 8
Do the same with 6 5 to get 5 6.
Merge them to get 5 6 7 8.