Certified Java Programmer Mock Exam
Question 1
class MCZ24 {
public static void main (String[] args) {
char a = 061; // 1
char b = '\61'; // 2
char c = '\061'; // 3
char d = 0x0031; // 4
char e = '\u0031'; // 5
System.out.print(""+a+b+c+d+e);
}}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | None of the above |
Question 2
class MWC114 {
public static void main(String[] s) {
String s1 = new String("ABCDEFG"), s2 = new String("EFGHIJ");
String s3 = s1.substring(4,7), s4 = s2.substring(0,3);
System.out.println((s3 == s4) + "," + (s3 + s4).equals(s4 + s3));
}}
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. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
Question 3
class SRC117 {
public static void main (String[] args) {
double d1 = Math.random();
boolean b1 = (d1 < 0.0), b2 = (d1 <= 0.0), b3 = (d1 == 0.0);
boolean b4 = (d1 >= 0.0), b5 = (d1 < 1.0), b6 = (d1 <= 1.0);
boolean b7 = (d1 == 1.0), b8 = (d1 >= 1.0), b9 = (d1 > 1.0);
}}
Which of the boolean variables will never be initialized to the value
true?
| a. | b1 |
| b. | b2 |
| c. | b3 |
| d. | b4 |
| e. | b5 |
| f. | b6 |
| g. | b7 |
| h. | b8 |
| i. | b9 |
Question 4
class RedException extends Exception {}
class BlueException extends Exception {}
class White {
void m1() throws RedException {throw new RedException();}
public static void main (String[] args) {
White white = new White();
int a,b,c,d; a = b = c = d = 0;
try {white.m1(); a++;}
catch (RedException e) {b++;}
catch (BlueException e) {c++;}
finally {d++;}
System.out.print(a+","+b+","+c+","+d);
}}
What is the result of attempting to compile and run the
program?
| a. | Prints: 0,1,0,0 |
| b. | Prints: 1,1,0,1 |
| c. | Prints: 0,1,0,1 |
| d. | Prints: 0,1,1,1 |
| e. | Prints: 1,1,1,1 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
Question 5
Which of the following are command-line switches used to
disable assertions in non-system classes?
| a. | -da |
| b. | -ad |
| c. | -aon |
| d. | -aoff |
| e. | -disableassertions |
| f. | -assertionsdisable |
| g. | -assertionson |
| h. | -assertionsoff |
Question 6
interface A {void m1();} // 1
class B implements A {public void m1() {}} // 2
class C implements A {protected void m1() {}} // 3
class D implements A {private void m1() {}} // 4
class E implements A {void m1() {}} // 5
Compile-time errors are generated at which lines?
Question 7
class MCZ23 {
public static void main (String[] args) {
// Insert code here.
}}
Which of the following lines can be inserted at the
specified location without generating a
compile-time error?
| a. | boolean b1 = true; |
| b. | boolean b2 = TRUE; |
| c. | boolean b3 = 'true'; |
| d. | boolean b4 = "TRUE"; |
| e. | boolean b5 = 0; |
| f. | None of the above |
Question 8
class MWC115 {
public static void main(String[] s) {
String s1 = new String("ABCDEFG"), s2 = s1.substring(0,3);
String s3 = s1.substring(4,6); char c1 = s1.charAt(3);
System.out.println(s2.concat(String.valueOf(c1)).concat(s3));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: "ABCDEFG" |
| b. | Prints: "ABCEFG" |
| c. | Prints: "ABCDDEFG" |
| d. | Prints: "ABCDEF" |
| e. | Prints: "ABCEF" |
| f. | Prints: "ABCDDEF" |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
Question 9
Which method of the java.lang.Math
class is not static?
| a. | abs |
| b. | ceil |
| c. | floor |
| d. | max |
| e. | min |
| f. | random |
| g. | round |
| h. | sin |
| i. | cos |
| j. | tan |
| k. | sqrt |
| l. | None of the above |
Question 10
import java.util.*;
class GFC116 {
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);
}}
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 11
Which of the following are command-line switches used to
disable assertions in non-system classes?
| a. | -disableassertions |
| b. | -disableAssertions |
| c. | -assertionsDisable |
| d. | -assertionsOn |
| e. | -assertionsOff |
| f. | None of the above |
Question 12
class EBH025 {
public static void main (String args[]) {
int i1 = 0xffffffff, i2 = i1 << 33;
int i3 = i1 << (33 & 0x1f);
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3));
}}
What is the result of attempting to compile and run the
program?
| a. | Prints: 0,0 |
| b. | Prints: 0,fffffffe |
| c. | Prints: 0,ffffffff |
| d. | Prints: ffffffff,ffffffff |
| e. | Prints: ffffffff,fffffffe |
| f. | Prints: fffffffe,ffffffff |
| g. | Prints: fffffffe,fffffffe |
| h. | Run-time error |
| i. | Compile-time error |
| j. | None of the above |
Question 13
Which of the following are true statements?
| a. | The relationship between a class and its superclass is an example of a "has-a" relationship. |
| b. | The relationship between a class and its superclass is an example of an "is-a" relationship. |
| c. | The relationship between a class and an object referenced by a field within the class is an example of a "has-a" relationship. |
| d. | The relationship between a class and an object referenced by a field within the class is an example of an "is-a" relationship. |
Question 14
Some of the following method names are overloaded and some
are not. Which of the following method names are overloaded
such that for each of the following four primitive types
int,
long,
float
and
double
there is at least one corresponding method that returns that type?
| a. | abs |
| b. | ceil |
| c. | floor |
| d. | max |
| e. | min |
| f. | random |
| g. | round |
| h. | sin |
| i. | cos |
| j. | tan |
| k. | sqrt |
Question 15
import java.util.*;
class GFC117 {
public static void main (String[] args) {
Object i1 = new HashMap(), i2 = new TreeMap();
System.out.print((i1 instanceof SortedMap)+",");
System.out.print((i2 instanceof SortedMap)+",");
System.out.print(i1 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 16
When a thread is created and started, what is its initial state?
| a. | New |
| b. | Ready |
| c. | Not-Runnable |
| d. | Runnning |
| e. | Dead |
| f. | None of the above |
Question 17
| | • | Stores key/value pairs. |
| | • | Duplicate entries replace old entries. |
| | • | Provides constant-time performance for the add, contains and remove operations. |
Which of these classes provides the specified features?
| a. | LinkedHashMap |
| b. | LinkedHashSet |
| c. | LinkedList |
| d. | TreeMap |
| e. | TreeSet |
| f. | HashMap |
| g. | HashSet |
| h. | Hashtable |
Question 18
import java.util.*;
class GFC114 {
public static void main (String[] args) {
Object a = new ArrayList();
System.out.print((a instanceof Collections)+",");
System.out.print((a instanceof Arrays)+",");
System.out.print(a 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 19
Which of the following are true statements?
| a. | The Thread.run method is used to start a new thread running |
| b. | The Thread.start method causes a new thread to get ready to run at the discretion of the thread scheduler |
| c. | The Runnable interface declares the start method |
| d. | The Runnable interface declares the run method |
| e. | The Thread class implements the Runnable interface |
| f. | If an Object.notify method call appears in a synchronized block, then it must be the last method call in the block |
| g. | No restriction is placed on the number of threads that can enter a synchronized method |
| h. | Some implementations of the Thread.yield method will not yield to a thread of lower priority |
Question 20
| | • | Each element must be unique. |
| | • | Duplicate elements must not replace old elements. |
| | • | Elements are not key/value pairs. |
| | • | Entries are not sorted using a Comparator or the Comparable interface. |
| | • | The iteration order is determined by the insertion order. |
Which of these classes provides the specified features?
| a. | HashMap |
| b. | HashSet |
| c. | Hashtable |
| d. | LinkedHashMap |
| e. | LinkedHashSet |
| f. | LinkedList |
| g. | TreeMap |
| h. | TreeSet |
| i. | None of the above |
Question 21
interface Z {void m1();} // 1
class A implements Z {void m1() {}} // 2
class B implements Z {public void m1() {}} // 3
abstract class C implements Z {public abstract void m1();} // 4
A Compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
Question 22
Which of the following are true statements?
| a. |
Thread.MAX_PRIORITY == 10 |
| b. |
Thread.MAX_PRIORITY == 5 |
| c. |
Thread.NORM_PRIORITY == 5 |
| d. |
Thread.NORM_PRIORITY == 3 |
| e. |
Thread.NORM_PRIORITY == 0 |
| f. |
Thread.MIN_PRIORITY == 1 |
| g. |
Thread.MIN_PRIORITY == 0 |
| h. |
Thread.MIN_PRIORITY == -5 |
| i. |
Thread.MIN_PRIORITY == -10 |
Question 23
class MWC116 {
public static void main(String[] args) {
String s1 = " B C D E ";
System.out.print('A' + s1.trim() + 'F');
}}
What is the result of attempting to compile and run the program?
| a. | Prints: ABCDEF |
| b. | Prints: A B C D E F |
| c. | Prints: A BCDEF |
| d. | Prints: ABCDE F |
| e. | Prints: AB C D EF |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
Question 24
Suppose that you would like to create a new instance of a
class that implements the
Set
interface, and you would like
the new instance to be initialized with the elements
of an existing
Set.
If you would like the iteration order of the new
Set
to be
the same as that of the existing
Set,
then which
concrete implementation of the
Set
interface should be
used for the new instance?
| a. | Hashtable |
| b. | HashMap |
| c. | HashSet |
| d. | LinkedHashSet |
| e. | TreeMap |
| f. | TreeSet |
| g. | None of the above. |
Question 25
class JMM115 {
static int m1(String s, int i) {
System.out.print(s + i);
return i;
}
public static void main (String[] args) {
int i = 0, j = 0, k = 0;
do while (m1("i", ++i) < 2)
System.out.print("k" + ++k);
while (m1("j", ++j) < 2);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: i1k1i2k2j1i3j2 |
| b. | Prints: i1k1i2k2j1i1k1i2k2j2 |
| c. | Prints: i1k1i2j1i3j2 |
| d. | Run-time error |
| e. | Compile-time error |
| f. | None of the above |
Question 26
class EBH024 {
public static void main(String[] args) {
int i1 = 15;
String b1 = (i1>30)?"Red":(i1>20)?"Green":(i1>10)?"Blue":"Violet";
String b2 = (i1>30)?"Red":((i1>20)?"Green":((i1>10)?"Blue":"Violet"));
System.out.println(b1 + "," + b2);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: Red,Red |
| b. | Prints: Green,Green |
| c. | Prints: Blue,Blue |
| d. | Prints: Violet,Violet |
| e. | Prints: Blue,Violet |
| f. | Prints: Violet,Blue |
| g. | Prints: Blue,Green |
| h. | Prints: Green,Blue |
| i. | Run-time error |
| j. | Compile-time error |
| k. | None of the above |
Question 27
class A {public void m1() {System.out.print("A1");}}
class B extends A {
public void m1() {System.out.print("B1");}
public void m2() {System.out.print("B2");}
}
class C {
public static void main(String[] args) {
A a1 = new B();
a1.m1(); // 1
a1.m2(); // 2
((B)a1).m1(); // 3
((B)a1).m2(); // 4
}}
What is the result of attempting to compile and run the program?
| a. | Prints: A1B2B1B2 |
| b. | Prints: B1B2B1B2 |
| c. | Compile-time error at 1 |
| d. | Compile-time error at 2 |
| e. | Compile-time error at 3 |
| f. | Compile-time error at 4 |
| g. | Run-time error |
| h. | None of the above |
Question 28
class JMM116 {
static int m1(String s, int i) {
System.out.print(s + i);
return i;
}
public static void main (String[] args) {
int j = 0;
for (int i = m1("A",0); m1("B",i) < 2; m1("C",++i)) {
m1("J",++j);
}
}}
What is the result of attempting to compile and run the program?
| a. | Prints: A0B0C1J1B1C2J2B2 |
| b. | Prints: A0B0J1C1B1J2C2B2 |
| c. | Prints: A0B0J1C1A1B1J2C2A2B2 |
| d. | Run-time error |
| e. | Compile-time error |
| f. | None of the above |
Copyright © 2002-2003, Dan Chisholm
All rights reserved.