1、Module 7 集合和泛型Copyright Tarena Corporation,2008.All rights reservedModule 7- 集合和泛型一、选择题: Question 1Given:11. public class Person 12. private name;13. public Person(String name) 14. this.name = name;15. 16. public int hashCode() 17. return 420;18. 19. Which is true?A. The time to find the value from
2、HashMap with a Person key dependson the size of the map.B. Deleting a Person key from a HashMap will delete all map entries forall keys of type Person.C. Inserting a second Person object into a HashSet will cause the firstPerson object to be removed as a duplicate.D. The time to determine whether a
3、Person object is contained in aHashSet is constant and does NOT depend on the size of the map.Answer: AQuestion 2Given:11. public static Collection get() 12. Collection sorted = new LinkedList();13. sorted.add( B ); sorted.add( C ); sorted.add( A );14. return sorted;15. 16. public static void main(S
4、tring args) 17. for (Object obj: get() 18. System.out.print(obj + , );19. 20. What is the result?A. A, B, C,B. B, C, A,C. Compilation fails.D. The code runs with no output.E. An exception is thrown at runtime.Answer: BCopyright Tarena Corporation,2008.All rights reservedQuestion 3Given:1. import jav
5、a.util.*;2. public class Example 3. public static void main(String args) 4. / insert code here5. set.add(new integer(2);6. set.add(new integer(l);7. System.out.println(set);8. 9. Which code, inserted at line 4, guarantees that this program willoutput 1, 2?A. Set set = new TreeSet();B. Set set = new
6、HashSet();C. Set set = new SortedSet();D. List set = new SortedList();E. Set set = new LinkedHashSet();Answer: AQuestion 4Given:1. import java.util.*;2. public class PQ 3. public static void main(String args) 4. PriorityQueue pq = new PriorityQueue();5. pq.add( carrot );6. pq.add( apple );7. pq.add(
7、 banana );8. System.out.println(pq.poll() + : + pq.peek();9. 10. What is the result?A. apple:appleB. carrot:appleC. apple:bananaD. banana:appleE. carrot:carrotF. carrot:bananaAnswer: CQuestion 5Given:1. import java.util.*;Copyright Tarena Corporation,2008.All rights reserved2. public class WrappedSt
8、ring 3. private String s;4. public WrappedString(String s) this.s = s; 5. public static void main(String args) 6. HashSet hs = new HashSet();7. WrappedString ws1 = new WrappedString( aardvark );8. WrappedString ws2 = new WrappedString( aardvark );9. String s1 = new String( aardvark );10. String s2 =
9、 new String( aardvark );11. hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2);12. System.out.println(hs.size(); What is the result?A. 0B. 1C. 2D. 3E. 4F. Compilation fails.G. An exception is thrown at runtime.Answer: DQuestion 6Click the Exhibit button.1. import java.util.*;2. public class TestSet 3.
10、 enum Example ONE, TWO, THREE 4. public static void main(String args) 5. Collection coll = new ArrayList();6. coll.add(Example.THREE);7. coll.add(Example.THREE);8. coll.add(Example.THREE);9. coll.add(Example.TWO);10. coll.add(Example.TWO);11. coll.add(Example.ONE);12. Set set = new HashSet(coll);13.
11、 14. Which statement is true about the set variable on line 12?A. The set variable contains all six elements from the coll collection,and the order is guaranteed to be preserved.B. The set variable contains only three elements from the collcollection, and the order is guaranteed to be preserved.C. T
12、he set variable contains all six elements from the coil collection,but the order is NOT guaranteed to be preserved.D. The set variable contains only three elements from the coilCopyright Tarena Corporation,2008.All rights reservedcollection, but the order is NOT guaranteed to be preserved.Answer: DQ
13、uestion 7A programmer has an algorithm that requires a java.util.List thatprovides an efficient implementation of add(0,object), but doesNOT need to support quick random access. What supports theserequirements?A. java.util.QueueB. java.util.ArrayListC. java.util.LinearListD. java.util.LinkedListAnsw
14、er: DQuestion 8Click the Exhibit button.1. import java.util.*;2. class KeyMaster 3. public int i;4. public KeyMaster(int i) this.i = i; 5. public boolean equals(Object o) return i = (KeyMaster)o).i; 6. public int hashCode() return i; 7. 8. public class MapIt 9. public static void main(String args) 1
15、0. Set set = new HashSet();11. KeyMaster k1 = new KeyMaster(1);12. KeyMaster k2 = new KeyMaster(2);13. set.add(k1); set.add(k1);14. set.add(k2); set.add(k2);15. System.out.print(set.size() + : );16. k2.i = 1;17. System.out.print(set.size() + : );18. set.remove(k1);19. System.out.print(set.size() + :
16、 );20. set.remove(k2);21. System.out.print(set.size();22. 23. What is the result?A. 4:4:2:2B. 4:4:3:2C. 2:2:1:0D. 2:2:0:0Copyright Tarena Corporation,2008.All rights reservedE. 2:1:0:0F. 2:2:1:1G. 4:3:2:1Answer: FQuestion 9Given:11. public static void append(List list) list.add( 0042 ); 12. public s
17、tatic void main(String args) 13. List intList = new ArrayList();14. append(intList);15. System.out.println(intList.get(0);16. What is the result?A. 42B. 0042C. An exception is thrown at runtime.D. Compilation fails because of an error in line 13.E. Compilation fails because of an error in line 14.An
18、swer: BQuestion 10Given:int myArray=newint 1, 2,3,4, 5;What allows you to create a list from this array?A. List myList = myArray.asList();B. List myList = Arrays.asList(myArray);C. List myList = new ArrayList(myArray);D. List myList = Collections.fromArray(myArray);Answer: BQuestion 11Given:34. Hash
19、Map props = new HashMap();35. props.put( key45 , some value );36. props.put( key12 , some other value );37. props.put( key39 , yet another value );38. Set s = props.keySet();39. / insert code hereWhat, inserted at line 39, will sort the keys in the props HashMap?A. Arrays.sort(s);B. s = new TreeSet(
20、s);C. Collections.sort(s);Copyright Tarena Corporation,2008.All rights reservedD. s = new SortedSet(s);Answer: BQuestion 12Given:11. public class Person 12. private String name, comment;13. private int age;14. public Person(String n, int a, String c) 15. name = n; age = a; comment = c;16. 17. public
21、 boolean equals(Object o) 18. if(! (o instanceof Person) return false;19, Person p = (Person)o;20. return age = p.age & name.equals(p.name);21. 22. What is the appropriate definition of the hashCode method in classPerson?A. return super.hashCode();B. return name.hashCode() + age * 7;C. return name.h
22、ashCode() + comment.hashCode() /2;D. return name.hashCode() + comment.hashCode() / 2 - age * 3;Answer: BQuestion 13Given:11. public class Key 12. private long id1;13. private long 1d2;14.15. / class Key methods16. A programmer is developing a class Key, that will be used as a key ina standard java.u
23、til.HashMap. Which two methods should beoverridden to assure that Key works correctly as a key? (Choose two.)A. public int hashCode()B. public boolean equals(Key k)C. public int compareTo(Object o)D. public boolean equals(Object o)E. public boolean compareTo(Key k)Answer: ADCopyright Tarena Corporat
24、ion,2008.All rights reservedQuestion 14Given:11. public class Person 12. private name;13. public Person(String name) 14. this.name = name;15. 16. public boolean equals(Object o) 17. if( !o instanceof Person ) return false;18. Person p = (Person) o;19. return p.name.equals(this.name);20. 21. Which is
25、 true?A. Compilation fails because the hashCode method is not overridden.B. A HashSet could contain multiple Person objects with the samename.C. All Person objects will have the same hash code because thehashCode method is not overridden.D. If a HashSet contains more than one Person object withname=
26、 Fred , then removing another Person, also with name= Fred ,will remove them all.Answer: BQuestion 15Given:1. public class Person 2. private String name;3. public Person(String name) this.name = name; 4. public boolean equals(Person p) 5. return p.name.equals(this.name);6. 7. Which is true?A. The eq
27、uals method does NOT properly override the Object.equalsmethod.B. Compilation fails because the private attribute p.name cannot beaccessed in line 5.C. To work correctly with hash-based data structures, this class mustalso implement the hashCode method.D. When adding Person objects to a java.util.Se
28、t collection, the equalsmethod in line 4 will prevent duplicates.Answer: ACopyright Tarena Corporation,2008.All rights reservedQuestion 16Which two statements are true about the hashCode method? (Choosetwo.)A. The hashCode method for a given class can be used to test forobject equality and object in
29、equality for that class.B. The hashCode method is used by the java.util.SortedSet collectionclass to order the elements within that set.C. The hashCode method for a given class can be used to test forobject inequality, but NOT object equality, for that class.D. The only important characteristic of t
30、he values returned by ahashCode method is that the distribution of values must follow aGaussian distribution.E. The hashCode method is used by the java.util.HashSet collectionclass to group the elements within that set into hash buckets forswift retrieval.Answer: CEQuestion 17Given:enum Example ONE, TWO, THREE Which is true?A. The expressions (ONE = ONE) and ONE.equals(ONE) are both
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1