Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Section 9 Mock test questions  RSS feed

Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Section 9

Again, these are created questions. Please comment and point out any poor wording or problems.

1) Which two of the following statements are true?

1) All of the collections implement the Collection interface
2) Hashtable is an ordered collection
3) LinkedHashMap is faster than HashMap for adding and removing elements
4) None of the list collections are sorted
5) Vector and Hashtable methods are synchronized for thread safety
6) LinkedHashSet is considered a sorted set

2) A programmer wishes to implement a stack for his postfix operator evaluator.

Which of the following collection classes BEST fits his needs?

1) ArrayList
2) TreeSet
3) StackList
4) HashMap
5) LinkedHashMap
6) LinkedList

3) What will the output of the following code be?

1) Chocolate Orange Strawberry Vanilla
2) Chocolate Orange Strawberry Vanilla Vanilla
3) Chocolate Vanilla Strawberry Orange
4) Chocolate Vanilla Strawberry Vanilla Orange
5) Orange Strawberry Vanilla
6) Vanilla Strawberry Vanilla Orange

4 What two override return values for hashCode would be appropriate and legal for

this class?

1) return width;
2) return width * height / 10;
3) return width + height;
4) return width / 5 * height / 5;
5) return width * 10000 + height;
6) return 0 * height;

5) Which two statements are incorrect? The word equals refers to the correct overridden method of equals. It is assumed the hashcode method is created appropriately and legally for the objects in question.

1) If two objects return true for == they should return true for equals
2) If two objects return false for equals they may have the same hashcode
3) If two objects have the same hashcode they will also be equals
4) If two objects have different hashcodes they can be equal
5) x.equals(y) == true and y.equals(z) == true then x.equals(z) == true

1 Objective 9.1


1 False, the Maps do not derive or implement the Collection interface
2 Hashtable is an unsorted, unordered, map with synchronized methods
3 False, the overhead of the linked inofmration slows adding and removing elements
4 True, the only sorted collections are TreeSet and TreeMap
5 True, vector and hashtable methods are synchronized for thread safety
6 LinkedHashSet is ordered, but not sorted

2 Objective 9.1

6 Linked List

1) ArrayList is a good choice- it is ordered by index, but is not as good for many insertions and deletions as LinkedList
2) TreeSet - This does not allow the same element to be inserted twice- bad choice
3) StackList - This is not a collection object
4) HashMap - This enforces uniqueness of keys, not a good fit
5) LinkedHashMap - This also enforced uniqueness of keys
6) LinkedList - This is the best choice for implementing a stack

3) Objective 9.1

1) Chocolate Orange Strawberry Vanilla

The TreeSet is a set which only allows one of each type. Additionally, it sorts the elements (the tree part of the set does the sorting). Thus, the flavors are output in string comparison order

4) Objective 9.2

2, 6

A hashcode should return the same hash value for any two items which are considered equal. Here, the area of the window is considered.
1) the width is not sufficient to compare areas
2) This is correct, width * height yields the area, and this is divided by 10. Some windows with different areas will have the same hash due to rounding, but this is okay
3) The width + height does not return the same hash code for all Windows of equal area
4) This is close to correct, but because width/5 is computed first it rounds differently depending on the size of width. Two windows with the same area will have different hash values depending on their width
5) This will not return equal hashcodes for all windows of equal size
6) This will always return 0- causing all Windows to have a hash of 0 which is both legal and appropriate, although slow

5) Objective 9.2


1) Two objects which return true for == are the same object. Since equals is reflexive, it should return true.
2) All objects could fall into the same bucket- thus it is possible for unequal objects to have the same hashcode
3) If two object are equals they should have the same hashcode, but not the reverse
4) If two objects have different hashcodes they are not equal
5) equals is transitive

Section 9: The Collections Framework

1 Make appropriate selection of collection classes/interfaces to suit specified

behavior requirements.
2 Distinguish between correct and incorrect implementations of hashcode methods.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!