You are on page 1of 12

Collection & Maps

Question 1
  • Elements are not key/value pairs.
  • Contains no duplicate elements.
  • The entries can be sorted using the Comparable interface.

Which of these classes provides the specified features?

a.  LinkedList
b.  TreeMap
c.  TreeSet
d.  HashMap
e.  HashSet
f.  Hashtable
g.  None of the above

ANSWER
The elements are not key/value pairs; so a Map is not a good choice.
A List generally accepts duplicate elements. A Set stores a collection
1 c  TreeSet  of unique objects; so any attempt to store a duplicate object is
rejected. TreeSet stores elements in an order that is determined either
by a Comparator or by the Comparable interface.  

Question 2
import java.util.*;
class GFC106 {
public static void main (String args[]) {
Object a = new HashSet(), b = new HashMap();
Object c = new Hashtable();
System.out.print((a instanceof Map)+",");
System.out.print((b instanceof Map)+",");
System.out.print(c instanceof Map);
}}

What is the result of attempting to compile and run the program?

a.  Prints: false,false,false


b.  Prints: false,false,true
c.  Prints: false,true,false
d.  Prints: false,true,true
e.  Prints: true,false,false
f.  Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.  None of the above

ANSWER
HashSet implements the Set interface, but not the Map interface.
Prints: HashMap extends AbstractMap and implements the Map
2 d 
false,true,true  interface. Hashtable extends Dictionary and implements the
Map interface.

Question 3
class A {
int i1, i2;
public void setI1(int i) {i1 = i;}
public int getI1() {return i1;}
public void setI2(int i) {i2 = i;}
public int getI2() {return i2;}
public A(int ii1, int ii2) {i1 = ii1; i2 = ii2;}
public boolean equals(Object obj) {
if (obj instanceof A) {
return (i1 == ((A)obj).getI1());
}
return false;
}
public int hashCode() {
// Insert statement here.
}}

Which of the following statements could be inserted at the specified location without
violating the hash code contract?

a.  return 31;


b.  return getI1();
c.  return getI2();
d.  return 31 * getI1() + getI2();

ANSWER
3 a  return 31;  return A hashCode method that returns the constant value 31 is
b  getI1();  consistent with the hash code contract. Even so, a hashCode
method that returns the same value regardless of the internal
state of the object is not very good, because it will cause
hashtables to place every instance of the class in the same
bucket. If the equals method determines that two instances are
equal, then the two instances must produce the same hash
code. For that reason, the hash code must not be calculated
using fields that are not used to determine equality. In this
case, the equals method determines equality based on the value
of i1. The value of i2 is not used to determine equality;
therefore, i2 can not be used to calculate the hash code.  

Question 4
  • Stores key/value pairs.
  • Allows null elements, keys, and values.
  • Duplicate entries replace old entries.
  • Entries are not sorted.

Which of these classes provides the specified features?

a.  LinkedList
b.  TreeMap
c.  TreeSet
d.  HashMap
e.  HashSet
f.  Hashtable
g.  None of the above

ANSWER
The requirement to store key/value pairs is directly satisfied by a
concrete implementation of the Map interface. The List and Set
interfaces recognize objects, but do not recognize keys and values.
4 d  HashMap 
The requirement to allow null elements is not satisfied by a
Hashtable. TreeMap and TreeSet store elements in a sorted order
based on the key.  

Question 5
import java.util.*;
class GFC107 {
public static void main (String args[]) {
Object a = new HashSet(), b = new HashMap();
Object c = new Hashtable();
System.out.print((a instanceof Cloneable)+",");
System.out.print((b instanceof Cloneable)+",");
System.out.print(c instanceof Cloneable);
}}

What is the result of attempting to compile and run the program?

a.  Prints: false,false,false


b.  Prints: false,false,true
c.  Prints: false,true,false
d.  Prints: false,true,true
e.  Prints: true,false,false
f.  Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.  None of the above

ANSWER
5 h  Prints: true,true,true  All three implement Cloneable.  

