Name:_______________________ Sample Exam Covers Chs 21-25 Part I: Questions: (1 pts each) 1. ________ is a data structure to store data in sequential order. a. A list b. A set c. A tree d. A stack e. A queue 2. A __________ (with no duplicate elements) has the property that for every node in the tree the value of any node in its left subtree is less than the value of the node and the value of any node in its right subtree is greater than the value of the node. a. binary tree b. binary search tree c. list d. linked list 3. The ________ is to visit the left subtree of the current node first, then the current node itself, and finally the right subtree of the current node. a. inorder traversal b. preorder traversal c. postorder traversal d. breadth-first traversal 4. The _________ is to visit the nodes level by level. First visit the root, then all children of the root from left to right, then grandchildren of the root from left to right, and so on. a. inorder traversal b. preorder traversal c. postorder traversal d. breadth-first traversal 5. To remove the root, you need to start a process by first placing _______ to the place of the root and move it down to maintain the heap property. a. one of the root's children b. the larger child of the root c. the smaller child of the root 1 d. the last node in the heap 6. To add a new node, you need to start a process by first placing it as _______ and move it up to main the heap property. a. the new root b. the last node in the heap c. the left child of the root d. the right child of the root 7. Which data structure is appropriate to store patients in an emergency room? a. Stack b. Queue c. Priority Queue d. Linked List 8. Which data structure is appropriate to store customers in a clinic for taking flu shots? a. Stack b. Queue c. Priority Queue d. Array List e. Linked List 9. a. b. c. d. Fill in the code in Comparable______ c = new Date(); <String> <?> <Date> <E> 10. Suppose List<String> list = new ArrayList<String>. Which of the following operations are correct? a. list.add("Red"); b. list.add(new Integer(100)); c. list.add(new java.util.Date()); d. list.add(new ArrayList()); 11. To declare a class named A with two generic types, use a. public class A<E> { ... } b. public class A<E, F> { ... } c. public class A(E) { ... } d. public class A(E, F) { ... } 12. The method header is left blank in the following code. Fill in the header. public class GenericMethodDemo { 2 public static void main(String[] args ) { Integer[] integers = {1, 2, 3, 4, 5}; String[] strings = {"London", "Paris", "New York", "Austin"}; print(integers); print(strings); } __________________________________________ { for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); System.out.println(); } } a. b. c. d. public public public public static static static static 13. To create a. <E extends b. <E extends c. <E> d. <E extends void void void void print(Integer[] list) print(String[] list) print(int[] list) print(Object[] list) a generic type bounded by Number, use Number> Object> Integer> 14. Which of the following declarations use raw type? a. ArrayList<Object> list = new ArrayList<Object>(); b. ArrayList<String> list = new ArrayList<String>(); c. ArrayList<Integer> list = new ArrayList<Integer>(); d. ArrayList list = new ArrayList(); Key:d 15. Is ArrayList<Integer> a subclass of ArrayList<Object>? a. true b. false 16. Is ArrayList<Integer> a subclass of ArrayList<?>? a. true b. false 17. Does <? super Number> represent a superclass of Number? a. yes b. no 18. Which of the data type below does not allow duplicates? a. Set 3 b. c. d. e. List Vector Stack LinkedList 19. Which of the following data types does not implement the Collection interface? a. HashSet b. TreeSet c. ArrayList d. LinkedList e. Map 20. Which of the data type below could be used to store elements in their natural order based on the compareTo method. a. HashSet b. TreeSet c. LinkedHashSet d. Collection e. Set 21. If you want to store non-duplicated objects in the order in which they are inserted, you should use ____________. a. HashSet b. LinkedHashSet c. TreeSet d. ArrayList e. LinkedList 22. Which of the following is correct to perform the set union of two sets s1 and s2. a. s1.union(s2) b. s1 + s2 c. s1.addAll(s2) d. s1.add(s2) 23. Analyze the following code. import java.util.*; public class Test { public static void main(String[] args) throws Exception { Set set = new TreeSet(); set.add("Red"); 4 set.add("Green"); set.add("Blue"); System.out.println(set.first()); } } a. The program displays Red b. The program displays Blue c. The program displays Green d. The program may display Red, Blue, or Green. e. The program cannot compile, because the first() method is not defined in Set. 24. Analyze the following code. import java.util.*; public class Test { public static void main(String[] args) throws Exception { TreeSet set = new TreeSet(); set.add("Red"); set.add("Green"); set.add("Blue"); System.out.println(set.last()); } } a. The program displays Red b. The program displays Blue c. The program displays Green d. The program may display Red, Blue, or Green. e. The program cannot compile, because the first() method is not defined in Set. 25. Analyze the following code. import java.util.*; public class Test { public static void main(String[] args) throws Exception { TreeSet set = new TreeSet(); set.add("Red"); set.add("Yellow"); set.add("Green"); set.add("Blue"); 5 SortedSet temp = set.headSet("Purple"); System.out.println(temp.first()); } } a. The program displays Blue b. The program displays Red c. The program displays Green d. The program displays Yellow e. The program displays Purple 26. Analyze the following code. import java.util.*; public class Test { public static void main(String[] args) throws Exception { TreeSet set = new TreeSet(); set.add("Red"); set.add("Yellow"); set.add("Green"); set.add("Blue"); SortedSet temp = set.tailSet("Purple"); System.out.println(temp.first()); } } a. The program displays Red b. The program displays Blue c. The program displays Green d. The program displays Yellow e. The program displays Purple 27. Which method do you use to test if an element is in a set or list named x? a. (element instanceof List) || (element instanceof Set) b. x.in(element) c. x.contain(element) d. x.contains(element) e. x.include(element) 28. Which method do you use to find the number of elements in a set or list named x? a. x.length() b. x.count() c. x.counts() 6 d. e. x.size() x.sizes() 29. Which method do you use to remove an element from a set or list named x? a. x.delete(element) b. x.remove(element) c. x.deletes(element) d. x.removes(element) e. None of the above 30. What is the printout of the following code? List<String> list = new ArrayList<String>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); for (int i = 0; i < list.size(); i++) System.out.print(list.remove(i)); a. ABCD b. AB c. AC d. AD e. ABC 31. Which of the following is correct to sort the elements in a list lst? a. lst.sort() b. Collections.sort(lst) c. Arrays.sort(lst) d. new LinkedList(new String[]{"red", "green", "blue"}) 32. Which of the following statements are true? a. Collections.shuffle(list) returns a new list while the original list is not changed. b. Collections.reverse(list) returns a new list while the original list is not changed. c. Collections.sort(list) returns a new list while the original list is not changed. d. Collections.nCopies(int, Object) returns a new list that consists of n copies of the object. 33. Which of the following is correct to create a list from an array? a. new List({"red", "green", "blue"}) b. new List(new String[]{"red", "green", "blue"}) c. Arrays.asList(new String[]{"red", "green", "blue"}) 7 d. new ArrayList(new String[]{"red", "green", "blue"}) e. new LinkedList(new String[]{"red", "green", "blue"}) 34. Which of the following is correct to create a list from an array? a. new List({"red", "green", "blue"}) b. new List(new String[]{"red", "green", "blue"}) c. Arrays.asList(new String[]{"red", "green", "blue"}) d. new ArrayList(new String[]{"red", "green", "blue"}) e. new LinkedList(new String[]{"red", "green", "blue"}) 35. To find a maximum object in an array of strings (e.g., String[] names = {"red", "green", "blue"}), use a. Arrays.max(names) b. Arrays.sort(names) c. Collections.max(names) d. Collections.max(Arrays.asList(names)) e. None of the above 36. To empty a Collection or a Map, you use the __________ method. a. empty b. clear c. zero d. setEmpty 37. Analyze the following code: public class Test { public static void main(String[] args) { Map map = new HashMap(); map.put("123", "John Smith"); map.put("111", "George Smith"); map.put("123", "Steve Yao"); map.put("222", "Steve Yao"); } } a. After all the four entries are added to the map, "123" is a key that corresponds to the value "John Smith". b. After all the four entries are added to the map, "123" is a key that corresponds to the value "Steve Yao". c. After all the four entries are added to the map, "Steve Yao" is a key that corresponds to the value "222". d. After all the four entries are added to the map, "John Smith" is a key that corresponds to the value "123". e. A runtime error occurs because two entries with the same key "123" are added to the map. 8 Part II: Complete the following program. 1. (15 pts) Add the following new methods in BinaryTree. /** Searches element o in this binary tree */ public boolean search(Object o) /** Returns the depth of this binary tree. Depth is the * number of the nodes in the longest path of the tree */ public int depth() 2 (15 pts) Write a program that reads words from a text file and displays all the nonduplicate words in ascending order. The text file is passed as a command-line argument. 9 Key Part I: Questions: (1 pts each) 1. Key:a 2. Key:b 3. Key:a 4. Key:d 5. Key:d 6. Key:b 7. Key:c 8. Key:b 9. Key:c 10. Key:a 11. Key:b 12. Key:d 13. Key:a 14. Key:d 15. Key:b 16. Key:a 17. Key:a 18. Key:a 19. Key:e 20. Key:b 21. Key:b 22. Key:c 10 23. Key:e 24. Key:a 25. Key:a 26. Key:c 27. Key:d 28. Key:d 29. Key:b 30. Key:c 31. Key: b 32. Key:d 33. Key:c 34. Key:c 35. Key: d 36. Key:b 37. Key:b 11