Certified Java Programmer Mock Exam
Question 1
Which of these words belong to the
set of Java keywords?
| a. | declare |
| b. | global |
| c. | const |
| d. | preserve |
| e. | continue |
| f. | Float |
| g. | extends |
| h. | select |
| i. | break |
| j. | dim |
| k. | instanceOf |
Question 2
class MCZ15 {
public static void main (String[] args) {
float a = 1.1e1f; // 1
float b = 1e-1F; // 2
float c = .1e1f; // 3
double d = .1d; // 4
double e = 1D; // 5
}}
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 3
class JSC105 {
private static int x;
protected static int y;
public static int z;
public static void main (String[] args) {
System.out.println(x+y+z);
}}
What is the result of attempting to compile and run the program?
| a. | Prints nothing. |
| b. | Prints an undefined value. |
| c. | Prints: null |
| d. | Prints: 0 |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
Question 4
class Z {
void m1() {
abstract class A {} // 1
final class B {} // 2
private class C {} // 3
protected class D {} // 4
public class E {} // 5
}
}
Which class declarations result in
compile-time errors?
Question 5
class JMM107 {
public static void main(String[] args) {
boolean b = true;
if (b = false) {System.out.print("A");
} else if (b) {System.out.print("B");
} else {System.out.print("C");}
}}
What is the result of attempting to compile and run the program?
| a. | Prints: A |
| b. | Prints: B |
| c. | Prints: C |
| d. | Run-time error |
| e. | Compile-time error |
| f. | None of the above |
Question 6
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new WhiteException();}
void m2() throws 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 (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,0 |
| c. | Prints: 0,1,1,0 |
| d. | Prints: 1,1,1,0 |
| e. | Prints: 1,1,1,1 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
Question 7
Which of the following are modifiers that can be applied
to a field declaration within an interface?
| a. | abstract |
| b. | const |
| c. | final |
| d. | private |
| e. | protected |
| f. | public |
Question 8
class JJF6 {
char c1 = 0xffff; // 1
char c2 = 0xfffff; // 2
byte b1 = 0xffff; // 3
byte b2 = 0x7f; // 4
byte b3 = 0xff; // 5
byte b4 = -0x80; // 6
}
Compile-time errors are generated at which lines?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | 6 |
Question 9
class EBH013 {
public static void main (String[] args) {
byte x = 3, y = 5;
System.out.print((~x == -x - 1)+","+(~y == -y - 1));
}}
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. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
Question 10
class GFC500 {private String name;}
class GFC501 {
private String name;
private void setName(String name) {this.name = name;}
private String getName() {return name;}
}
class GFC502 {
private String name;
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}
Which class is not tightly encapsulated?
| a. | GFC501 |
| b. | GFC502 |
| c. | GFC503 |
| d. | None of the above |
Question 11
class MWC107 {
public static void main(String[] s) {
String s1 = "A", s2 = " B ", s3 = "C";
s2.trim(); s3.concat("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 SRC110 {
public static void main (String[] args) {
byte x1 = 0; byte x2 = 1;
byte a1 = Math.min(x1,x2); // 1
int c1 = Math.min(0,1); // 2
long d1 = Math.min(0L,+1L); // 3
System.out.print(a1+","+c1+","+d1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0,0,0 |
| b. | Compile-time error at 1 |
| c. | Compile-time error at 2 |
| d. | Compile-time error at 3 |
| e. | Run-time error |
| f. | None of the above |
Question 13
class B {
public static void main (String[] args) {
byte b1 = 11;
System.out.print(new Integer(b1).equals(new Integer(b1)) + ",");
System.out.print(new Integer(b1).equals(new Short(b1)) + ",");
System.out.print(new Integer(b1).equals(new Byte(b1)));
}}
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. | Compile-time error |
| j. | Run-time error |
| k. | None of the above |
Question 14
Which of the method invocation expressions would produce
a run-time error?
| a. | Long.parseLong("-1") |
| b. | Long.parseLong("+1") |
| c. | Long.parseLong("01") |
| d. | Long.parseLong("1L") |
| e. | Long.parseLong("1.0") |
Question 15
Which methods of the
java.lang.Double
class declare a parameter of type
String?
| a. | doubleValue |
| b. | floatValue |
| c. | intValue |
| d. | longValue |
| e. | parseDouble |
| f. | valueOf |
Question 16
| | • | 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 17
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 18
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 19
class MWC207 {
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 20
class Z {
void m1() {
static class F {} // 1
synchronized class G {} // 2
transient class H {} // 3
volatile class I {} // 4
abstract class A {} // 5
final class B {} // 6
}
}
Compile-time errors are generated at which lines?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | 6 |
Question 21
class D {
private boolean b1, b2;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void m1 () {
if (!b2 & !b1) {System.out.print("A");
} else if (!b2 & b1) {System.out.print("B");
} else if (b2 & !b1) {System.out.print("C");
} else {assert false;}
}
public static void main (String[] args) {
D d = new D();
d.setB1(true); d.setB2(true);
d.m1();
}}
Which statements are true?
| a. | With assertions enabled it prints an AssertionError message. |
| b. | With assertions enabled it prints nothing. |
| c. | With assertions disabled it prints an AssertionError message. |
| d. | With assertions disabled it prints nothing. |
| e. | An assertion should not be placed at any location that the programmer believes will never be reached under normal operating conditions. |
| f. | 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. |
Question 22
class I {
private String name;
public String toString() {return name;}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {a1 = null;}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1);
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i]);
}}}
After method
m1
returns, the object created on which
line is eligible for garbage collection?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
Question 23
class GFC211 {}
class GFC212 extends GFC211 {}
class GFC213 extends GFC212 {
static void m(GFC211 x, GFC211 y) {System.out.print("GFC211,GFC211");}
static void m(GFC211 x, GFC212 y) {System.out.print("GFC211,GFC212");}
static void m(GFC212 x, GFC211 y) {System.out.print("GFC212,GFC211");}
static void m(GFC212 x, GFC212 y) {System.out.print("GFC212,GFC212");}
static void m(GFC211 x, GFC213 y) {System.out.print("GFC211,GFC213");}
public static void main(String[] args) {
GFC213 gfc213 = new GFC213();
m(gfc213, gfc213);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: GFC211,GFC211 |
| b. | Prints: GFC211,GFC212 |
| c. | Prints: GFC212,GFC211 |
| d. | Prints: GFC212,GFC212 |
| e. | Prints: GFC211,GFC213 |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
Question 24
abstract class D {
String s1 = "D"; String getS1() {return s1;}
}
class E extends D {
String s1 = "E"; String getS1() {return s1;}
}
class F {
public static void main (String[] s) {
D x = new E();
System.out.print(x.s1 + x.getS1());
}}
What is the result of attempting to compile and run the program?
| a. | Prints: DD |
| b. | Prints: DE |
| c. | Prints: ED |
| d. | Prints: EE |
| e. | Run-time error |
| f. | Compile-time error |
| g. | None of the above |
Question 25
class Outer {
static class StaticNested {
static final int a = 25; // 1
static final int b; // 2
static int c; // 3
int d; // 4
static {b = 42;} // 5
}
class NonStaticInner {
static final int e = 25; // 6
static final int f; // 7
static int g; // 8
int h; // 9
static {f = 42;} // 10
}}
Compile-time errors are generated at which lines?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | 6 |
| g. | 7 |
| h. | 8 |
| i. | 9 |
| j. | 10 |
Question 26
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
public static void main(String[] args) {
A a1 = new A(); B b1 = new B();
C c1 = new C(); C c2 = new A();
c2.m1(a1); c2.m1(b1); c2.m1(c1);
}
}
What is the result of attempting to compile and run the program?
| a. | Prints: AAA |
| b. | Prints: ABC |
| c. | Prints: CCC |
| d. | Compile-time error |
| e. | Run-time error |
| f. | None of the above |
Question 27
Which kind of variable would you
prefer to synchronize on?
| a. | A member variable of a primitive type |
| b. | A member variable that is an object reference |
| c. | A method local variable that is a reference to an instance that is created within the method |
| d. | None of the above |
Question 28
class EBH108 {
public static void main(String s[]) {
int i=0;
int j = ++i + ((++i * ++i) % ++i) + ++i;
System.out.print(j%5);
}}
What is the result of attempting to compile and run the above
program?
| a. | Prints: 1 |
| b. | Prints: 2 |
| c. | Prints: 3 |
| d. | Prints: 4 |
| e. | Prints: 5 |
| f. | Run-time error |
| g. | Compile-time error |
| h. | None of the above |
Copyright © 2002-2003, Dan Chisholm
All rights reserved.