Certified Java Programmer Mock Exam


Question 1

Which of these words belong to the set of Java keywords?

a. Double
b. goto
c. min
d. extern
e. new
f. signed
g. finally
h. const
i. and
j. array
k. do

Question 2

class MCZ13 {
  public static void main (String[] args) {
    String s = null;
    System.out.print(s);
}}

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

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

Question 3

Suppose that an interface, I1, is not a member of an enclosing class or interface. Which of the following modifiers can be applied to interface I1?

a. abstract
b. public
c. static
d. synchronized
e. transient
f. volatile

Question 4

Which of the following represent the full range of type char?

a. -0x8000 to 0x7fff
b. 0x0000 to 0xffff
c. -0100000 to 077777
d. 0 to 0177777
e. 0 to 32767
f. 0 to 65535
g. -32768 to 32767

Question 5

class EBH012 {
  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 6

class Black {
  public static void main(String[] args) {
    short s1 = 1;        //1
    char c1 = 1;         //2
    byte b1 = s1;        //3
    byte b2 = c1;        //4
    final short s2 = 1;  //5
    final char c2 = 1;   //6
    byte b3 = s2;        //7
    byte b4 = c2;        //8
}}

Compile-time errors are generated at which lines?

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

Question 7

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

a. All member fields are declared final.
b. The class is not anonymous.
c. The internal data model can be read and modified only through accessor and mutator methods.
d. The class is an inner class.
e. None of the above

Question 8

Which of the following is a checked exception?

a. IllegalMonitorStateException
b. IllegalThreadStateException
c. IllegalArgumentException
d. InterruptedException
e. None of the above

Question 9

class MWC106 {
  static void m1(String s) {
    s = s.trim(); s = s.concat("D");
  }
  public static void main(String[] s) {
    String s1 = "A", s2 = " B ", s3 = "C";
    m1(s2);
    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 10

class SRC109 {
  public static void main (String[] args) {
    short x3 = 0; short x4 = 1;
    short b1 = Math.min(x3,x4); // 1
    int c1 = Math.min(0,1);     // 2
    long d1 = Math.min(0L,+1L); // 3
    System.out.print(b1+","+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 11

class C {
  public static void main (String args[]) {
    String s1 = "1", s2 = "2";
    Byte b1 = Byte.parseByte(s1);
    Byte b2 = Byte.parseByte(s2);
    System.out.print(b1.byteValue() + b2.byteValue());
}}

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

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

Question 12

class A {
  public static void main (String[] args) {
    String s = "11";
    int i1 = Integer.parseInt(s);
    System.out.print(new Integer(i1).equals(new Integer(i1)) + ",");
    System.out.print(new Integer(i1).equals(new Integer(s))  + ",");
    System.out.print(new Integer(i1) == new Integer(i1));
}}

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 13

Which of the method invocation expressions would produce a run-time error?

a. Long.parseLong("1")
b. Long.parseLong("1L")
c. Long.parseLong("010")
d. Long.parseLong("0x10")
e. Long.parseLong("1.0")

Question 14

Which method of the java.lang.Double class returns an instance of type Double?

a. doubleValue
b. floatValue
c. intValue
d. longValue
e. parseDouble
f. toString(double)
g. valueOf
h. None of the above

Question 15

 Elements are not key/value pairs.
 Duplicate elements are not accepted.
 The entries can be sorted using the Comparable interface.

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 16

import java.util.*;
class GFC106 {
  public static void main (String args[]) {
    Object a = new HashSet(), b = new HashMap();
    Object c = new Hashtable();
    System.out.print((a instanceof Map)+",");
    System.out.print((b instanceof Map)+",");
    System.out.print(c instanceof Map);
}}

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

Which of the following classes override both the equals and hashCode methods?

a. java.lang.Byte
b. java.lang.Integer
c. java.util.Vector
d. java.lang.String
e. java.lang.StringBuffer

Question 18

class JSC104{
  public float a;        // 1
  protected float b;     // 2
  private float c;       // 3
  static float d;        // 4
  synchronized float 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 19

class C {
  String m1(int i) {
    switch (i) {
      case 0: return "A";
      case 1: return "B";
      case 2: return "C";
      default:
        assert false;
    }
  }
  public static void main(String[] args) {
    C c = new C();
    for (int i = 0; i < 4; i++) {
      System.out.print(c.m1(i));
}}}

Which statements are true?

a. With assertions enabled it prints ABC followed by an AssertionError message.
b. With assertions disabled it prints ABC followed by an AssertionError message.
c. Assertions should not be used within the default case of a switch statement.
d. In this code example a throw statement must be used in place of the assert statement.
e. Compile-time error

Question 20

class I {
  private String name;
  public void finalize() {System.out.print(name);}
  public I(String s) {name = s;}
}
class J {
  private static void m1(I[] a1) {
    a1[0] = a1[1] = a1[2] = 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);
    System.gc();
}}

After method m1 returns, the object created on which line is not eligible for garbage collection?

a. 1
b. 2
c. 3
d. 4
e. None of the above
f. Compile-time error
g. Run-time error

Question 21

class GFM17 {
  int x;                                    // 1
  public static void main(String[] args) {  // 2
    int y = 0;                              // 3
    System.out.print(x+",");                // 4
    System.out.print(y);                    // 5
}}

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

a. Prints: 0,0
b. Compile-time error at line 1.
c. Compile-time error at line 1.
d. Compile-time error at line 2.
e. Compile-time error at line 3.
f. Compile-time error at line 4.
g. Compile-time error at line 5.
h. Run-time error
i. None of the above

Question 22

class GFC205 {}
class GFC206 extends GFC205 {}
class GFC207 extends GFC206 {
  static void m(GFC205 x, GFC205 y) {System.out.print("GFC205,GFC205");}
  static void m(GFC205 x, GFC206 y) {System.out.print("GFC205,GFC206");}
  static void m(GFC206 x, GFC205 y) {System.out.print("GFC206,GFC205");}
  static void m(GFC206 x, GFC206 y) {System.out.print("GFC206,GFC206");}
  public static void main(String[] args) {
    GFC207 gfc207 = new GFC207();
    m(gfc207, gfc207);
}}

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

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

Question 23


interface I {String s1 = "I";}
class A implements I {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {
  String s1 = "C";
  void printIt() {
    System.out.print(((A)this).s1 + ((B)this).s1 +
                     ((C)this).s1 + ((I)this).s1);
  }
  public static void main (String[] args) {
    new C().printIt();
}}

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

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

Question 24

class G {
  final String s1 = "G.s1";
  class Z {
    String s1;
    void m1() {System.out.println(???);}
  }
  public static void main(String args[]) {
    G g = new G(); g.new Z().m1();
}}

Which name or expression could be used in place of ??? to cause the program to print "G.s1"?

a. s1
b. G.s1
c. ((G)this).s1
d. G.this.s1
e. G.super.s1
f. None of the above

Question 25

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 A();
    C c1 = new A(); C 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 26

class EBH107 {
  static int m(int i) {
    System.out.print(i + ",");
    return i;
  }
  public static void main(String s[]) {
    int i=0;
    int j = m(++i) + m(++i) * m(++i) % m(++i) + m(++i);
    System.out.print(j%5);
}}

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

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

Question 27

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

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 A13 {}
class A14 {
  public static void main(String[] arg) {
    A13[]     a1 = new A13[1];         // 1
    A13[][]   a2 = new A13[2][1];      // 2
    A13[][][] a3 = new A13[3][3][3];   // 3
    System.out.print(a3[2][2][2]);     // 4
    a1[0] = new A13();                 // 5
    a2[0] = a2[1] = a1;                // 6
    a3[0] = a3[1] = a3[2] = a2;        // 7
    System.out.print(a3[2][2][2]);     // 8
}}

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

a. Prints: null
b. Prints: nullnull
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. Compile-time error at 8.
k. Run-time error


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