Please Help Save a Tree!
The pages of this web site are not formatted to
conserve paper, but
my new book
(ISBN: 0-9745862-0-X)
is formatted to save paper,
save your printer toner,
and save money.
If you prefer to work my exams from printed pages,
then you can save a tree,
save your printer toner,
and save money if you buy
my new book
instead of attempting to print the pages of my web site.
Today, you can find
my book
on the
retail web site
of the company that prints it and distributes it.
Question 1
Which implementation of the
List
interface produces the
slowest access to an element in the middle
of the list by means of an index?
| a. | Vector |
| b. | ArrayList |
| c. | LinkedList |
| d. | None of the above |
Question 2
import java.util.*;
class GFC100 {
public static void main (String args[]) {
Object a1 = new LinkedList(), b1 = new TreeSet();
Object c1 = new TreeMap();
System.out.print((a1 instanceof Collection)+",");
System.out.print((b1 instanceof Collection)+",");
System.out.print(c1 instanceof Collection);
}}
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 |
Question 3
Suppose that an instance of class
C
has legal implementations of the
hashCode
and
equals
methods.
Within any one execution of the Java application, the
hash code
contract requires that each invocation of the
hashCode
method
on the same instance of class
C
must consistently return the same result as long as the
fields used for the
equals
comparison remain unchanged.
Question 4
| | • | Each element must be unique. |
| | • | Contains no duplicate elements. |
| | • | Elements are not key/value pairs. |
| | • | Accessing an element can be almost as fast as performing a similar operation on an array. |
Which of these classes provides the specified features?
| a. | LinkedList |
| b. | TreeMap |
| c. | TreeSet |
| d. | HashMap |
| e. | HashSet |
| f. | LinkedHashMap |
| g. | Hashtable |
| h. | None of the above |
Question 5
import java.util.*;
class GFC101 {
public static void main (String args[]) {
Object a1 = new HashMap(), b1 = new ArrayList();
Object c1 = new HashSet();
System.out.print((a1 instanceof Collection)+",");
System.out.print((b1 instanceof Collection)+",");
System.out.print(c1 instanceof Collection);
}}
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 |
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 objects.
Question 7
| | • | Entries are organized as key/value pairs. |
| | • | Duplicate entries replace old entries. |
Which interface of the java.util package
offers the specified behavior?
| a. | List |
| b. | Map |
| c. | Set |
| d. | None of the above |
Question 8
import java.util.*;
class GFC102 {
public static void main (String args[]) {
Object 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. | Prints: false,false |
| b. | Prints: false,true |
| c. | Prints: true,false |
| d. | Prints: true,true |
| e. | None of the above |
Question 9
If two instances of a class type
are not equal according to the
equals
method,
then the same integer value must not be returned by the
hashCode
method of the two objects.
Question 10
Which implementation of the List interface provides for the
fastest insertion of a new element into the middle of the list?
| a. | Vector |
| b. | ArrayList |
| c. | LinkedList |
| d. | None of the above |
Question 11
import java.util.*;
class GFC103 {
public static void main (String args[]) {
Object a1 = new TreeSet();
System.out.print((a1 instanceof Set)+",");
System.out.print(a1 instanceof SortedSet);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: false,false |
| b. | Prints: false,true |
| c. | Prints: true,false |
| d. | Prints: true,true |
| e. | None of the above |
Question 12
class A {
static void m1 (B a, B b, B c, B d, B e, B f, B g, B h) {
if (a.equals(b)) {System.out.print("A");}
if (!c.equals(d)) {System.out.print("B");}
if (e.hashCode() == f.hashCode()) {System.out.print("C");}
if (g.hashCode() != h.hashCode()) {System.out.print("D");}
}}
Suppose that method
m1
is invoked with eight instances
of the same class and the output is
ABCD.
If the
B.equals
and
B.hashCode
methods are implemented
according to the
hash code
contract, then which of the following
statements must always be true?
| a. | (a.hashCode() == b.hashCode()) |
| b. | (c.hashCode() != d.hashCode()) |
| c. | (e.equals(f)) |
| d. | (!g.equals(h)) |
Question 13
| | • | Stores key/value pairs. |
| | • | Duplicate entries replace old entries. |
| | • | Entries are sorted using a Comparator or 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 |
Question 14
import java.util.*;
class GFC104 {
public static void main (String args[]) {
LinkedList a1 = new LinkedList();
ArrayList b1 = new ArrayList();
Vector c1 = new Vector();
System.out.print((a1 instanceof List)+",");
System.out.print((b1 instanceof List)+",");
System.out.print(c1 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 |
Question 15
class B {
private int i1;
public int hashCode() {return 1;}
}
class C {
private int i1;
public int hashCode() {return -1;}
}
class D {
private int i1;
public int hashCode() {return i1;}
}
Suppose that the
equals
method of classes
B,
C
and
D
all make use of the value of the
int
variable,
i1.
Which class has a
hashCode
method that is not consistent with the hash code contract?
| a. | B |
| b. | C |
| c. | D |
| d. | None of the above |
Question 16
| | • | Entries are not organized as key/value pairs. |
| | • | Duplicate entries are rejected. |
Which interface of the java.util package
offers the specified behavior?
| a. | List |
| b. | Map |
| c. | Set |
| d. | None of the above |
Question 17
import java.util.*;
class GFC105 {
public static void main (String args[]) {
Object a = new HashSet(), b = new HashMap();
Object c = new Hashtable();
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. | 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 |
Question 18
Which of the following classes override both the
equals
and
hashCode
methods?
| a. | java.lang.Byte |
| b. | java.lang.Integer |
| c. | java.util.Vector |
| d. | java.lang.String |
| e. | java.lang.StringBuffer |
Copyright © 2002-2004, Dan Chisholm
All rights reserved.