Tuesday 24 July 2012

Objects and Collections - Objective Questions - Part 1

1)

Which interface provides the capability to store objects using a key-value pair?

A. java.util.Set
B. java.util.Map
C. java.util.List
D. java.util.Collection

Answer: B

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

2)

What will be the output of the program?

import java.util.*;
class H
{
    public static void main (String[] args)
    {
        Object x = new Vector().elements();
        System.out.print((x instanceof Enumeration)+",");
        System.out.print((x instanceof Iterator)+",");
        System.out.print(x instanceof ListIterator);
    }
}

A. Prints: false,false,false
B. Prints: false,false,true
C. Prints: false,true,false
D. Prints: true,false,false

Answer: D

The Vector.elements method returns an Enumeration over the elements of the vector. Vector implements the List interface and extends AbstractList so it is also possible to get an Iterator over a Vector by invoking the iterator  or listIterator method.

3)

class Test1
{
    public int value;
    public int hashCode() { return 42; }
}
class Test2
{
    public int value;
    public int hashcode() { return (int)(value^5); }
}
which statement is true?

A. class Test1 will not compile.
B. The Test1 hashCode() method is more efficient than the Test2 hashCode() method.
C. The Test1 hashCode() method is less efficient than the Test2 hashCode() method.
D. class Test2 will not compile.

Answer: C

The so-called "hashing algorithm" implemented by class Test1 will always return the same value, 42, which is legal but which will place all of the hash table entries into a single bucket, the most inefficient setup possible.
Option A and D are incorrect because these classes are legal.
Option B is incorrect based on the logic described above.

4)

Which of the following statements about the hashcode() method are incorrect?

   1. The value returned by hashcode() is used in some collection classes to help locate
      objects.
   2. The hashcode() method is required to return a positive int value.
   3. The hashcode() method in the String class is the one inherited from Object.
   4. Two new empty String objects will produce identical hashcodes.

A. 1 and 2
B. 2 and 3
C. 3 and 4
D. 1 and 4

Answer: B

(2) is an incorrect statement because there is no such requirement.
(3) is an incorrect statement and therefore a correct answer because the hashcode for a string is computed from the characters in the string.

5)

Which class does not override the equals() and hashCode() methods,
inheriting them directly from class Object?

A. java.lang.String
B. java.lang.Double
C. java.lang.StringBuffer
D. java.lang.Character

Answer: C

java.lang.StringBuffer is the only class in the list that uses the default methods provided by class Object.

6)

What will be the output of the program?

public static void main(String[] args)
{
    Object obj = new Object()
    {
        public int hashCode()
        {
            return 42;
        }
    };
    System.out.println(obj.hashCode());
}

A. 42
B. Runtime Exception
C. Compile Error at line 2
D. Compile Error at line 5

Answer: A

This code is an example of an anonymous inner class. They can be declared to extend another class or implement a single interface. Since they have no name you can not use the "new" keyword on them. In this case the annoynous class is extending the Object class. Within the {}  you place the methods you want for that class.After this class has been declared its methods can be used by that object in the usual way e.g. objectname.annoymousClassMethod()

7)

Which collection class allows you to access its elements by associating a key with an
element's value, and provides synchronization?

A. java.util.SortedMap
B. java.util.TreeMap
C. java.util.TreeSet
D. java.util.Hashtable

Answer: D

Hashtable is the only class listed that provides synchronized methods. If you need synchronization great;otherwise, use HashMap, it's faster.

8)

x = 0;
if (x1.hashCode() != x2.hashCode() )  x = x + 1;
if (x3.equals(x4) )  x = x + 10;
if (!x5.equals(x6) ) x = x + 100;
if (x7.hashCode() == x8.hashCode() )  x = x + 1000;
System.out.println("x = " + x);

and assuming that the equals() and hashCode() methods are property implemented, if the
output is "x = 1111", which of the following statements will always be true?

A. x2.equals(x1)
B. x3.hashCode() == x4.hashCode()
C. x5.hashCode() != x6.hashCode()
D. x8.equals(x7)

Answer: B

By contract, if two objects are equivalent according to the equals() method, then the hashCode()  method must evaluate them to be ==.Option A is incorrect because if the hashCode() values are not equal, the two objects must not be equal.Option C is incorrect because if equals() is not true there is no guarantee of any result from hashCode().Option D is incorrect because hashCode() will often return == even if the two objects do not evaluate to equals() being true.

9)

Which collection class allows you to associate its elements with key values, and allows you
to retrieve objects in FIFO (first-in, first-out) sequence?

A. java.util.HashMap
B. java.util.Hashtable
C. java.util.TreeMap
D. java.util.LinkedHashMap

Answer: D

LinkedHashMap is the collection class used for caching purposes. FIFO is another way to indicate caching
behavior. To retrieve LinkedHashMap elements in cached order, use the values() method and iterate over the
resultant collection.

10)

Which of the following are true statements?

   1. The Iterator interface declares only three methods: hasNext, next and remove.
   2. The ListIterator interface extends both the List and Iterator interfaces.
   3. The ListIterator interface provides forward and backward iteration capabilities.
   4. The ListIterator interface provides the ability to modify the List during iteration.
   5. The ListIterator interface provides the ability to determine its position in the List.

A. 2, 3, 4 and 5
B. 1, 3, 4 and 5
C. 3, 4 and 5
D. 1, 2 and 3

Answer: B

The ListIterator interface extends the Iterator interface and declares additional methods to provide forward and backward iteration capabilities, List modification capabilities, and the ability to determine the position of the iterator in the List.

No comments:

Post a Comment