Certified Java Programmer Mock Exam


Question 1

class MCZ21 {
  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. char a = a;
b. char b = abc;
c. char c = \u0041;
d. char d = \uabcd;
e. None of the above

Question 2

interface A {
  void m1();            // 1
  public void m2();     // 2
  protected void m3();  // 3
  private void m4();    // 4
}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4

Question 3

class MWC112 {
  static void m1(String s1) {
    s1 = s1.toUpperCase(); System.out.print(s1);
  }
  static void m2(String s1) {
    s1.toLowerCase(); System.out.print(s1);
  }
  public static void main(String[] s) {
    String s1 = "Ab";
    m1(s1); m2(s1);
    System.out.print(s1);
}}

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

a. Prints: AbAbAb
b. Prints: ABabab
c. Prints: ABabAb
d. Prints: ABAbAb
e. Prints: Ababab
f. Compile-time error
g. Run-time error
h. None of the above

Question 4

class SRC115 {
  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) {
    long seed = 1L;
    System.out.print(m(Math.random(seed)));
}}

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 5

import java.util.*;
class GFC112 {
  public static void main (String[] args) {
    Object m = new LinkedHashSet();
    System.out.print((m instanceof Collection)+",");
    System.out.print((m instanceof Set)+",");
    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 MWC212 {
  public static void main(String[] args) {
    int[] a1[],a2[];  // 1
    int []a3,[]a4;    // 2
    int []a5,a6[];    // 3
    int[] a7,a8[];    // 4
}}

A compile-time error is generated at which line?

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

Question 7

abstract class A {                   // 1
  abstract synchronized void m1();   // 2
  abstract synchronized class B {}   // 3
  synchronized 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 8

class ColorException extends Exception {}
class WhiteException extends ColorException {}
abstract class Color {
  abstract void m1() throws ColorException;
}
class White extends Color {
  void m1() throws WhiteException {throw new WhiteException();}
  public static void main (String[] args) {
    White white = new White();
    int a,b,c; a = b = c = 0;
    try {white.m1(); a++;} 
      catch (WhiteException e) {b++;}
      finally {c++;}
    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,1
c. Prints: 0,1,0
d. Prints: 0,1,1
e. Prints: 1,0,0
f. Prints: 1,0,1
g. Prints: 1,1,0
h. Prints: 1,1,1
i. Compile-time error
j. Run-time error
k. None of the above

Question 9

Which of the following is a modifier that can be applied to a method declaration within an interface?

a. static
b. synchronized
c. transient
d. volatile
e. native
f. None of the above

Question 10

class MWC113 {
  public static void main(String[] s) {
    String s1 = "1", s2 = "2", s3 = s1 + s2;
    s1 += s2; s3 += s1;
    System.out.println(s3);
}}

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

a. Prints: 3
b. Prints: 6
c. Prints: 33
d. Prints: 1212
e. Compile-time error
f. Run-time error
g. None of the above

Question 11

class SRC116 {
  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.sin(0.0f))+",");
    System.out.print(m(Math.cos(0.0f))+",");
    System.out.print(m(Math.tan(0.0f)));
}}

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

a. Prints: int,int,int
b. Prints: long,long,long
c. Prints: float,float,float
d. Prints: double,double,double
e. Compile-time error
f. Run-time error
g. None of the above

Question 12

import java.util.*;
class GFC113 {
  public static void main (String[] args) {
    Object m = new LinkedHashSet();
    System.out.print((m instanceof Collection)+",");
    System.out.print((m instanceof Set)+",");
    System.out.print(m instanceof HashSet);
}}

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 13

Which of the following are command-line switches used to enable assertions in non-system classes?

a. -ea
b. -ae
c. -aon
d. -aoff
e. -enableassertions
f. -assertionsenable
g. -assertionson
h. -assertionsoff

Question 14

class MCZ22 {
  public static void main (String[] args) {
    // Insert code here.
}}

Which of the following lines will generate a compile-time error if inserted at the specified location?

a. char a = 0x0041;
b. char b = '\u0041';
c. char c = 0101;
d. char d = -1;
e. char e = (char)-1;
f. None of the above

Question 15

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);
  }}
  void m1() {new A().new B();}  // 1
  void m2() {new A.B();}        // 2
  void m3() {new B();}          // 3
  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 16

Which of the following will not force a thread to move into the Not-Runnable state?

a.  Thread.yield method
b.  Thread.sleep method
c.  Thread.join method
d.  Object.wait method
e. By blocking on I/O
f. Unsuccessfully attempting to acquire the lock of an object
g. None of the above

Question 17

 Stores key/value pairs.
 Allows null elements, keys, and values.
 Duplicate entries replace old entries.
 Entries are not sorted using a Comparator or the Comparable interface.
 The iteration order is unspecified.

