Certified Java Programmer Mock Exam


Question 1

Which of the following are true statements?

a. A top-level class can not be called "tightly encapsulated" unless it is declared private.
b. Encapsulation enhances the maintainability of the code.
c. A tightly encapsulated class allows fast public access to member fields.
d. A tightly encapsulated class allows access to data only through accessor and mutator methods.
e. Encapsulation usually reduces the size of the code.
f. A tightly encapsulated class might have mutator methods that validate data before it is loaded into the internal data model.

Question 2

Which of the following methods are static members of the Thread class?

a. join
b. notify
c. notifyAll
d. run
e. sleep
f. start
g. yield
h. wait

Question 3

class SRC103 {
  public static void main (String[] args) {
    int i1 = Math.round(0.5f);
    int i2 = Math.round(1.5f);
    System.out.print(i1 + "," + i2);
}}

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

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

Question 4

class B {
  public static void main (String args[]) {
    Byte b1 = new Byte(1);    // 1
    Byte b2 = new Byte('2');  // 2 
    Byte b3 = new Byte("3");  // 3
    byte i1 = b1.byteValue()+b2.byteValue()+b3.byteValue(); // 4
    System.out.print(i1);
}}

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

a. Prints: 6
b. Compile-time error at 1
c. Compile-time error at 2
d. Compile-time error at 3
e. Compile-time error at 4
f. Run-time error

Question 5

class A {
  public static void main (String args[]) {
    int primitiveInt = 1;
    long primitiveLong = 1L;
    float primitiveFloat = 1.0f;
    String s = "1";
    Integer i1 = new Integer(primitiveInt);
    Integer i2 = new Integer(primitiveLong);
    Integer i3 = new Integer(primitiveFloat);
    Integer i4 = new Integer(s);
    int i5 = i1.intValue() + i2.intValue() +
             i3.intValue() + i4.intValue();
    System.out.print(i5);
}}

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

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

Question 6

