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 21 Check Point Questions

Section 21.2
21.2.1
How do you create an instance of Set? How do you insert a new element in a set? How do you remove an element from a set? How do you find the size of a set?
21.2.2
If two objects o1 and o2 are equal, what is o1.equals(o2) and o1.hashCode() == o2.hashCode()?
21.2.3
What are the differences between HashSet, LinkedHashSet, and TreeSet?
21.2.4
How do you traverse the elements in a set?
21.2.5
How do you sort the elements in a set using the compareTo method in the Comparable interface? How do you sort the elements in a set using the Comparator interface? What would happen if you added an element that could not be compared with the existing elements in a tree set? How does the TreeeSet test if two elements are equal?
21.2.6
Suppose that set1 is a set that contains the strings red, yellow, and green, and that set2 is another set that contains the strings red, yellow, and blue. Answer the following questions:
(a) What are in set1 and set2 after executing set1.addAll(set2)?
(b) What are in set1 and set2 after executing set1.add(set2)?
(c) What are in set1 and set2 after executing set1.removeAll(set2)?
(d) What are in set1 and set2 after executing set1.remove(set2)?
(e) What are in set1 and set2 after executing set1.retainAll(set2)?
(f) What is in set1 after executing set1.clear()?
21.2.7
Show the output of the following code:
import java.util.*;

public class Test {
  public static void main(String[] args) {
    LinkedHashSet<String> set1 = new LinkedHashSet<>();
    set1.add("New York");
    LinkedHashSet<String> set2 = set1;
    LinkedHashSet<String> set3 = 
      (LinkedHashSet<String>)(set1.clone());
    set1.add("Atlanta");
    System.out.println("set1 is " + set1);
    System.out.println("set2 is " + set2);
    System.out.println("set3 is " + set3);
    set1.forEach(e -> System.out.print(e + " "));
  }
}
21.2.8
Show the output of the following code:
Set<String> set = new LinkedHashSet<>();
set.add("ABC");
set.add("ABD");
System.out.println(set);
21.2.9
What will the output be if lines 6-7 in Listing 21.5 is replaced by the following code:
Set<GeometricObject> set = new HashSet<>();
21.2.10
Show the output of the following code:
Set<String> set = new TreeSet<>(
  Comparator.comparing(String::length));
set.add("ABC");
set.add("ABD");
System.out.println(set);
21.2.11
(This is not in the printed book) Show the output of the following code:
import java.util.*;
import java.io.*;

public class Test {
  public static void main(String[] args) throws Exception {
    ObjectOutputStream output = new ObjectOutputStream(
      new FileOutputStream("c:\\test.dat"));
    LinkedHashSet<String> set1 = new LinkedHashSet<>();
    set1.add("New York");
    LinkedHashSet<String> set2 = 
      (LinkedHashSet<String>)set1.clone();
    set1.add("Atlanta");
    output.writeObject(set1);
    output.writeObject(set2);
    output.close();

    ObjectInputStream input = new ObjectInputStream(
      new FileInputStream("c:\\test.dat"));
    set1 = (LinkedHashSet<String>)input.readObject();  
    set2 = (LinkedHashSet<String>)input.readObject();  
    System.out.println(set1);        
    System.out.println(set2);        
    input.close();
  }
}
Section 21.3
21.3.1
Suppose you need to write a program that stores unordered, non-duplicate elements, what data structure should you use?
21.3.2
Suppose you need to write a program that stores non-duplicate elements in the order of insertion, what data structure should you use?
21.3.3
Suppose you need to write a program that stores non-duplicate elements in increasing order of the element values, what data structure should you use?
21.3.4
Suppose you need to write a program that stores a fixed number of the elements (possibly duplicates), what data structure should you use?
21.3.5
Suppose you need to write a program that stores the elements in a list with frequent operations to append and delete elements at the end of the list, what data structure should you use?
21.3.6
Suppose you need to write a program that stores the elements in a list with frequent operations to insert and delete elements at the beginning of the list, what data structure should you use?
Section 21.4
21.4.1
Will the CountKeywords program work if lines 33-34 are changed to
Set<String> keywordSet = 
  new LinkedHashSet<>(Arrays.asList(keywordString));
21.4.2
Will the CountKeywords program work if lines 33-34 are changed to
List<String> keywordSet = 
  new ArrayList<String>(Arrays.asList(keywordString));
Section 21.5
21.5.1
How do you create an instance of Map? How do you add an entry to a map consisting of a key and a value? How do you remove an entry from a map? How do you find the size of a map? How do you traverse entries in a map?
21.5.2
Describe and compare HashMap, LinkedHashMap, and TreeMap.
21.5.3
Show the printout of the following code:
import java.util.*;
public class Test {
  public static void main(String[] args) {
    Map<Integer, String> map = new LinkedHashMap<>();
    map.put("123", "John Smith");
    map.put("111", "George Smith");
    map.put("123", "Steve Yao");
    map.put("222", "Steve Yao");
    System.out.println("(1) " + map);
    System.out.println("(2) " + new TreeMap<, String>(map));
            
    map.forEach((k, v) -> {
      if (k.equals("123")) System.out.println(v);});
  }
}
Section 21.6
21.6.1
Will the CountOccurrenceOfWords program work if line 10 is changed to
Map<String, int> map = new TreeMap<>();
21.6.2
Will the CountOccurrenceOfWords program work if line 17 is changed to
if (map.get(key) == null) {
21.6.3
Will the CountOccurrenceOfWords program work if lines 32-33 are changed to
for (String key: map)
  System.out.println(key + "\t" + map.getValue(key));
21.6.4
Replace the code in lines 17-24 in one line of code using a conditional expression.
Section 21.7
21.7.1
What is wrong in the following code?
Set<String> set = Collections.singleton("Chicago");
set.add("Dallas");
21.7.2
What happens when you run the following code?
List<String> list = Collections.unmodifiableList(
  Arrays.asList("Chicago", "Boston"));
list.remove("Dallas");