Question 6
class A {
int i1, i2;
public void setI1(int i) {i1 = i;}
public int getI1() {return i1;}
public void setI2(int i) {i2 = i;}
public int getI2() {return i2;}
public A(int ii1, int ii2) {i1 = ii1; i2 = ii2;}
public boolean equals(Object obj) {
if (obj instanceof A) {
return (i1 == ((A)obj).getI1()) & (i2 == ((A)obj).getI2());
}
return false;
}
public int hashCode() {
// Insert statement here.
}}

If inserted at the specified location, which of the following statements would produce the
most efficient hashCode method?

a.  return 31;


b.  return getI1();
c.  return getI2();
d.  return getI1() + getI2();
e.  return 31 * getI1() + getI2();
f.  None of the above

ANSWER
All of the statements would produce a hashCode method that
is consistent with the hash code contract. The expression 31 *
getI1() + getI2() produces the most efficient hashCode
return 31 * getI1()
6 e  method, because it is most likely to produce unique hashcodes
+ getI2(); 
for various combinations of i1 and i2. The expression getI1() +
getI2() is less efficient, because it produces the same hash
code when the values of i1 and i2 are swapped.  

Question 7
  • Entries are not organized as key/value pairs.
  • Generally accepts duplicate elements.
  • Entries may be accessed by means of an index.

Which interface of the java.util package offers the specified behavior?

a.  List
b.  Map
c.  Set
d.  None of the above

ANSWER
The Map interface organizes entries as key/value pairs. A list generally
7 a  List  allows duplicate entries. A Set rejects duplicate entries. A List allows
entries to be accessed using an index.  

Question 8
import java.util.*;
import java.io.Serializable;
class GFC108 {
public static void main (String args[]) {
HashMap a = new HashMap();
boolean b1, b2, b3;
b1 = (a instanceof Cloneable) & (a instanceof Serializable);
b2 = a instanceof Map;
b3 = a instanceof Collection;
System.out.print(b1 + "," + b2 + "," + b3);
}}
What is the result of attempting to compile and run the program?

a.  Prints: false,false,false


b.  Prints: false,false,true
c.  Prints: false,true,false
d.  Prints: false,true,true
e.  Prints: true,false,false
f.  Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.  None of the above

ANSWER
8 g  Prints: true,true,false  HashMap does not implement the Collection interface.  

Question 9
Which of the following classes allow unsynchronized read operations by multiple
threads?

a.  Vector
b.  Hashtable
c.  TreeMap
d.  TreeSet
e.  HashMap
f.  HashSet

ANSWER
The Vector and Hashtable methods are synchronized and do
not allow for simultaneous access by multiple threads. The
concrete subclasses of the AbstractList, AbstractMap, and
c  TreeMap  AbstractSet classes allow for unsynchronized read operations
d  TreeSet  by multiple threads. Additionally, the sychronized wrapper
9
e  HashMap  methods of the Collections class allow for the instantiation of a
f  HashSet  Collection, List, Map, Set, SortedMap, or SortedSet with
synchronized methods. If simultaneous read and write
operations are necessary then a synchronized instance should
be used.  

Question 10
  • Entries are organized as key/value pairs.
  • Duplicate entries replace old entries.
  • Entries are sorted using a Comparator or the Comparable interface.

Which interface of the java.util package offers the specified behavior?

a.  List
b.  Map
c.  Set
d.  SortedSet
e.  SortedMap
f.  None of the above

ANSWER
The List and Set interfaces do not support key/value pairs. A list
generally allows duplicate entries. A Set rejects duplicate entries.
10 e  SortedMap  A Map organizes the entries as key/value pairs. The SortedMap
is similar to a Map except that the ordering of the elements is
determined by a Comparator or the Comparable interface.  

Question 11
import java.util.*;
class GFC110 {
public static void main (String[] args) {
Object m = new LinkedHashMap();
System.out.print((m instanceof Collection)+",");
System.out.print((m instanceof Map)+",");
System.out.print(m instanceof List);
}}

What is the result of attempting to compile and run the program?

a.  Prints: false,false,false


b.  Prints: false,false,true
c.  Prints: false,true,false
d.  Prints: false,true,true
e.  Prints: true,false,false
f.  Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.  None of the above

ANSWER
Prints: LinkedHashMap does not implement the Collection
11 c 
false,true,false  interface or the List interface.  

