Certified Java Programmer Mock Exam


Question 1

A class can not be called "tightly encapsulated" unless which of the following is true?

a. The class is a nested class.
b. The constructors are declared private.
c. The mutator methods are declared private.
d. The class implements the Encapsulated interface.
e. None of the above

Question 2

Which of the following instance methods should only be called by a thread that holds the lock of the instance on which the method is invoked?

a. join
b. notify
c. notifyAll
d. run
e. start
f. wait

Question 3

Which of these methods is a static member of the java.lang.String class?

a. append
b. concat
c. delete
d. insert
e. replace
f. substring
g. valueOf
h. None of the above.

Question 4

class SRC107 {
  public static void main (String[] args) {
    int a = -1; long b = -2;
    float c = -3.0f; double d = -4.0;
    a = Math.abs(a); b = Math.abs(b);
    c = Math.abs(c); d = Math.abs(d);
    System.out.print(a+b+c+d);
}}

What is the result of attempting to compile and run the program?

a. Prints: 10.0
b. Compile-time error
c. Run-time error
d. None of the above

Question 5

class MWC205 {
  static void m1(StringBuffer s1) {
    s1.append("B"); System.out.print(s1);
  }
  static void m2(StringBuffer s1) {
    s1 = new StringBuffer("C"); System.out.print(s1);
  }
  public static void main(String[] s) {
    StringBuffer s1 = new StringBuffer("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: ABCA
c. Prints: ABCAB
d. Prints: ABCABC
e. Prints: ABCAC
f. Prints: ABABCABC
g. Compile-time error
h. Run-time error
i. None of the above

Question 6

class F {
  public static void main (String[] args) {
    System.out.print(Byte.MIN_VALUE+","+Byte.MAX_VALUE);
}}

What is the result of attempting to compile and run the program?

a. Prints: -128,127
b. Prints: -127,128
c. Prints: 0,255
d. Compile-time error
e. Run-time error
f. None of the above

Question 7

class D {
  public static void main (String args[]) {
    boolean b1 = Integer.MIN_VALUE == 0x80000000;
    boolean b2 = Integer.MAX_VALUE == 0x7FFFFFFF;
    System.out.print(b1 + "," + b2);
}}

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 8

class G {
  public static void main (String[] args) {
    Long l1 = new Long("1"), l2 = new Long("1");
    int h1 = l1.hashCode(), h2 = l2.hashCode();
    StringBuffer s1 = new StringBuffer("1");
    StringBuffer s2 = new StringBuffer("1");
    int h3 = s1.hashCode(), h4 = s2.hashCode();
    System.out.print((l1.equals(l2) & (h1==h2)) + ",");
    System.out.print(s1.equals(s2) | (h3==h4));
}}

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 9

Which methods of the java.lang.Double class return a primitive value?

a. doubleValue
b. floatValue
c. intValue
d. longValue
e. parseDouble
f. toString(double)
g. valueOf

Question 10

class E {
  static String m1(boolean b) {return "b";}
  static String m1(Boolean b) {return "B";}
  public static void main(String[] args) {
    Boolean b1 = new Boolean(true);
    System.out.print(m1(Boolean.valueOf(null)));
    System.out.print(m1(b1.booleanValue()));
    System.out.println(m1(Boolean.TRUE));
}}

What is the result of attempting to compile and run the program?

a. Prints: bbb
b. Prints: bbB
c. Prints: bBb
d. Prints: bBB
e. Prints: Bbb
f. Prints: BbB
g. Prints: BBb
h. Prints: BBB
i. Compile-time error
j. Run-time error
k. None of the above

Question 11

class F {
  static String m(float f) {return "f";}
  static String m(Float f) {return "F";}
  public static void main (String[] args) {
    Float f1 = new Float(1);
    System.out.print(m(f1.parseFloat("1")));
    System.out.print(m(f1.floatValue()));
    System.out.print(m(f1.valueOf("1")));
}}

What is the result of attempting to compile and run the program?

a. Prints: fff
b. Prints: ffF
c. Prints: fFf
d. Prints: fFF
e. Prints: Fff
f. Prints: FfF
g. Prints: FFf
h. Prints: FFF
i. Compile-time error
j. Run-time error
k. None of the above

Question 12

 Entries are not organized as key/value pairs.
 Duplicate entries are rejected.

Which interface of the java.util package offers the specified behavior?

a. List
b. Map
c. Set
d. None of the above

Question 13

import java.util.*;
class GFC105 {
  public static void main (String args[]) {
    Object a = new HashSet(), b = new HashMap();
    Object c = new Hashtable();
    System.out.print((a instanceof Collection)+",");
    System.out.print((b instanceof Collection)+",");
    System.out.print(c 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 14


class B {
  private int i1;
  public int hashCode() {return 1;}
}
class C {
  private int i1;
  public int hashCode() {return -1;}
}
class D {
  private int i1;
  public int hashCode() {return i1;}
}

Suppose that the equals method of classes B, C and D all make use of the value of the int variable, i1. Which class has a hashCode method that is not consistent with the hash code contract?

a. B
b. C
c. D
d. None of the above

Question 15

class JSC103 {
  transient float e = 1;         // 1
  volatile float f = 1;          // 2
  abstract float j = 1;          // 3
  final float g = 1;             // 4
  private final float k = 1;     // 5
  private transient float l = 1; // 6
  volatile final float m = 1;    // 7
}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7

Question 16

Which of the following statements are true?

a. If an accessible superclass method is static, then any method with the same signature in a subclass must also be static.
b. If a superclass method is synchronized, then the overriding method must also be synchronized.
c. If a superclass method is public, then the overriding method must also be public.
d. If a superclass method is native, then the overriding method must also be native.
e. If a superclass method is protected, then the overriding method must be protected or public.

Question 17

protected class D {}    // 1
synchronized class F {} // 2
volatile class H {}     // 3
final class B {}        // 4

Suppose these are top-level class declarations and not nested class declarations. Which of these declarations would not produce a compile-time error?

a. 1
b. 2
c. 3
d. 4
e. None of the above

Question 18

class Z {
  static class F {}       // 1
  synchronized class G {} // 2
  transient class H {}    // 3
  volatile class I {}     // 4
}

Which class declaration 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 Purple {
  public static void main(String args[]) {
    int a,b,c,d,f,g,x;
    a = b = c = d = f = g = 0;
    x = 5;
    try {
      try {
	switch (x) {
          case 1: throw new Level1Exception();
          case 2: throw new Level2Exception();
          case 3: throw new Level3Exception();
          case 4: throw new Exception();
      } a++; }
      catch (Level2Exception e) {b++;}
      finally {c++;}
    }
    catch (Level1Exception e) { d++;}
    catch (Exception e) {f++;}
    finally {g++;}
    System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}

What is the result of attempting to compile and run the program?

a. Prints: 1,0,0,0,0,0
b. Prints: 1,0,1,0,0,1
c. Prints: 0,0,1,0,0,1
d. Prints: 1,1,1,1,1,1
e. Compile-time error
f. Run-time error
g. None of the above

Question 20

class GFC202 {}
class GFC203 extends GFC202 {}
class GFC204 {
  static void m(GFC202 x) {System.out.print("GFC202");}
  static void m(GFC203 x) {System.out.print("GFC203");}
  public static void main(String[] args) {
    m(null);
}}

What is the result of attempting to compile and run the program?

a. Prints: GFC202
b. Prints: GFC203
c. Compile-time error
d. Run-time error
e. None of the above

Question 21

class T {
  private int i1, i2;
  void printI1I2() {System.out.print("T, i1="+i1+", i2="+i2);}
  T(int i1, int i2) {
    this.i1=i1; this.i2=i2;
}}
class U extends T {
  private int i1, i2;
  void printI1I2() {System.out.print("U, i1="+i1+", i2="+i2);}
  U(int i1, int i2) {this.i1=i1; this.i2=i2;}
  public static void main(String[] args) {
    T t = new U(1,2); t.printI1I2();
}}

What is the result of attempting to compile and run the program?

a. Prints: U, i1=1, i2=2
b. Prints: T, i1=1, i2=2
c. Prints: U, i1=null, i2=null
d. Prints: T, i1=null, i2=null
e. Run-time error
f. Compile-time error
g. None of the above

Question 22

class F {
  public void m1() {Z.m1();}      // 1
  private static class Y {
    private static void m1() {
      System.out.print("Y.m1 ");
  }}
  private static class Z {
    private static void m1(){
      System.out.print("Z.m1 ");
      Y.m1();                     // 2
  }}
  public static void main(String[] args) {
    new F().m1();
}}

What is the result of attempting to compile and run the program?

a. Compile-time error at line 1
b. Compile-time error at line 2
c. Run-time error at line 1
d. Run-time error at line 2
e. Prints: Z.m1 Y.m1
f. None of the above

Question 23

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(); A c2 = new C();
    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 24


class A {
  private static int f1 = 1;
  private int f2 = 2;
  void m1(int p1, final int p2) {
    int l1 = 5;
    final int l2 = 6;
    Object x = new Object() {
      int a = f1;  // 1
      int b = f2;  // 2
      int c = p1;  // 3
      int d = p2;  // 4
      int e = l1;  // 5
      int f = l2;  // 6
};}}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6

Question 25

class JMM106 {
  public static void main(String args[]) {
    int x = -5; int success = 0;
    do  {
      switch(x) {
        case 0: System.out.print("0"); x += 5; break;
        case 1: System.out.print("1"); x += 3; break;
        case 2: System.out.print("2"); x += 1; break;
        case 3: System.out.print("3"); success++; break;
        case 4: System.out.print("4"); x -= 1; break;
        case 5: System.out.print("5"); x -= 4; break;
        case 6: System.out.print("6"); x -= 5; break;
        default: x += x < 0 ? 2 : -2;
      }
    } while ((x != 3) || (success < 2));
}}

What is the result of attempting to compile and run the program?

a. Prints: 60514233
b. Prints: 1433
c. Prints: 61433
d. Prints: 051433
e. Run-time error
f. Compile-time error

Question 26

class Amber {
  public static void main(String[] args) {
    int[][] a = {{1,2},{0,1,2},{-1,0,2}};  // 1
    Object[] obj = (Object[])a.clone();    // 2
    for(int i = 0; i < obj.length; i++) {  // 3
      int[] ia = (int[])obj[i];            // 4
      System.out.print(ia[i]);             // 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 27

class GFC307 {
  static void m1(int[] i1, int[] i2) {
    i1 = i2 = null;
  }
  public static void main (String[] args) {
    int[] i1 = {1}, i2 = {3}; m1(i1, i2);
    System.out.print(i1[0] + "," + i2[0]);
}}

What is the result of attempting to compile and run the program?

a. Prints: 0,0
b. Prints: 1,1
c. Prints: 1,3
d. Prints: 3,1
e. Prints: null,null
f. Run-time error
g. Compile-time error
h. None of the above

Question 28

class A11 {public String toString() {return "A11";}}
class A12 {
  public static void main(String[] arg) {
    A11[]     a1 = new A11[1];         // 1
    A11[][]   a2 = new A11[2][];       // 2
    A11[][][] a3 = new A11[3][][];     // 3
    a1[0] = new A11();                 // 4
    a2[0] = a2[1] = a1;                // 5
    a3[0] = a3[1] = a3[2] = a2;        // 6
    System.out.print(a3[2][1][0]);     // 7
}}

What is the result of attempting to compile and run the program?

a. Prints: null
b. Prints: A11
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. Compile-time error at 6.
i. Compile-time error at 7.
j. Run-time error
k. None of the above


Copyright © 2002-2003, Dan Chisholm
All rights reserved.