Which of these classes provides the specified features?

a. LinkedList
b. LinkedHashMap
c. LinkedHashSet
d. TreeMap
e. TreeSet
f. HashMap
g. HashSet
h. Hashtable
i. None of the above

Question 18

class C {
  int a, b, c;
  public void setA(int i) {a = i; assert validateC() : c;}
  public void setB(int i) {b = i; assert validateC() : c;}
  private boolean validateC() {
    return c > a + 2 * b;
  }
  public int m1(int i) {
    c = a + b + i;
    assert validateC() : c;
    return c;
  }
  public C(int i) {
    c = i; assert validateC() : c;
  }
  public static void main(String[] args) {
    C c = new C(251); c.setA(50); c.setB(100);
}}

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 class invariant--something that must be true about each instance of the class.
f. The assert statements are being used to check a precondition--something that must be true when the method is invoked.

Question 19

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
  }}
  static void m1() {new A().new B();}  // 2
  static void m2() {this.new B();}     // 3
  static void m3() {new B();}          // 4
  public static void main(String[] args) {
    m1(); m2(); m3();
}}

What are the results 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

Question 20

Which of the following will cause a dead thread to restart?

a.  Thread.yield method
b.  Thread.join method
c.  Thread.start method
d.  Thread.resume method
e. None of the above

Question 21

 Stores key/value pairs.
 Does not allow null elements, keys, and values.

Which of these classes provides the specified features?

a. LinkedList
b. LinkedHashMap
c. LinkedHashSet
d. TreeMap
e. TreeSet
f. HashMap
g. HashSet
h. Hashtable
i. None of the above

Question 22

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 = 1;
    try {
      throw new Level1Exception();
      try {
        switch (x) {
          case 1: throw new Level1Exception();
          case 2: throw new Level2Exception();
          case 3: throw new Level3Exception();
      } 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,1,1,0,0,1
b. Prints: 0,1,1,0,0,1
c. Prints: 0,1,0,0,0,0
d. Prints: 0,1,0,0,0,1
e. Prints: 0,0,1,0,0,1
f. Compile-time error
g. Run-time error
h. None of the above

Question 23

class JMM113 {
  public static void main (String[] args) {
    int i = 0, j = 0, k = 0;
    do while (i++ < 3)
      System.out.print(k++);
    while (j++ < 3);
}}

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 24

class SuperA {String s1="SuperA";}
class SuperB {String s1="SuperB";}
class A extends SuperA {
  String s1="A";
  class B extends SuperB {  // 1
    String s1="B";
    void m1() {
      System.out.print(this.s1 + ",");   // 2
      System.out.print(super.s1 + ",");  // 3
      System.out.print(A.this.s1 + ","); // 4
      System.out.print(A.super.s1);      // 5
    }
  }
  public static void main (String[] args) {
    new A().new B().m1();  // 6
}}

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

a. Prints: B,SuperB,B,SuperB
b. Prints: B,SuperB,A,SuperA
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. Run-time error
j. None of the above

Question 25

class JMM114 {
  public static void main (String[] args) {
    int i = 0, j = 0;
    while (i++ < 3) do
      System.out.print(j);
    while (j++ < 3);
}}

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

a. Prints: 0001
b. Prints: 001122
c. Prints: 012012
d. Prints: 012345
e. Prints: 1112
f. Prints: 111222
g. Prints: 121212
h. Run-time error
i. Compile-time error
j. None of the above

Question 26

class EBH023 {
  static String m1(boolean b){return b?"T":"F";}
  public static void main(String [] args) {
    boolean b1 = false?false:true?false:true?false:true;
    boolean b2 = false?false:(true?false:(true?false:true));
    boolean b3 = ((false?false:true)?false:true)?false:true;
    System.out.println(m1(b1) + m1(b2) + m1(b3));
}}

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

a. Prints: FFF
b. Prints: FFT
c. Prints: FTF
d. Prints: FTT
e. Prints: TFF
f. Prints: TFT
g. Prints: TTF
h. Prints: TTT
i. Run-time error
j. Compile-time error
k. None of the above

Question 27


class A {void m1() {System.out.print("A");}}
class B extends A {void m1(){System.out.print("B");}}
class C extends B {void m1() {System.out.print("C");}}
class D extends C {
  void m1() {System.out.print("D");}
  void m2() {
    m1();
    ((C)this).m1(); // 1
    ((B)this).m1(); // 2
    ((A)this).m1(); // 3
  }
  public static void main (String[] args) {
    new D().m2(); // 4
}}

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

a. Prints: DCBA
b. Prints: DDDD
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. Run-time error
h. None of the above

Question 28

class EBH021 {
  public static void main(String[] args) {
    System.out.print((-1 & 0x1f) + "," + (8 << -1));
}}

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

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


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