Вы находитесь на странице: 1из 7

Certified Java Programmer Mock Exam Collections 1 -------------------------------------------------------------------------------Question 1

A class C has legal implementations of the equals and hashCode methods. On Monda y, an instance of the class is created and the hashCode method is invoked. On Tu esday the program is loaded again and an instance of class C is created containi ng the same data that was loaded on Monday. If the hashCode method is invoked af ter restarting the program on Tuesday then the hashCode method must return the s ame integer value that was returned on Monday. a. false b. true -------------------------------------------------------------------------------Question 2

Suppose that class C has legal implementations of the hashCode and equals method s. Within any one execution of the Java application the hashCode contract requir es that each invocation of the hashCode method of class C must consistently retu rn the same result as long as the fields used for the equals comparison remain u nchanged. a. false b. true -------------------------------------------------------------------------------Question 3 import java.util.*; class B { public static void main (String args[]) { AbstractMap a = new HashMap(); AbstractList b = new ArrayList(); AbstractSet c = new HashSet(); System.out.print((a instanceof Collection)+","); System.out.print((b instanceof Collection)+","); System.out.print(c instanceof Collection); } } What is the result of attempting to compile and run the program? a. b. c. d. e. f. g. h. Prints: Prints: Prints: Prints: Prints: Prints: Prints: Prints: false,false,false false,false,true false,true,false false,true,true true,false,false true,false,true true,true,false true,true,true

i. None of the above -------------------------------------------------------------------------------Question 4 import java.util.*; class D { public static void main (String args[]) { AbstractSet a = new HashSet(); System.out.print((a instanceof Set)+","); System.out.print(a instanceof SortedSet); } } What is the result of attempting to compile and run the program? a. b. c. d. e. Prints: Prints: Prints: Prints: None of false,false false,true true,false true,true the above

-------------------------------------------------------------------------------Question 5 import java.util.*; class I { public static void main (String[] args) { Object i = new ArrayList().iterator(); System.out.print((i instanceof List)+","); System.out.print((i instanceof Iterator)+","); System.out.print(i instanceof ListIterator); } } What is the result of attempting to compile and run the program? a. b. c. d. e. f. g. h. i. Prints: Prints: Prints: Prints: Prints: Prints: Prints: Prints: None of false,false,false false,false,true false,true,false false,true,true true,false,false true,false,true true,true,false true,true,true the above

-------------------------------------------------------------------------------Question 6

If two instances of a class type are equal according to the equals method, then the same integer value must be returned by the hashCode method of the two object s.

a. false b. true -------------------------------------------------------------------------------Question 7

An immutable class C contains a field of type int and a large array of primitive s of type double. You must consider developing a hashCode method based one of th ese three options. Which of the three is most likely to optimize the performance of a Hashtable without violating any of the rules for coding a hashCode method? a. Calculate the hashCode using only b. Calculate the hashCode using both c. Calculate the hashCode using both ulate the hashCode once and store the le. the int field. the int field and the array. the int field and the array, but only calc value for future use in an instance variab

-------------------------------------------------------------------------------Question 8 class A { private int[] val; private int hash; public int hashCode() { int h = hash; if (h == 0) { int off = 0; int len = val.length; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; } // The equals method has been omitted for clarity. A (int[] val) {this.val = val;} public static void main (String[] args) { A a = new A(new int[]{1,2,3}); System.out.print(a.hashCode()); } } What is the result of attempting to compile and run the program? a. b. c. d. e. f. Prints: 1026 Prints: 1091 Prints: 31806 Compiler error Run time error None of the above

--------------------------------------------------------------------------------

Question 9

Which implementation of the List interface provides for the fastest insertion of a new element into the middle of the list? a. b. c. d. Vector ArrayList LinkedList None of the above

-------------------------------------------------------------------------------Question 10

Which of the following classes allow elements to be accessed in the order that t hey were added? a. b. c. d. e. f. g. h. i. j. k. l. ArrayList LinkedHashMap LinkedHashSet LinkedList HashMap HashSet Hashtable TreeMap TreeSet Vector WeakHashMap None of the above

-------------------------------------------------------------------------------Question 11

Which of the following classes allow unsynchronized read operations by multiple threads? a. b. c. d. e. f. g. h. Vector Hashtable TreeMap TreeSet HashMap HashSet WeakHashMap None of the above

-------------------------------------------------------------------------------Question 12 a. Entries are not organized as key/value pairs. b. Duplicate entries are rejected.

c. Entries are sorted using a comparator or the Comparable interface. Which interface of the java.util package offers the specified behavior? a. b. c. d. e. f. List Map Set SortedSet SortedMap None of the above

-------------------------------------------------------------------------------Question 13

If two instances of a class type are not equal according to the equals method, t hen the same integer value must not be returned by the hashCode method of the tw o objects. a. false b. true -------------------------------------------------------------------------------Question 14 import java.util.*; class A { public static void main (String args[]) { AbstractList a = new LinkedList(); AbstractSet b = new TreeSet(); AbstractMap c = new TreeMap(); System.out.print((a instanceof Collection)+","); System.out.print((b instanceof Collection)+","); System.out.print(c instanceof Collection); } } What is the result of attempting to compile and run the program? a. b. c. d. e. f. g. h. i. Prints: Prints: Prints: Prints: Prints: Prints: Prints: Prints: None of false,false,false false,false,true false,true,false false,true,true true,false,false true,false,true true,true,false true,true,true the above

-------------------------------------------------------------------------------Question 15 import java.util.*;

class F { public static void main (String args[]) { LinkedList a = new LinkedList(); ArrayList b = new ArrayList(); Vector c = new Vector(); System.out.print((a instanceof List)+","); System.out.print((b instanceof List)+","); System.out.print(c instanceof List); } } What is the result of attempting to compile and run the program? a. b. c. d. e. f. g. h. i. Prints: Prints: Prints: Prints: Prints: Prints: Prints: Prints: None of false,false,false false,false,true false,true,false false,true,true true,false,false true,false,true true,true,false true,true,true the above

-------------------------------------------------------------------------------Question 16 a. b. c. d. nt Stores key/value pairs. Allows null elements, keys, and values. Duplicate entries replace old entries. The least recently used element can be removed automatically when a new eleme is added.

Which of these classes provides the specified features? a. b. c. d. e. f. g. h. i. LinkedHashMap LinkedHashSet LinkedList TreeMap TreeSet HashMap HashSet Hashtable None of the above

-------------------------------------------------------------------------------Question 17 import java.util.*; class F { public static void main (String[] args) { Object v = new Vector(); System.out.print((v instanceof Collections)+","); System.out.print((v instanceof Arrays)+","); System.out.print(v instanceof List); } }

What is the result of attempting to compile and run the program? a. b. c. d. e. f. g. h. i. Prints: Prints: Prints: Prints: Prints: Prints: Prints: Prints: None of false,false,false false,false,true false,true,false false,true,true true,false,false true,false,true true,true,false true,true,true the above

-------------------------------------------------------------------------------Question 18 a. b. c. d. Stores key/value pairs. Allows null elements, keys, and values. Duplicate entries replace old entries. Entries are not sorted using a comparator or the Comparable interface. e. The iteration order is unspecified. Which of these classes provides the specified features? a. b. c. d. e. f. g. h. i. LinkedList LinkedHashMap LinkedHashSet TreeMap TreeSet HashMap HashSet Hashtable None of the above

--------------------------------------------------------------------------------------------------------------------------------------------------------------Copyright 2002, Dan Chisholm All rights reserved.

Вам также может понравиться