Question 12
  • Entries are not organized as key/value pairs.
  • Duplicate entries are rejected.
  • Entries are sorted using a Comparator or the Comparable interface.

Which interface of the java.util package offers the specified behavior?

a.  List
b.  Map
c.  Set
d.  SortedSet
e.  SortedMap
f.  None of the above

ANSWER
The Map interface organizes entries as key/value pairs. A list
generally allows duplicate entries. A Set rejects duplicate entries.
12 d  SortedSet  The SortedSet is similar to a Set except that the ordering of the
elements is determined by a Comparator or the Comparable
interface.  

Question 13
import java.util.*;
class GFC111 {
public static void main (String[] args) {
Object m = new LinkedHashMap();
System.out.print((m instanceof Collection)+",");
System.out.print((m instanceof Map)+",");
System.out.print(m instanceof HashMap);
}}

What is the result of attempting to compile and run the program?

a.  Prints: false,false,false


b.  Prints: false,false,true
c.  Prints: false,true,false
d.  Prints: false,true,true
e.  Prints: true,false,false
f.  Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.  None of the above

ANSWER
LinkedHashMap does not implement the Collection interface.
Prints:
13 d  LinkedHashMap extends HashMap and implements Map,
false,true,true 
Cloneable and Serializable.  

Question 14
import java.util.*;
class GFC112 {
public static void main (String[] args) {
Object m = new LinkedHashSet();
System.out.print((m instanceof Collection)+",");
System.out.print((m instanceof Set)+",");
System.out.print(m instanceof List);
}}

What is the result of attempting to compile and run the program?

a.  Prints: false,false,false


b.  Prints: false,false,true
c.  Prints: false,true,false
d.  Prints: false,true,true
e.  Prints: true,false,false
f.  Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.  None of the above

ANSWER
LinkedHashSet does not implement the List interface.
Prints:
14 g  LinkedHashSet extends HashSet and implements Collection,
true,true,false 
Set, Cloneable and Serializable.

Question 15
import java.util.*;
class GFC109 {
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.  Prints: false,false,false


b.  Prints: false,false,true
c.  Prints: false,true,false
d.  Prints: false,true,true
e.  Prints: true,false,false
f.  Prints: true,false,true
g.  Prints: true,true,false
h.  Prints: true,true,true
i.  None of the above

ANSWER
The Collections class is not the same as the Collection
interface. The Collections class contains a variety of methods
Prints: used to work with collections. For example,
15 b 
false,false,true  Collections.shuffle is used to randomly shuffle the elements of
a Collection. Similarly, the Arrays class provides utility
methods for working with arrays.  

Question 16
  • 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.
  • The iteration order is unspecified.

Which of these classes provides the specified features?

a.  LinkedList
b.  LinkedHashMap
c.  LinkedHashSet
d.  TreeMap
e.  TreeSet
f.  HashMap
g.  HashSet
h.  Hashtable
i.  None of the above

ANSWER
The requirement to store key/value pairs is directly satisfied by a
concrete implementation of the Map interface. The List and Set
interfaces recognize objects, but do not recognize keys and values.
The requirement to allow null elements is not satisfied by a
Hashtable. TreeMap and TreeSet store elements in a sorted order
16 f  HashMap  based on the key. The iteration order of LinkedHashMap and
LinkedHashSet is not unspecified. By default, the iteration order of
LinkedHashMap and LinkedHashSet is based on the order in which
elements were inserted. Optionally, the iteration order of the
LinkedHashMap can be set to the order in which the elements were
last accessed.  

Question 17
  • Stores key/value pairs.
  • Does not allow null elements, keys, and values.

Which of these classes provides the specified features?

a.  LinkedList
b.  LinkedHashMap
c.  LinkedHashSet
d.  TreeMap
e.  TreeSet
f.  HashMap
g.  HashSet
h.  Hashtable
i.  None of the above

ANSWER
The requirement to store key/value pairs is directly satisfied by a
Hashtable or any concrete implementation of the Map interface.
The List and Set interfaces recognize objects, but do not
17 h  Hashtable  recognize keys and values. The requirement to NOT allow null
elements is satisfied by Hashtable, but not by HashMap or any of
the other Collection implementations that were introduced with
Java 1.2 and later.  

You might also like