Certified Java Programmer Mock Exam
Question 1
class EBH019 {
public static void main (String args[]) {
int i1 = 0xffffffff, i2 = i1 << 1;
int i3 = i1 >> 1, i4 = i1 >>> 1;
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3) + ",");
System.out.print(Integer.toHexString(i4));
}}
What is the result of attempting to compile and run the
program?
| a. | Prints: ffffffff,ffffffff,ffffffff |
| b. | Prints: ffffffff,ffffffff,7fffffff |
| c. | Prints: ffffffff,7fffffff,ffffffff |
| d. | Prints: ffffffff,7ffffffe,7ffffffe |
| e. | Prints: fffffffe,ffffffff,ffffffff |
| f. | Prints: fffffffe,ffffffff,7fffffff |
| g. | Prints: fffffffe,7fffffff,ffffffff |
| h. | Prints: fffffffe,7fffffff,7fffffff |
| i. | Run-time error |
| j. | Compile-time error |
| k. | None of the above |
Question 2
class MCZ20 {
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. | String a = 'a'; |
| b. | String b = 'abc'; |
| c. | String c = '\u0041'; |
| d. | String d = '\uabcd'; |
| e. | None of the above |
Question 3
class G {
public static void main (String[] args) {
Integer i1 = new Integer("1"), i2 = new Integer("1");
StringBuffer sb1 = new StringBuffer("1");
StringBuffer sb2 = new StringBuffer("1");
System.out.print(sb1.equals(sb2)+","+i1.equals(i2));
}}
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 4
| | • | 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 5
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 6
class MWC210 {
public static void main(String[] args) {
int[] a2 = {1,2}, a3 = {3,4,5}, a4 = {6,7,8,9}; // 1
int[][] a1 = {a2,a3,a4}; // 2
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 7
abstract class A { // 1
private abstract void m1(); // 2
private abstract class B {} // 3
private class C extends B {} // 4
}
Which line results in a
compile-time error?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above. |
Question 8
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
public static void main(String args[]) {
int a, b, c, d, f; a = b = c = d = f = 0;
int x = 2;
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level3Exception e) {b++;}
catch (Level2Exception e) {c++;}
catch (Level1Exception e) {d++;}
finally {f++;}
System.out.print(a+","+b+","+c+","+d+","+f);
}}
What is the result of attempting to compile and run the
program?
| a. | Prints: 0,0,0,1,1 |
| b. | Prints: 0,0,1,1,1 |
| c. | Prints: 0,1,1,1,1 |
| d. | Prints: 1,1,1,1,1 |
| e. | Prints: 0,0,1,0,1 |
| f. | Prints: 0,1,0,0,1 |
| g. | Prints: 1,0,0,0,1 |
| h. | Compile-time error |
| i. | Run-time error |
| j. | None of the above |
Question 9
interface A {
protected int e = 5; // 1
private int f = 6; // 2
volatile int g = 7; // 3
transient int h = 8; // 4
public static final int d = 9; // 5
}
Which of the field declarations does not result in a
compile-time error?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | 5 |
| f. | None of the above |
Question 10
class MCZ19 {
public static void main (String[] args) {
String a1 = null; // 1
String b1 = 'null'; // 2
String c1 = "null"; // 3
String d1 = "'null'"; // 4
}}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
Question 11
class A {
void m1() {System.out.print("A.m1");}
}
class B extends A {
void m1() {System.out.print("B.m1");}
static void m1(String s) {System.out.print(s+",");}
}
class C {
public static void main (String[] args) {
B.m1("main"); new B().m1();
}}
What is the result of attempting to compile and run the program?
| a. | Prints: main,B.m1 |
| b. | Compile-time error |
| c. | Run-time error |
| d. | None of the above |
Question 12
class MWC111 {
static void m1(String s1) {
s1.replace('A','Y'); System.out.print(s1);
}
static void m2(String s1) {
s1 = s1.replace('A','Z'); System.out.print(s1);
}
public static void main(String[] s) {
String s1 = "A"; m1(s1); m2(s1);
System.out.print(s1);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: AAA |
| b. | Prints: YZA |
| c. | Prints: YAA |
| d. | Prints: AZA |
| e. | Prints: AZZ |
| f. | Compile-time error |
| g. | Run-time error |
| h. | None of the above |
Question 13
class SRC114 {
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
System.out.print(m(Math.random()));
}}
What is the result of attempting to compile and run the program?
| a. | Prints: int |
| b. | Prints: long |
| c. | Prints: float |
| d. | Prints: double |
| e. | Compile-time error |
| f. | Run-time error |
| g. | None of the above |
Question 14
class H {
public static void main (String[] args) {
int i1, i2; Integer i3, i4;
i1=new Integer(Integer.parseInt("1")).intValue(); // 1
i3=new Integer(Integer.parseInt("1")).intValue(); // 2
i2=Integer.parseInt(Integer.valueOf("1").toString()); // 3
i4=Integer.parseInt(Integer.valueOf("1").intValue()); // 4
}}
What is the result of attempting to compile and run the program?
| a. | Compile-time error at 1. |
| b. | Compile-time error at 2. |
| c. | Compile-time error at 3. |
| d. | Compile-time error at 4. |
| e. | Compile-time error |
| f. | Run-time error |
Question 15
| | • | 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 16
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 17
class MWC211 {
public static void main(String[] args) {
int a1[3]; // 1
int []a2[]; // 2
int[ ]a3; // 3
int[] a4[]; // 4
}}
A compile-time error is generated at which line?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
Question 18
abstract class A { // 1
abstract final void m1(); // 2
abstract final class B {} // 3
class C extends B {} // 4
}
Which line does not result in a
compile-time error?
| a. | 1 |
| b. | 2 |
| c. | 3 |
| d. | 4 |
| e. | None of the above |
Question 19
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
public static void main(String args[]) {
int a, b, c, d, f; a = b = c = d = f = 0;
int x = 4;
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level3Exception e) {b++;}
catch (Level2Exception e) {c++;}
catch (Level1Exception e) {d++;}
finally {f++;}
System.out.print(a+","+b+","+c+","+d+","+f);
}}
What is the result of attempting to compile and run the
program?
| a. | Prints: 0,0,0,1,1 |
| b. | Prints: 0,0,1,1,1 |
| c. | Prints: 0,1,1,1,1 |
| d. | Prints: 1,1,1,1,1 |
| e. | Prints: 0,0,1,0,1 |
| f. | Prints: 0,1,0,0,1 |
| g. | Prints: 1,0,0,0,1 |
| h. | Compile-time error |
| i. | Run-time error |
| j. | None of the above |
Question 20
Which of the following are modifiers that can be applied
to a method declaration within an interface?
| a. | abstract |
| b. | final |
| c. | private |
| d. | protected |
| e. | public |
Question 21
class EBH020 {
public static void main (String[] args) {
int a = 1 | 2 ^ 3 & 5;
int b = ((1 | 2) ^ 3) & 5;
int c = 1 | (2 ^ (3 & 5));
System.out.print(a + "," + b + "," + c);
}}
What is the result of attempting to compile and run the
program?
| a. | Prints: 0,0,0 |
| b. | Prints: 0,0,3 |
| c. | Prints: 0,3,0 |
| d. | Prints: 0,3,3 |
| e. | Prints: 3,0,0 |
| f. | Prints: 3,0,3 |
| g. | Prints: 3,3,0 |
| h. | Prints: 3,3,3 |
| i. | Run-time error |
| j. | Compile-time error |
| k. | None of the above |
Question 22
class A {
private void m1 (int i) {
assert i < 10 : i; System.out.print(i);
}
public void m2 (int i) {
assert i < 10 : i; System.out.print(i);
}
public static void main (String[] args) {
A a = new A(); a.m1(11); a.m2(12);
}}
Which statements are true?
| a. | If assertions are enabled at run time it prints an error message. |
| b. | With assertions enabled it prints nothing. |
| c. | With assertions disabled it prints an error message. |
| d. | With assertions disabled it prints 1112. |
| e. | With assertions disabled it prints nothing. |
| f. | The assert statements are being used to check a precondition--something that must be true when the method is invoked. |
| g. | Method m1 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a non-public method. |
| h. | Method m2 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a public method. |
Question 23
class A {
private static int counter;
public static int getCounter(){return counter++;}
private static int innerCounter;
public static int getInnerCounter(){return innerCounter++;}
private String name;
A() {name = "A" + getCounter();}
class B {
private String name;
B() {
name = "B" + getInnerCounter();
System.out.print(A.this.name + name); // 1
}}
void m1() {new A().new B();} // 2
void m2() {this.new B();} // 3
void m3() {new B();} // 4
public static void main(String[] args) {
A a1 = new A();
a1.m1(); a1.m2(); a1.m3();
}}
What is the result of attempting to compile and run the program?
| a. | Prints: A0B0A1B1A1B2 |
| b. | Prints: A0B0A1B1A2B2 |
| c. | Prints: A1B0A0B1A0B2 |
| d. | Compile-time error at line 1 |
| e. | Compile-time error at line 2 |
| f. | Compile-time error at line 3 |
| g. | Compile-time error at line 4 |
| h. | Other compile-time error. |
| i. | Run-time error |
| j. | None of the above |
Question 24
Which of the following are true statements?
| a. | The Thread.yield method might cause the thread to move to the Not-Runnable state |
| b. | The Thread.yield method might cause the thread to move to the Ready state |
| c. | The same thread might continue to run after calling the Thread.yield method |
| d. | The Thread.yield method is a static method |
| e. | The behavior of the Thread.yield method is consistent from one platform to the next |
| f. | The Thread.sleep method causes the thread to move to the Not-Runnable state |
| g. | The Thread.sleep method causes the thread to move to the Ready state |
Question 25
class B {
int a, b, c;
private void setA(int i) {a = i;}
private void setB(int i) {b = i;}
private int m1 (int i) {
c = a + b + i; assert c < 200 : c; return c;
}
public static void main (String[] args) {
B b = new B(); b.setA(50); b.setB(100); b.m1(50);
}}
Which statements are true?
| a. | If assertions are not enabled at run time it prints an error message. |
| b. | If assertions are not enabled at run time it prints nothing. |
| c. | With assertions enabled it prints an error message. |
| d. | With assertions enabled it prints nothing. |
| e. | The assert statement is being used to check a postcondition--something that must be true when the method completes successfully. |
Question 26
class JMM111 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i < 2; i++) do
System.out.print(i);
while (j++ < 2);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0001 |
| b. | Prints: 012 |
| c. | Prints: 012012 |
| d. | Prints: 012345 |
| e. | Prints: 001122 |
| f. | Prints: 1112 |
| g. | Prints: 111222 |
| h. | Prints: 121212 |
| i. | Run-time error |
| j. | Compile-time error |
| k. | None of the above |
Question 27
class JMM112 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i++ < 2;) do
System.out.print(i);
while (j++ < 2);
}}
What is the result of attempting to compile and run the program?
| a. | Prints: 0001 |
| b. | Prints: 012 |
| c. | Prints: 012012 |
| d. | Prints: 012345 |
| e. | Prints: 001122 |
| f. | Prints: 1112 |
| g. | Prints: 111222 |
| h. | Prints: 121212 |
| i. | Run-time error |
| j. | Compile-time error |
| k. | None of the above |
Question 28
class A {String s1="A";}
class B extends A {String s1="B";}
class C extends B {String s1="C";}
class D extends C {
String s1="D";
void m1() {
System.out.print(this.s1 + ","); // 1
System.out.print(((C)this).s1 + ","); // 2
System.out.print(((B)this).s1 + ","); // 3
System.out.print(((A)this).s1); // 4
}
public static void main (String[] args) {
new D().m1(); // 5
}}
What is the result of attempting to compile and run the program?
| a. | Prints: D,D,D,D |
| b. | Prints: D,C,B,A |
| 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. | Compile-time error at 5. |
| h. | Run-time error |
| i. | None of the above |
Copyright © 2002-2003, Dan Chisholm
All rights reserved.