Certified Java Programmer Mock Exam
Question 1
| | • | Elements are not key/value pairs. |
| | • | Duplicate elements are not accepted. |
| | • | 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 |
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 |
Question 3
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 |
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 |
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 |
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());
}
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(); |
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 |
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 |
Question 9
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 |
Question 10
Which of the following classes allow unsynchronized
read operations by multiple threads?
| a. | Vector |
| b. | Hashtable |
| c. | TreeMap |
| d. | TreeSet |
| e. | HashMap |
| f. | HashSet |
Question 11
| | • | 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 |
Question 12
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 |
Question 13
| | • | 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 |
Question 14
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 |
Question 15
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 |
Question 16
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 |
Question 17
| | • | 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 |
Question 18
| | • | 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 |
Copyright © 2002-2003, Dan Chisholm
All rights reserved.