A The equals method does NOT properly override the Objectequals method B

A the equals method does not properly override the

This preview shows page 77 - 80 out of 101 pages.

A. The equals method does NOT properly override the Object.equals method. B. Compilation fails because the private attribute p.name cannot be accessed in line 5. C. To work correctly with hash-based data structures, this class must also implement the hashCode method. D. When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates. Answer: A Question 172 Which two statements are true about the hashCode method? (Choose two.) A. The hashCode method for a given class can be used to test for object equality and object inequality for that class. B. The hashCode method is used by the java.util.SortedSet collection class to order the elements within that set. C. The hashCode method for a given class can be used to test for object inequality, but NOT object equality, for that class. D. The only important characteristic of the values returned by a hashCode method is that the distribution of values must follow a Gaussian distribution. E. The hashCode method is used by the java.util.HashSet collection class to group the elements within that set into hash buckets for swift retrieval. Answer: CE Question 173 Given: enum Example { ONE, TWO, THREE } Which is true? A. The expressions (ONE == ONE) and ONE.equals(ONE) are both guaranteed to be true. B. The expression (ONE < TWO) is guaranteed to be true and ONE.compareTo(TWO) is guaranteed to be less than one. C. The Example values cannot be used in a raw java.util.HashMap; instead, the programmer must use a java.util.EnumMap. D. The Example values can be used in a java.util.SortedSet, but the set will NOT be sorted because enumerated types do NOT implement java.lang.Comparable. Answer: A Question 174 Click the Exhibit button. 1. import java.util.*;
Image of page 77
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) { 10. Set<KeyMaster> set = new HashSet<KeyMaster>(); 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() + “:”); 20. set.remove(k2); 21. System.out.print(set.size()); 22. } 23. } What is the result? A. 4:4:2:2 B. 4:4:3:2 C. 2:2:1:0 D. 2:2:0:0 E. 2:1:0:0 F. 2:2:1:1 G. 4:3:2:1 Answer: F Question 175 Given: 1. import java.util.*; 2. public class Test { 3. public static void main(String[] args) { 4. List<String> strings = new ArrayList<String>(); 5. // insert code here 6. } 7. } Which four, inserted at line 5, will allow compilation to succeed? (Choose four.) A. String s = strings.get(0); B. Iterator i1 = strings.iterator(); C. String[] array1 = strings.toArray(); D. Iterator<String> i2 = strings.iterator();
Image of page 78
E. String[] array2 = strings.toArray(new String[1]); F. Iterator<String> i3 = strings.iterator<String>(); Answer: ABDE Question 176 Given: 1. import java.util.*; 2. public class Old { 3. public static Object get()(List list) { 4. return list.get(0); 5. } 6. } Which three will compile successfully? (Choose three.) A. Object o = Old.get0(new LinkedList()); B. Object o = Old.get0(new LinkedList<?>());
Image of page 79
Image of page 80

You've reached the end of your free preview.

Want to read all 101 pages?

  • Fall '15
  • Muler
  • Madrid Metro, Metropolitana di Napoli, class a, Osaka Municipal Subway, public class, public static void

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture