Witscale Test Center

Sample Test


Sample Test

 

1.       Which classes among the following represent an ordered collection of non-unique elements? (Select two correct answers)

 

A.      java.util.Arrays

B.      java.util.List

C.      java.util.HashSet

D.     java.util.ArrayList

E.      java.util.Vector

F.       java.util.Hashtable

 

 

 

2.       Which of the following collection classes can resize dynamically and provides indexed access to its elements? (Select three correct answers)

 

A.      java.util.LinkedList

B.      java.util.List

C.      java.util.HashSet

D.     java.util.ArrayList

E.      java.util.Vector

 

 

3.       Which of the following is the collection class that allows a synchronized access to its elements using a key where a key is associated with an element’s value?

 

A.      java.util.LinkedHashSet

B.      java.util.HashMap

C.      java.util.SortedMap

D.     java.util.TreeMap

E.      java.util.Hashtable

 

 

 

4.       Consider the following code. 

 

1.        HashSet numbers = new HashSet();

2.        numbers.add(11);

3.        numbers.add(6);

4.        numbers.add(2);

5.        numbers.add(6.0f);

6.        numbers.add(6L);

7.        Iterator iterator = numbers.iterator();

8.        while (iterator.hasNext() ) {

9.          System.out.print((int)iterator.next() + " " );

10.     }

 

What will be the result?

 

A.      11 6 2 6 6

B.      11 6 2

C.      11 6 2 6

D.     This code will compile prints nothing.

E.      This code will not compile.

 

5.       Which of the following statements are true about collections? (Select one correct answer)

 

A.      When you are adding a duplicate element in a Set type of collection, an UnsupportedOperationException is thrown.

B.      The collection framework does not offer any ordered collection where you can have unique elements.

C.      The collection framework does not offer any ordered collection where you can store the key-value mappings.

D.     When you are calling optional methods of collection, you must handle the UnsupportedOperationException.

E.      None of the above.

 

 

6.       Which of the following collection classes associate the elements with key values, and also allows you to retrieve objects in the same order in which they were inserted.

 

A.      java.util.ArrayList

B.      java.util.LinkedHashMap

C.      java.util.HashMap

D.     java.util.TreeMap

E.      java.util.LinkedHashSet

F.       java.util.TreeSet

 

 

 

7.       Consider the following code fragment.

 

4.        if (car2.hashCode() == car3.hashCode() )

5.          System.out.println("1 ");

6.        if (!car3.equals(car4) )

7.          System.out.println("2 ");

8.        if (car1.equals(car2) )

9.          System.out.println("3");

 

Assume that the car1, car2, car3 and car4 refer to objects of Car class and the Car class properly implements the equals() and hashCode() methods. If the output of the above code is “1 3”, which of the following statements are guaranteed to be true?

 

A.      car2.equals(car3)

B.      car2.hashCode() != car3.hashCode()

C.      car1.hashCode() == car2.hashCode()

D.     car3.hashCode() == car4.hashCode()

E.      None of the above.

 

 

8.       Consider the following code fragment.

 

4.        if (car1.equals(car2) )

5.          System.out.println("1 ");

6.        if (car2.equals(car3) )

7.          System.out.println("2 ");

8.        if (car3.equals(car4))

9.          System.out.println("3");

 

Assume that the car1, car2, car3 and car4 refer to objects of Car class and the Car class properly implements the equals() and hashCode() methods. If the output of the above code is “1 2 3”, which of the following statements are guaranteed to be true?

 

A.      car1.equals(car3)

B.      car1.equals(car4)

C.      car2.equals(car1)

D.     car1.hashCode() != car3.hashCode()

E.      car2.hashCode() != car4.hashCode()

 

 

9.       Consider the following code fragment.

 

4.        class Flower1 {

5.          public String color;

6.          public int hashCode() { return 5; }

7.          // Other methods...

8.        }

9.        class Flower2 {

10.      public String color;

11.      public int hashcode() { return color.length(); }

12.      // Other methods...

13.     }

 

Which of the following statements is true when the objects are to be stored in a collection that uses hashing?

 

A.     The hashCode() method in class Flower1 is better (more efficient) than the hashCode() method in Flower2.

B.     The hashCode() method in class Flower1 is less efficient than the hashCode() method in Flower2.

C.     The hashcode() methods in Flower1 and Flower2 are equally efficient.

D.     The class Flower1 will not compile.

E.      The class Flower2 will not compile.

 

 

10.    Which of the following statements are true about two instances of a class that overrides the equals() and hashCode() methods properly? (Choose two correct answers)

 

A.     If the equals() method returns true for the two instances, the hashCode() must return the same code for both the instances.

B.     If the hashCode() method returns same code for the two instances, the equals() method might return true for those two instances.

C.     If the equals() method returns false for the two instances, the hashCode() must return the different code for both the instances.

D.     If the hashCode() method returns same code for two instances, the equals() method must return true for those two instances.

 

 

11.    Which of the following statements is true about the hashCode() and equals() methods?

 

A.      You need not override hashCode() if you are overriding equals() method.

B.      You need not override equals() if you are overriding hashCode() method.

C.      You must override hashCode() such that it always return the same number no matter what object invoked it.

D.     You must override hashCode() such that it always return a different number no matter what object invoked it.

E.      You can invoke equals() method on an object of say TypeA by sending an object of TypeB.

 

 

 

12.    Which of the following classes override the equals() and the hashCode() methods ? (Choose all the correct answers).

 

A.      String

B.      StringBuffer

C.      Integer

D.     Character

E.      Float