Certified Java Programmer Mock Exam
Question 1
class MCZ25 {
public static void main (String[] args) {
char a = '\7'; // 1
char b = '\61'; // 2
char c = '\062'; // 3
char d = '\x7'; // 4
char e = '\x41'; // 5
System.out.print(""+a+b+c+d+e);
}}
Compile-time errors are generated at which lines?
Question 2
Which of the following are command-line switches used to
enable assertions in system classes?
| a. | -saon |
| b. | -saoff |
| c. | -esa |
| d. | -sae |
| e. | -systemassertionson |
| f. | -systemassertionsoff |
| g. | -enablesystemassertions |
| h. | -systemassertionsenable |
Question 3
class SRC118 {
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.abs(1.0)) + m1(Math.ceil(1.0f)));
System.out.print(m1(Math.max(1.0,1.0)) + m1(Math.round(1.0)));
System.out.print(m1(Math.sin(1.0)) + m1(Math.sqrt(1.0)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: DDDDDD |
| b. | Prints: LLLLLL |
| c. | Prints: DLLLDD |
| d. | Prints: DIDLDD |
| e. | Prints: DLDLDD |
| f. | Prints: DDDLDD |
| g. | Prints: DIDIDD |
| h. | None of the above |
Question 4
import java.util.*;
class GFC118 {
public static void main (String[] args) {
Object i = new ArrayList().listIterator();
System.out.print((i instanceof List)+",");
System.out.print((i instanceof Iterator)+",");
System.out.print(i 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 5
Which of the following statements are true?
| a. | By default assertions are disabled at run-time. |
| b. | Assertions can be selectively enabled for any named class. |
| c. | If assertions are selectively enabled for a named class then assertions are automatically enabled for any subclass of the named class. |
| d. | Assertions can be selectively enabled for any named package. |
| e. | If assertions are selectively enabled for any named package then assertions are automatically enabled for the subpackages. |
| f. | Assertions can be selectively enable for the unnamed package in the current working directory. |
| g. | Assertions can be selectively enable for any unnamed package in any named directory. |
Question 6
interface A {
void m1(); // 1
public void m2(); // 2
protected void m3(); // 3
private void m4(); // 4
abstract void m5(); // 5
}
Compile-time errors are generated at which lines?
Question 7
class MWC118 {
public static void main(String[] args) {
byte[] b = {'a', 'b', 'c'};
char[] c = {'a', 'b', 'c'};
String s = "abc";
StringBuffer sb = new StringBuffer("abc");
byte b2 = 'a';
char c2 = 'a';
String s1 = new String(b); // 1
String s2 = new String(c); // 2
String s3 = new String(s); // 3
String s4 = new String(sb); // 4
String s5 = new String(b2); // 5
String s6 = new String(c2); // 6
}}
Compile-time errors are generated at which lines?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | 6 |
Question 8
class SRC119 {
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.floor(1.0f)) + m1(Math.floor(1.0d)));
System.out.print(m1(Math.ceil(1.0f)) + m1(Math.ceil(1.0d)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: IIII |
| b. | Prints: ILIL |
| c. | Prints: LLLL |
| d. | Prints: FDFD |
| e. | Prints: DDDD |
| f. | None of the above |
Question 9
Which of the following is thrown to indicate that an
assertion has failed?
| a. | AssertError |
| b. | AssertException |
| c. | AssertionError |
| d. | AssertionException |
| e. | None of the above |
Question 10
interface A {
final void m1(); // 1
synchronized void m2(); // 2
native void m3(); // 3
abstract void m4(); // 4
public void m5(); // 5
}
Compile-time errors are generated at which lines?
Question 11
class SRC120 {
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.abs(1.0f)) + m1(Math.abs(1.0d)));
System.out.print(m1(Math.sqrt(1.0f)) + m1(Math.sqrt(1.0d)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: IIII |
| b. | Prints: ILIL |
| c. | Prints: LLLL |
| d. | Prints: FDFD |
| e. | Prints: FDDD |
| f. | Prints: DDFD |
| g. | Prints: DDDD |
| h. | None of the above |
Question 12
Which of the following classes allow elements to
be accessed in the order that they were added?
| a. | HashMap |
| b. | HashSet |
| c. | Hashtable |
| d. | TreeMap |
| e. | TreeSet |
| f. | LinkedHashMap |
| g. | LinkedHashSet |
Question 13
interface A {void main(String[] args);} // 1
interface B {public void main(String[] args);} // 2
interface C {public static void main(String[] args);} // 3
interface D {protected void main(String[] args);} // 4
interface E {private void main(String[] args);} // 5
Which interface declarations generate a Compile-time error?
Question 14
class SRC121 {
static String m1(byte i) {return "B";}
static String m1(char i) {return "C";}
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.min((byte)1,(byte)2)));
System.out.print(m1(Math.min('A','B')));
System.out.print(m1(Math.min(1,2)));
System.out.print(m1(Math.min((long)1,2)));
System.out.print(m1(Math.min(1.0f,1)));
System.out.print(m1(Math.min(1.0,1)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: DDDDDD |
| b. | Prints: FFFFFD |
| c. | Prints: FFFDFD |
| d. | Prints: IIILFD |
| e. | Prints: IILLDD |
| f. | Prints: IIILDD |
| g. | Prints: BCILFD |
| h. | Prints: CCILFD |
| i. | None of the above |
Question 15
interface Z {void m1();} // 1
class D implements Z {public final void m1() {}} // 2
class E implements Z {public synchronized void m1() {}} // 3
class G implements Z {public native 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 16
class Leg{}
class Fur{}
abstract class Pet {
public abstract void eat();
public abstract void sleep();
}
class Dog extends Pet {
Leg leftFront = new Leg(), rightFront = new Leg();
Leg leftRear = new Leg(), rightRear = new Leg();
Fur fur = new Fur();
public Fur shed() {return fur;}
public void eat() {}
public void sleep() {}
}
class Cat extends Dog {
public void ignoreOwner() {}
public void climbTree() {}
}
Which of the following statements is not a true statement?
| a. | A Cat object inherits an instance of Fur and four instances of Leg from the Dog superclass. |
| b. | A Cat object is able to sleep and eat. |
| c. | A Cat object is able to climb a tree. |
| d. | The relationship between Dog and Pet is an example of an appropriate use of inheritance. |
| e. | The relationship between Cat and Dog is an example of an appropriate use of inheritance. |
| f. | None of the above. |
Question 17
Which of the following are true statements?
| a. | A program will terminate only when all daemon threads stop running |
| b. | A program will terminate only when all user threads stop running |
| c. | A daemon thread always runs at Thread.MIN_PRIORITY |
| d. | A thread inherits its daemon status from the thread that created it |
| e. | The daemon status of a thread can be changed at any time using the Thread.setDaemon method |
| f. | The Thread.setDaemon method accepts one of two argument values defined by the constants Thread.DAEMON and Thread.USER |
Question 18
class MWC117 {
public static void main (String[] args) {
System.out.print(String.valueOf(1) + String.valueOf(2));
String s1 = "S1";
String s2 = s1.toString();
System.out.print("," + (s1==s2));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 3,false |
| b. | Prints: 3,true |
| c. | Prints: 12,false |
| d. | Prints: 12,true |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
Question 19
Suppose that you would like to create a new instance of a
class that implements the
Map
interface, and you would like
the new instance to be initialized with the elements
of an existing
Map.
If you would like the iteration order of the new
Map
to be
the same as that of the existing
Map,
then which
concrete implementation of the
Map
interface should be
used for the new instance?
| a. | Hashtable |
| b. | HashSet |
| c. | HashMap |
| d. | TreeMap |
| e. | TreeSet |
| f. | LinkedHashMap |
| g. | None of the above. |
Question 20
class A {
A() {System.out.print("CA ");}
static {System.out.print("SA ");}
}
class B extends A {
B() {System.out.print("CB ");}
static {System.out.print("SB ");}
public static void main (String[] args) {
B b = new B();
}}
What is the result of attempting to compile and run the above
program?
| a. | Prints: SA CA SB CB |
| b. | Prints: SA SB CA CB |
| c. | Prints: SB SA CA CB |
| d. | Prints: SB CB SA CA |
| e. | Runtime Exception |
| f. | Compiler Error |
| g. | None of the above |
Question 21
class A extends Thread {
public A(Runnable r) {super(r);}
public void run() {System.out.print("A");}
}
class B implements Runnable {
public void run() {System.out.print("B");}
}
class C {
public static void main(String[] args) {
new A(new B()).start();
}
}
What is the result of attempting to compile and run the program?
| a. | Prints: A |
| b. | Prints: B |
| c. | Prints: AB |
| d. | Prints: BA |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
Question 22
| | • | Stores key/value pairs. |
| | • | Allows null elements, keys, and values. |
| | • | Duplicate entries replace old entries. |
| | • | The least recently used element can be removed automatically when a new element is added. |
Which of these classes provides the specified features?
| a. | LinkedHashMap |
| b. | LinkedHashSet |
| c. | LinkedList |
| d. | TreeMap |
| e. | TreeSet |
| f. | HashMap |
| g. | HashSet |
| h. | Hashtable |
| i. | None of the above |
Question 23
class A implements Runnable {
public void run() {System.out.print(Thread.currentThread().getName());}
}
class B implements Runnable {
public void run() {
new A().run();
new Thread(new A(),"T2").run();
new Thread(new A(),"T3").start();
}
}
class C {
public static void main (String[] args) {
new Thread(new B(),"T1").start();
}
}
What is the result of attempting to compile and run the program?
| a. | Prints: T1T1T1 |
| b. | Prints: T1T1T2 |
| c. | Prints: T1T2T2 |
| d. | Prints: T1T2T3 |
| e. | Prints: T1T1T3 |
| f. | Prints: T1T3T3 |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
Question 24
class AnException extends Exception {}
class A extends Thread {
public void run() throws AnException {
System.out.print("A");
throw new AnException();
}
}
class B {
public static void main (String[] args) {
A a = new A();
a.start();
System.out.print("B");
}
}
What is the result of attempting to compile and run the program?
| a. | Prints: A |
| b. | Prints: B |
| c. | Prints: AB |
| d. | Prints: BA |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
Question 25
class JMM117 {
public static void main (String[] args) {
int i = 0, j = 9;
do {
i++;
if (j-- < i++) {break;}
} while (i < 5);
System.out.print(i + "," + j);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 5,4 |
| b. | Prints: 6,3 |
| c. | Prints: 6,6 |
| d. | Prints: 7,2 |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
Question 26
class JMM118 {
public static void main (String[] args) {
int i = 0, j = 9;
while (i++ <= j--) {i++; if (j < 5) break;}
System.out.print(i + "," + j);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 4,7 |
| b. | Prints: 6,6 |
| c. | Prints: 7,2 |
| d. | Prints: 8,5 |
| e. | Prints: 9,4 |
| f. | Run-time error |
| g. | Compile-time error |
| h. | None of the above |
Question 27
class JMM119 {
public static void main (String[] args) {
int i = 0, j = 9;
do {
if (j < 4) {break;} else if (j-- < 7) {continue;}
i++;
} while (i++ < 7);
System.out.print(i + "," + j);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 4,7 |
| b. | Prints: 6,6 |
| c. | Prints: 6,5 |
| d. | Prints: 6,4 |
| e. | Prints: 7,5 |
| f. | Prints: 8,4 |
| g. | Run-time error |
| h. | Compile-time error |
| i. | None of the above |
Question 28
class JMM120 {
public static void main (String args[]) {
int i = 0, j = 0, k = 0;
label1:
for (;;) { i++;
label2:
do {
k = i + j;
switch (k) {
case 0: continue label2;
case 1: continue label1;
case 2: break;
case 3: break label2;
case 4: break label1;
default: break label1;
}
} while (++j<5);
}
System.out.println(i + "," + j);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 2,1 |
| b. | Prints: 2,2 |
| c. | Prints: 3,1 |
| d. | Prints: 3,2 |
| e. | Prints: 3,3 |
| f. | Run-time error |
| g. | Compile-time error |
| h. | None of the above |
Copyright © 2002-2003, Dan Chisholm
All rights reserved.