Certified Java Programmer Mock Exam
Question 1
Which of these words belong to the
set of Java keywords?
| a. | next |
| b. | catch |
| c. | function |
| d. | instanceof |
| e. | mod |
| f. | const |
| g. | or |
| h. | Boolean |
| i. | goto |
| j. | import |
| k. | transient |
Question 2
class MCZ16 {
public static void main (String[] args) {
float a = 1; // 1
float b = 1L; // 2
float c = 1F; // 3
float d = 1.0; // 4
}}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
Question 3
class Red {
public int a;
public static int b;
public static void main (String[] in) {
Red r1 = new Red(), r2 = new Red();
r1.a++; r1.b++;
System.out.print(r1.a+", "+r1.b+", "+r2.a+", "+r2.b);
}}
What is the result of attempting to compile and run the
program?
| a. | Prints: 0, 0, 0, 0 |
| b. | Prints: 0, 1, 1, 1 |
| c. | Prints: 1, 1, 1, 0 |
| d. | Prints: 1, 1, 0, 1 |
| e. | Prints: 1, 1, 0, 0 |
| f. | Prints: 1, 1, 1, 1 |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
Question 4
Which of these words belong to the set of Java keywords?
| a. | byte |
| b. | short |
| c. | int |
| d. | long |
| e. | decimal |
| f. | int64 |
| g. | float |
| h. | single |
| i. | double |
| j. | boolean |
| k. | char |
| l. | unsigned |
| m. | array |
| n. | string |
Question 5
class MCZ17 {
public static void main (String[] args) {
String a = "\n"; // 1
String b = "\r"; // 2
String c = "\u000a"; // 3 \u000a = new line
String d = "\u000d"; // 4 \u000d = return
}}
Compile-time errors are generated at which lines?
Question 6
class JMM108 {
static boolean b;
public static void main(String[] args) {
if (b) {System.out.print("A");
} else if (b = false) {System.out.print("B");
} else if (b) {System.out.print("C");
} else if (!b) {System.out.print("D");
} else {System.out.print("E");}
}}
What is the result of attempting to compile and run the program?
| a. | Prints: A |
| b. | Prints: B |
| c. | Prints: C |
| d. | Prints: D |
| e. | Prints: E |
| f. | Run-time error |
| g. | Compile-time error |
| h. | None of the above |
Question 7
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new ColorException();}
void m2() throws WhiteException {throw new ColorException();}
public static void main (String[] args) {
White white = new White();
int a,b,d,f; a = b = d = f = 0;
try {white.m1(); a++;} catch (ColorException e) {b++;}
try {white.m2(); d++;} catch (WhiteException e) {f++;}
System.out.print(a+","+b+","+d+","+f);
}}
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 8
Which of the following is a modifier that can be applied
to a field declaration within an interface?
| a. | static |
| b. | synchronized |
| c. | transient |
| d. | volatile |
| e. | None of the above. |
Question 9
class EBH014 {
public static void main (String[] args) {
byte x = 3, y = 5;
System.out.print((y % x) + ",");
System.out.print(y == ((y/x)*x + (y%x)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 1,true |
| b. | Prints: 2,true |
| c. | Prints: 1,false |
| d. | Prints: 2,false |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
Question 10
class GFC505 extends GFC504 {
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}
class GFC504 extends GFC503 {
private void setName(String name) {this.name = name;}
private String getName() {return name;}
}
class GFC503 {String name;}
Which class is tightly encapsulated?
| a. | GFC503 |
| b. | GFC504 |
| c. | GFC505 |
| d. | None of the above |
Question 11
class MWC108 {
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
s2.trim(); s3.append("D");
System.out.print(s1 + s2 + s3);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: ABC |
| b. | Prints: A B C |
| c. | Prints: ABCD |
| d. | Prints: ABDC |
| e. | Prints: A B CD |
| f. | Prints: A B DC |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
Question 12
class SRC111 {
static String m(byte i) {return "byte";}
static String m(short i) {return "short";}
static String m(char i) {return "char";}
static String m(int i) {return "int";}
static String m(double i) {return "double";}
public static void main (String[] args) {
byte b = 0; short s = 0; char c = 0;
System.out.print(m(Math.min(b,b))+",");
System.out.print(m(Math.min(s,s))+",");
System.out.print(m(Math.min(c,c)));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: byte,byte,byte |
| b. | Prints: short,short,short |
| c. | Prints: int,int,int |
| d. | Prints: byte,short,int |
| e. | Prints: short,short,int |
| f. | Prints: byte,short,char |
| g. | Compile-time error |
| h. | Run-time error |
| i. | None of the above |
Question 13
Which of the following methods of the java.lang.Integer
class returns a primitive int type?
| a. | intValue |
| b. | parseInt |
| c. | valueOf |
Question 14
Which methods of the
java.lang.Double
class declare a
NumberFormatException
in the throws clause?
| a. | doubleValue |
| b. | floatValue |
| c. | intValue |
| d. | longValue |
| e. | parseDouble |
| f. | toString(double) |
| g. | valueOf |
Question 15
| | • | 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 16
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 17
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 18
class MWC208 {
public static void main(String[] args) {
int[][] a1 = ((1,2),(3,4,5),(6,7,8,9));
System.out.print(a1[0][1]+","+a1[1][2]+","+a1[2][3]);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 14 |
| b. | Prints: 16 |
| c. | Prints: 1,5,9 |
| d. | Prints: 2,4,8 |
| e. | Prints: 2,5,9 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
Question 19
class A {
A() {} // 1
int A; // 2
void A() {} // 3
class A {} // 4
}
Which line results in a compile-time error?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
Question 20
class JMM109 {
public static void main(String[] args) {
boolean b;
if (b = false) {System.out.print("A");
} else if (b) {System.out.print("B");
} else if (!b) {System.out.print("C");
} else {System.out.print("D");}
}}
What is the result of attempting to compile and run the program?
| a. | Prints: A |
| b. | Prints: B |
| c. | Prints: C |
| d. | Prints: D |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
Question 21
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new ColorException();}
void m2() throws WhiteException {throw new WhiteException();}
public static void main (String[] args) {
White white = new White();
int a,b,d,f; a = b = d = f = 0;
try {white.m1(); a++;} catch (WhiteException e) {b++;}
try {white.m2(); d++;} catch (WhiteException e) {f++;}
System.out.print(a+","+b+","+d+","+f);
}}
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 22
Which of the following modifiers can be applied to a
class that is declared within an enclosing interface?
| a. | public |
| b. | protected |
| c. | private |
| d. | abstract |
| e. | static |
| f. | final |
Question 23
class A {static void m() {System.out.print("A");}}
class B extends A {static void m() {System.out.print("B");}}
class C extends B {static void m() {System.out.print("C");}}
class D {
public static void main(String[] args) {
C c = new C();
c.m(); B b = c; b.m(); A a = b; a.m();
}}
What is the result of attempting to compile and run the program?
| a. | Prints: AAA |
| b. | Prints: ABC |
| c. | Prints: CBA |
| d. | Prints: CCC |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
Question 24
class Red {
private static final int a = 10; // 1
protected static int b = 20; // 2
int c = 30; // 3
static class StaticNested {
int d = a; // 4
int e = b; // 5
int f = c; // 6
}
class NonStaticInner {
int g = a; // 7
int h = b; // 8
int i = c; // 9
}}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | 6 |
| g. | 7 |
| h. | 8 |
| i. | 9 |
Question 25
synchronized (expression) block
The synchronized statement has the form shown above.
Which of the following are true statements?
| a. | A compile-time error occurs if the expression produces a value of any reference type |
| b. | A compile-time error occurs if the expression produces a value of any primitive type |
| c. | A compile-time error does not occur if the expression is of type boolean |
| d. | The sychronized block may be processed normally if the expression is null |
| e. | If execution of the block completes normally, then the lock is released |
| f. | If execution of the block completes abruptly, then the lock is released |
| g. | A thread can hold more than one lock at a time |
| h. | Synchronized statements can be nested |
| i. | Synchronized statements with identical expressions can be nested |
Question 26
class E {
private boolean b1, b2, b3;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void setB3(boolean b) {b3 = b;}
public void m1 (int i) {
b2 = i % 2 == 0;
if (!b3 & !b2 & !b1) {System.out.print("A");
} else if (!b3 & !b2 & b1) {System.out.print("B");
} else if (!b3 & b2 & !b1) {System.out.print("C");
} else { // Only b3 is true.
assert b3 & !b2 & !b1;
}
System.out.print(b1 + "," + b2 + "," + b3);
b1 = b2 = b3 = false;
}
public static void main (String[] args) {
E e = new E(); e.setB1(true); e.m1(2);
}}
Which statements are true?
| a. | With assertions enabled it prints an error message. |
| b. | With assertions enabled it prints: true,true,false |
| c. | With assertions disabled it prints an error message. |
| d. | With assertions disabled it prints: true,true,false |
| e. | With assertions disabled it prints nothing. |
| f. | The combination of the if/else statements and the assert statement indicate that the programmer expects no more than one boolean, b1, b2 or b3, to be true. |
| g. | The assert statement is being used to check a precondition--something that must be true when the method begins. |
| h. | The assert statement is being used to check an internal invariant--something that the programmer assumes to be true at a particular point in the program. |
Question 27
class A {}
class B extends A {}
class C extends B {
static void m(A x, A y) {System.out.print("AA");}
static void m(A x, B y) {System.out.print("AB");}
static void m(B x, A y) {System.out.print("BA");}
static void m(B x, B y) {System.out.print("BB");}
public static void main(String[] args) {
A a1; B b1;
m(null,null); m(a1=null,b1=null); m(b1, a1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: BBABAB |
| b. | Prints: BBABBA |
| c. | Prints: BBBBAB |
| d. | Prints: BBBBBA |
| e. | Prints: BBBBBB |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
Question 28
class F {
private boolean b1, b2, b3;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void setB3(boolean b) {b3 = b;}
public String m1 (int i) {
b2 = i % 2 == 0;
if (!b3 & !b2 & !b1) {return "None are true.";
} else if (!b3 & !b2 & b1) {return "Only b1 is true.";
} else if (!b3 & b2 & !b1) {return "Only b2 is true.";
} else if (b3 & !b2 & !b1) {return "Only b3 is true.";
} else {throw new AssertionError();}
}
public static void main (String[] args) {
F f = new F();
f.setB1(true);
System.out.println(f.m1(5));
System.out.println(f.m1(6));
}}
Which statements are true?
| a. | Prints "Only b1 is true" followed by an error message. |
| b. | An assertion should not be placed at any location that the programmer believes
will never be reached under normal operating conditions. |
| c. | The combination of the if/else statements and the assert statement indicate that the programmer expects no more than one boolean, b1, b2, or b3, to be true. |
| d. | The assert statement is being used to check a precondition--something that must be true when the method begins. |
| e. | The assert statement is being used to check a control-flow invariant to verify that the control flow never reaches a point in the program. |
| f. | The throw statement could be replaced by an assert statement. |
Copyright © 2002-2003, Dan Chisholm
All rights reserved.