Due to the print book page limit, we cannot inlcude all good CheckPoint questions in the physical book. The CheckPoint on this Website may contain extra questions not printed in the book. The questions in some sections may have been reordered as a result. Nevertheless, it is easy to find the CheckPoint questions in the book on this Website. Please send suggestions and errata to Dr. Liang at y.daniel.liang@gmail.com. Indicate the book, edition, and question number in your email. Thanks!

Chapter 20 Check Point Questions

Section 20.2
20.2.1
What is a data structure?
20.2.2
Describe the Java Collections Framework. List the interfaces, convenience abstract classes, and concrete classes under the Collection interface.
20.2.3
Can a collection object be cloned and serialized?
20.2.4
What method do you use to add all the elements from one collection to another collection?
20.2.5
When should a method throw an UnsupportedOperationException?
Section 20.3
20.3.1
How do you obtain an iterator from a collection object?
20.3.2
What method do you use to obtain an element in the collection from an iterator?
20.3.3
Can you use a foreach loop to traverse the elements in any instance of Collection?
20.3.4
When using a foreach loop to traverse all elements in a collection, do you need to use the next() or hasNext() methods in an iterator?
Section 20.4
20.4.1
Can you use the forEach method on any instance of Collection? Where is the forEach method defined?
20.4.2
Suppose each element in list is a StringBuilder, write a statement using a forEach method to change the first character to uppercase for each element in list.
Section 20.5
20.5.1
How do you add and remove elements from a list? How do you traverse a list in both directions?
20.5.2
Suppose that list1 is a list that contains the strings red, yellow, and green, and that list2 is another list that contains the strings red, yellow, and blue. Answer the following questions:
a. What are list1 and list2 after executing list1.addAll(list2)?
b. What are list1 and list2 after executing list1.add(list2)?
c. What are list1 and list2 after executing list1.removeAll(list2)?
d. What are list1 and list2 after executing list1.remove(list2)?
e. What are list1 and list2 after executing list1.retainAll(list2)?
f. What is list1 after executing list1.clear()?
20.5.3
What are the differences between ArrayList and LinkedList? Which list should you use to insert and delete elements at the beginning of a list?
20.5.4
Are all the methods in ArrayList also in LinkedList? What methods are in LinkedList but not in ArrayList?
20.5.5
How do you create a list from an array of objects?
Section 20.6
20.6.1
What are the differences between the Comparable interface and the Comparator interface? In which package is Comparable, and in which package is Comparator?
20.6.2
How do you define a class A that implements the Comparable interface? Are two instances of class A comparable? How do you define a class B that implements the Comparator interface and override the compare method to compare two objects of type B1? How do you invoke the sort method to sort a list of objects of the type B1 using a comparator?
20.6.3
Write a lambda expression to create a comparator that compares two Loan objects by their annualInterestRate. Create a comparator using the Comparator.comparing method to compare Loan objects on annualInterestRate. Create a comparator to compare Loan objects first on annualInterestRate then on loanAmount.
20.6.4
Create a comparator using a lambda expression and using the Comparator.comparing method, respectively, to compare Collection objects on their size.
20.6.5
Write a statement that sorts an array named points of Point2D objects on their y values and then on their x values.
20.6.6
Write a statement that sorts an ArrayList of strings named list in increasing order of their last character.
20.6.7
Write a statement that sorts a two-dimensional array of double[][] in increasing order of their second column. For example, if the array is double[][] x = {{3, 1}, {2, -1}, {2, 0}}, the sorted array will be {{2, -1}, {2, 0}, {3, 1}}.
20.6.8
Write a statement that sorts a two-dimensional array of double[][] in increasing order of their second column as the primary order and the first column as the secondary order. For example, if the array is double[][] x = {{3, 1}, {2, -1}, {2, 0}, {1, -1}}, the sorted array will be {{1, -1}, {2, -1}, {2, 0}, {3, 1}}.
Section 20.7
20.7.1
Are all the methods in the Collections class static?
20.7.2
Which of the following static methods in the Collections class are for lists, and which are for collections?
sort, binarySearch, reverse, shuffle, max, min, disjoint, frequency
20.7.3
Show the output of the following code:
import java.util.*;

public class Test {
  public static void main(String[] args) {
    List<String> list =
      Arrays.asList("yellow", "red", "green", "blue");
    Collections.reverse(list);
    System.out.println(list);

    List<String> list1 = 
      Arrays.asList("yellow", "red", "green", "blue");
    List<String> list2 = Arrays.asList("white", "black");
    Collections.copy(list1, list2);
    System.out.println(list1);

    Collection<String> c1 = Arrays.asList("red", "cyan");
    Collection<String> c2 = Arrays.asList("red", "blue");
    Collection<String> c3 = Arrays.asList("pink", "tan");
    System.out.println(Collections.disjoint(c1, c2));
    System.out.println(Collections.disjoint(c1, c3));

    Collection<String> collection = 
      Arrays.asList("red", "cyan", "red");
    System.out.println(Collections.frequency(collection, "red"));
  }
}
20.7.4
Which method can you use to sort the elements in an ArrayList or a LinkedList? Which method can you use to sort an array of strings?
20.7.5
Which method can you use to perform binary search for elements in an ArrayList or a LinkedList? Which method can you use to perform binary search for an array of strings?
20.7.6
Write a statement to find the largest element in an array of comparable objects.
Section 20.8
20.8.1
What is the return value from invoking pane.getChildren() for a pane?
20.8.2
How do you modify the code in the MutilpleBallApp program to remove the first ball in the list when the button is clicked?
20.8.3
How do you modify the code in the MutilpleBallApp program so that each ball will get a random radius between 10 and 20?
Section 20.9
20.9.1
How do you create an instance of Vector? How do you add or insert a new element into a vector? How do you remove an element from a vector? How do you find the size of a vector?
20.9.2
How do you create an instance of Stack? How do you add a new element to a stack? How do you remove an element from a stack? How do you find the size of a stack?
20.9.3
Does Listing 20.1, TestCollection.java, compile and run if all the occurrences of ArrayList are replaced by LinkedList, Vector, or Stack?
Section 20.10
20.10.1
Is java.util.Queue a subinterface of java.util.Collection, java.util.Set, or java.util.List? Does LinkedList implement Queue?
20.10.2
How do you create a priority queue for integers? By default, how are elements ordered in a priority queue? Is the element with the least value assigned the highest priority in a priority queue?
20.10.3
How do you create a priority queue that reverses the natural order of the elements?
Section 20.11
20.11.1
Can the EvaluateExpression program evaluate the following expressions "1+2", "1 + 2", "(1) + 2", "((1)) + 2", and "(1 + 2)"?
20.11.2
Show the change of the contents in the stacks when evaluating "3 + (4 + 5) * (3 + 5) + 4 * 5" using the EvaluateExpression program.
20.11.3
If you enter an expression "4 + 5 5 5", the program will display 10. How do you fix this problem?