CPSC426 Java EH421

Exercise/Homework EH42

Q1: The collections framework algorithms are ______, i.e., each of these algorithms can operate on objects that offer given interfaces without concern to the underlying implementations.

  1. Stable.
  2. Lexicographical.
  3. Polymorphic.
  4. Implementation dependent.

ANS:

Q2: Collections methodsortthat accepts a List as an argument sorts the elements of a List, which must implement the______interface.

  1. Comparable.
  2. Comparator.
  3. Compare.
  4. Ordering.

ANS:

Q3: Collections method ______returns a Comparator object that orders the collection’s elements in reverse order.

  1. rotate.
  2. shuffle.
  3. reverse.
  4. reverseOrder.

ANS:

Q4: Comparator method compare should return ______if the first argument is greater than the second argument.

  1. a positive int value.
  2. zero.
  3. a negative int value.
  4. a String.

ANS:

Q5: Algorithm ______randomly orders a List's elements.

  1. random.
  2. randomsort.
  3. distribute.
  4. shuffle.

ANS:

Q6: Method shuffle is a member of ______.

  1. Class Arrays.
  2. Class Collections.
  3. Interface Collection.
  4. Interface List.

ANS:

Q7: Class Collections provides algorithms for reversing, filling and copying ______.

  1. Lists.
  2. Collections.
  3. Arrays.
  4. Stacks.

ANS:

Q8: To find the smallest and largest element of a Collection, use Collections methods ______and ______.

  1. least, greatest.
  2. small, large.
  3. first, last.
  4. min, max.

ANS:

Q9: If the desired Object is not found, binarySearch returns ______.

  1. A positive value
  2. Zero
  3. A negative value
  4. An ObjectNotFoundError.

ANS:

Q10: Which statement is false?

  1. Java does not guarantee which item will be found first when a binarySearch is performed on a List containing multiple elements equivalent to the search key.
  2. If the search key is found, method binarySearch returns the List index (position relative to 1) of the element containing the search key.
  3. The binary search algorithm is fast.
  4. Method binarySearch takes a List as the first argument.

ANS:

Q11: Among others, J2SE 5.0 includes three new collections algorithms. Which of the following is not a new algorithm?

  1. add.
  2. addAll.
  3. frequency.
  4. disjoint.

ANS:

Q12. Write a program that determines and prints the number of duplicate words in a sentence. Treat uppercase and lowercase letters the same. Ignore punctuation.

ANS: