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 cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.
Today, you can find my book at amazon.com.
If you have any questions or comments concerning my mock exams or my book, then please send an e-mail to me at scjpexam2000@yahoo.com.
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 |
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 |
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.
| a. | false |
| b. | true |
| • | 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 |
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 |
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.
| a. | false |
| b. | true |
| • | 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 |
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 |
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.
| a. | false |
| b. | true |
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 |
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 |
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
| a. | (a.hashCode() == b.hashCode()) |
| b. | (c.hashCode() != d.hashCode()) |
| c. | (e.equals(f)) |
| d. | (!g.equals(h)) |
| • | 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 |
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 |
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 |
| • | 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 |
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 |
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 |