class B {
  public static void main (String args[]) {
    Long i1 = new Long(1);
    Long i2 = new Long(i1);
    System.out.print((i1==i2) + "," + 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 7

class A {
  public static void main (String args[]) {
    Double d1 = new Double(1.0);
    Double d2 = new Double(d1);
    System.out.print(d1.equals(d2));
}}

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

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

Question 8

class B {
  public static void main(String[] args) {
    Boolean b1 = new Boolean(true);
    Boolean b2 = new Boolean(true);
    Boolean b3 = new Boolean("TrUe");
    Boolean b4 = new Boolean("tRuE");
    System.out.print((b1==b2) + ",");
    System.out.print((b1.booleanValue()==b2.booleanValue()) + ",");
    System.out.println(b3.equals(b4));
}}

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 9

class F {
  public static void main (String[] args) {
    Float f1 = new Float('B' - 'A');  // 1
    Float f2 = new Float(010);        // 2
    Float f3 = new Float(0x10);       // 3                     
    Float f4 = new Float(.1e1);       // 4
    Float f5 = new Float(1.0);        // 5
    Float f6 = new Float(1.0d);       // 6
    System.out.print(f1+","+f2+","+f3+","+f4+","+f5+","+f6);
}}

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 at 5
f. Compile-time error at 6
g. Run-time error at 1
h. Run-time error at 2
i. Run-time error at 3
j. Run-time error at 4
k. Run-time error at 5
l. Run-time error at 6
m. Prints: 1.0,10.0,10.0,1.0,1.0,1.0
n. Prints: 1.0,8.0,16.0,1.0,1.0,1.0
o. None of the above

Question 10

Which of the following class instance creation expressions would generate a compile-time error?

a. new Short("1")
b. new Short("-1")
c. new Short("1.0")
d. new Short("0x1")
e. new Short("011")
f. None of the above

Question 11

 Each element must be unique.
 Duplicate elements must not replace old elements.
 Elements are not key/value pairs.
 Accessing an element can be almost as fast as performing a similar operation on an array.

Which of these classes provides the specified features?

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

Question 12

import java.util.*;
class GFC101 {
  public static void main (String args[]) {
    Object a1 = new HashMap(), b1 = new ArrayList();
    Object c1 = new HashSet();
    System.out.print((a1 instanceof Collection)+",");
    System.out.print((b1 instanceof Collection)+",");
    System.out.print(c1 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 13

If two instances of a class type are equal according to the equals method, then the same integer value must be returned by the hashCode method of the two objects.

a. false
b. true

Question 14

class MWC103 {
  public static void main(String[] args) {
    int[] a1 = {1,2,3};
    int []a2 = {4,5,6};
    int a3[] = {7,8,9};
    System.out.print(a1[1]+","+a2[1]+","+a3[1]);
}}

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

a. Prints: 12
b. Prints: 15
c. Prints: 18
d. Prints: 1,4,7
e. Prints: 2,5,8
f. Prints: 3,6,9
g. Compile-time error
h. Run-time error
i. None of the above

Question 15

Which of the following statements are true?

a. The compiler will create a default constructor if no other constructor is declared.
b. The default constructor takes no arguments.
c. If a class A has a direct superclass, then the default constructor of class A invokes the no-argument constructor of the superclass.
d. The default constructor declares Exception in the throws clause.
e. The default constructor is always given the private access modifier.
f. The default constructor is always given the public modifier.
g. The default constructor is always given default package access.

Question 16

Which of the following is used to prevent the serialization of a non-static field?

a. final
b. protected
c. synchronized
d. transient
e. volatile
f. native

Question 17

Which of the following modifiers can not be used with the abstract modifier in a method declaration?

a. final
b. private
c. protected
d. public
e. static
f. synchronized
g. native

Question 18

class JSC203 {
  static int m1(byte b) {return b;}   // 1
  static int m2(char c) {return c;}   // 2
  static int m3(long l) {return l;}   // 3
  public static void main(String[] args) {
    byte b = 1; char c = '\u0002'; long l = 4L;
    System.out.print(""+m1(b)+m2(c)+m3(l));
}}

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

a. Prints: 124
b. Prints: 7
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Run-time error

Question 19

public class A {int i1; void m1() {}}

Which of the following statements are true?

a. class A extends Object.
b. Field i1 is implicitly public, because class A is public.
c. Method m1 is implicitly public, because class A is public.
d. The compiler will insert a default constructor implicitly.
e. The default constructor has no throws clause.
f. The default constructor of class A has package access.
g. The default constructor accepts one parameter for each field in class A.
h. The default constructor invokes the no-parameter constructor of the superclass.

Question 20

Which of the following are class modifiers? Select all that are applicable to a top-level class and all that are applicable to a nested class. It is not required that a modifier be applicable to both.

a. abstract
b. extends
c. final
d. implements
e. private
f. protected
g. public
h. static
i. synchronized
j. transient
k. volatile

Question 21

class JMM103 {
  public static void main(String args[]) {
    for (int i = 0; i < 5 ;i++) {
      switch(i) {
        0: System.out.print("v ");break;
        1: System.out.print("w ");
        2: System.out.print("x ");break;
        3: System.out.print("y ");
        4: System.out.print("z ");break;
}}}}

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

a. Prints: v w x y z
b. Prints: v w x x y z z
c. Prints: v w w x y y z
d. Run-time error.
e. Compile-time error.
f. 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 {
      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: 0,0,0,1,0,0
b. Prints: 0,0,1,1,0,1
c. Prints: 0,1,1,1,0,1
d. Prints: 1,0,1,1,0,1
e. Prints: 1,1,1,1,0,1
f. Compile-time error
g. Run-time error
h. None of the above

Question 23

class A {}
class B extends A {}
class C extends B {}
class D {
  void m1(A a) {System.out.print("A");}
  void m1(B b) {System.out.print("B");}
  void m1(C c) {System.out.print("C");}
  public static void main(String[] args) {
    A c1 = new C(); B c2 = new C();
    C c3 = new C(); D d1 = new D();
    d1.m1(c1); d1.m1(c2); d1.m1(c3);
}}

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 MWC102 {
  public static void main (String[] args) {
    String s1 = "ABCDE";
    System.out.print(s1.substring(1,2)+s1.substring(3));
}}

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

a. Prints: AABC
b. Prints: ACDE
c. Prints: ABABC
d. Prints: ABCDE
e. Prints: BABCD
f. Prints: BDE
g. Prints: BCABCD
h. Prints: BCDE
i. Compile-time error
j. Run-time error
k. None of the above

Question 25

class MWC201 {
  public static void main (String[] args) {
    String s1 = new String("ABC"), s2 = new String("ABC");
    StringBuffer sb1 = new StringBuffer(s1);
    StringBuffer sb2 = new StringBuffer(s2);
    System.out.print(s1.equals(s2) + "," + sb1.equals(sb2));
}}

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 26

class MWC203 {
  public static void main(String[] args) {
    int[] a1[] = {new int[]{1,2},new int[]{3,4,5}};
    int []a2[] = new int[][]{{1,2},{3,4,5}};
    int a3[][] = {{1,2},new int[]{3,4,5}};
    System.out.print(a1[0][1]+","+a2[1][0]+","+a3[1][2]);
}}

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

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

Question 27

Which statements are true?

a. Assertions should not be used to validate arguments passed to public methods.
b. Assertions should not be used to validate arguments passed to nonpublic methods.
c. Assertions should not be used within the default case of a switch statement.
d. Assertions should not be used to perform processing that is required for the normal operation of the application.

Question 28

void m1() {
  Q q1 = null;
  for (int i = 0; i < 10; i++) {
    q1 = new Q();   // 1
    m2(q1);         // 2
  }
  System.out.print("All done"); // 3
}

When the processing of line 3 begins, how many objects of type Q that were created at line 1 are eligible for garbage collection?

a. 0
b. 1
c. 9
d. 10
e. Indeterminate.
f. Compile-time error
g. Run-time error
h. None of the above

Question 29


abstract class A {
  private int x = 4;
  private int y = 2;
  public int x() {return x;}
  public void x(int x) {this.x = x;}
  public int y() {return y;}
  public void y(int y) {this.y = y;}
  public abstract int math();
}
class B {
  static A a1 = new A() {public int math() {return x()+y();}}
  static A a2 = new A() {public int math() {return x()-y();}}
  static A a3 = new A() {public int math() {return x()*y();}}
  static A a4 = new A() {public int math() {return x()/y();}}
  public static void main(String[] args) {
    System.out.print("" + a1.math() + a2.math() +
                          a3.math() + a4.math());
}}

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

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


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