Certified Java Programmer Mock Exam


Question 1

class GFC404 {
  private static int x=1;
  static void m1(int x, int y) {x++; y++;}
  public static void main (String[] args) {
    int y=3; m1(x, y);
    System.out.println(x + "," + y);
}}

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

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

Question 2

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

a. All of the methods are declared private.
b. All of the methods are synchronized.
c. All local variables are declared final.
d. The class is a direct subclass of Object.
e. Accessor methods are used to prevent fields from being set with invalid data.
f. None of the above

Question 3

Which of the following methods name the InterruptedException in its throws clause?

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

Question 4

Which of the following are not methods of the java.lang.String class?

a. append
b. concat
c. delete
d. insert
e. replace
f. substring
g. valueOf

Question 5

class SRC105 {
  public static void main(String[] args) {
    double d1 = Math.ceil(0.5);
    double d2 = Math.ceil(1.5);
    System.out.print(d1 + "," + d2);
}}

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

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

Question 6

class E {
  public static void main (String[] args) {
    Byte b1 = new Byte("1"), b2 = new Byte("1");
    System.out.print((b1==b2)+","+(b1.equals(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 7

class B {
  public static void main (String args[]) {
    Integer a = new Integer(256);
    byte b = a.byteValue();
    short c = a.shortValue();
    int e = a.intValue();
    long f = a.longValue();
    float g = a.floatValue();
    double h = a.doubleValue();
    System.out.print(b+","+c+","+ (e+f+g+h == 4 * 256));
}}

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

a. Prints: 0,0,false
b. Prints: 0,0,true
c. Prints: 0,-1,false
d. Prints: 0,-1,true
e. Prints: -1,0,false
f. Prints: -1,0,true
g. Prints: -1,-1,false
h. Prints: -1,-1,true
i. Compile-time error
j. Run-time error
k. None of the above

Question 8

class F {
  static String m(long i) {return "long";}
  static String m(Long i) {return "Long";}
  static String m(double i) {return "double";}
  static String m(Double i) {return "Double";}
  static String m(String i) {return "String";}
  public static void main (String[] args) {
    System.out.print(m(Long.parseLong("1")));
}}

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

a. Prints: long
b. Prints: Long
c. Prints: double
d. Prints: Double
e. Prints: String
f. Compile-time error
g. Run-time error
h. None of the above

Question 9

class D {
  static boolean m(double v) {
    return(v != v == Double.isNaN(v));
  }
  public static void main (String args[]) {
    double d1 = Double.NaN;
    double d2 = Double.POSITIVE_INFINITY;
    double d3 = Double.MAX_VALUE;
    System.out.print(m(d1) + "," + m(d2) + "," + m(d3));
}}

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 10

class D {
  public static void main (String[] args) {
    Boolean b1 = new Boolean("trUE");          // 1
    Boolean b2 = new Boolean("What's This?");  // 2
    Boolean b3 = new Boolean(null);            // 3
    System.out.print(b1 + "," + b2 + "," + b3);
}}

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 11

class E {
  public static void main (String args[]) {
    String s1 = Float.toString(1.0);  // 1
    String s2 = Float.toString(1.0f); // 2
    String s3 = Float.toString(0xf);  // 3
    String s4 = Float.toString(010);  // 4
    String s5 = Float.toString('A');  // 5
}}

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. Run-time error at 1
g. Run-time error at 2
h. Run-time error at 3
i. Run-time error at 4
j. Run-time error at 5
k. None of the above

Question 12

class F {
  public static void main (String[] args) {
    Short s1 = new Short("1"), s2 = new Short("1");
    int a1 = s1.hashCode(), b1 = s2.hashCode();
    System.out.print((s1==s2)+","+(s1.equals(s2))+","+(a1==b1));
}}

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

a. false,false,false
b. false,false,true
c. false,true,false
d. false,true,true
e. true,false,false
f. true,false,true
g. true,true,false
h. true,true,true
i. Compile-time error
j. Run-time error
k. None of the above

Question 13

Which implementation of the List interface provides for the fastest insertion of a new element into the middle of the list?

a. Vector
b. ArrayList
c. LinkedList
d. None of the above

Question 14

import java.util.*;
class GFC103 {
  public static void main (String args[]) {
    Object a1 = new TreeSet();
    System.out.print((a1 instanceof Set)+",");
    System.out.print(a1 instanceof SortedSet);
}}

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. None of the above

Question 15

class A {
  private int[] val;
  private int hash;
  public int hashCode() {
    int h = hash;
    if (h == 0) {
      int len = val.length;
      for (int i = 0; i < len; i++) {
        h = 31*h + val[i];
      }
      hash = h;
    }
    return h;
  }
  // The equals method has been omitted for clarity.
  A (int[] val) {this.val = (int[])val.clone();}
  public static void main (String[] args) {
    A a = new A(new int[]{1,2,3});
    System.out.print(a.hashCode());
}}

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

a. Prints: 1026
b. Prints: 1091
c. Prints: 31806
d. Compile-time error
e. Run-time error
f. None of the above

Question 16

class A {A() throws Exception {}} // 1
class B extends A {B() throws Exception {}} // 2
class C extends A {} // 3

Which of the following statements is true?

a. Compile-time error at 1.
b. Compile-time error at 2.
c. Compile-time error at 3.
d. None of the above

Question 17

class JSC101 {
  void m1() {
    public int a;      // 1
    protected int b;   // 2
    private int c;     // 3
    static int d;      // 4
    transient int e;   // 5
    volatile int f;    // 6
    final int g = 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 18

Which of the following statements is not true?

a. A static method is also known as a class method.
b. A class method is not associated with a particular instance of the class.
c. The keyword, this, can not be used inside the body of a static method.
d. The keyword, super, may be used in the body of a static method.
e. A method that is not static is known as an instance method.
f. None of the above.

Question 19

class Black {
  public static void main(String args[]) {
    int[] i = {1,2,3,4,5};                  // 1
    long[] l = new long[5];                 // 2
    for (int j = 0; j < l.length(); j++) {  // 3
      l[j] = i[j];                          // 4
}}}

A compile-time error is generated at which line?

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

Question 20

class GFC218 {
  static void m(Object x) {System.out.print("Object");}
  static void m(String x) {System.out.print("String");}
  public static void main(String[] args) {
    m(null);
}}

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

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

Question 21

class P {
  static void printS1(){System.out.print("P.printS1 ");}
  void printS2() {System.out.print("P.printS2 ");}
  void printS1S2(){printS1();printS2();}
}
class Q extends P {
  static void printS1(){System.out.print("Q.printS1 ");}
  void printS2(){System.out.print("Q.printS2 ");}
  public static void main(String[] args) {
    new Q().printS1S2();
}}

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

a. Prints: P.printS1 P.printS2
b. Prints: P.printS1 Q.printS2
c. Prints: Q.printS1 P.printS2
d. Prints: Q.printS1 Q.printS2
e. Run-time error
f. Compile-time error
g. None of the above

Question 22

class D {
  D() {System.out.print("D");}
  class Z {Z(){System.out.print("Z");}}
  public static void main(String args[]) {
    new D.Z();
}}

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

a. Prints: D
b. Prints: Z
c. Prints: DZ
d. Prints: ZD
e. Run-time error
f. Compile-time error
g. 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 c1 = new C(); C c2 = new C(); c1.m1(c2);
}}

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

a. Prints: A
b. Prints: B
c. Prints: C
d. Compile-time error
e. Run-time error
f. None of the above

Question 24

class MWC203 {
  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);
    boolean b1 = s1.hashCode() == s2.hashCode();
    boolean b2 = sb1.hashCode() == sb2.hashCode();
    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 25

class MWC105 {
  static boolean b1;
  public static void main(String[] args) {
    boolean[] array = new boolean[1];
    boolean b2;
    System.out.print(b1+",");
    System.out.print(array[0]+",");
    System.out.print(b2);
}}

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

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

Question 26

class MWC205 {
  public static void main(String[] args) {
    int[][] a1 = {{1,2},{3,4,5},{6,7,8,9},{}};
    for (int i = 0; i < a1.length; i++) {
      System.out.print(a1[i].length+",");
}}}

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

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

Question 27

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(2,1) {
    public A(int i1, int i2) {x(i1);y(i2);};
    public int math() {return x()+y();}
  };
  public static void main(String[] args) {
    System.out.print(a1.math());
}}

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

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

Question 28

class GFC305 {
  static void m1(int[] i1, int[] i2) {
    int i = i1[0]; i1[0] = i2[0]; i2[0] = i;
  }
  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: 1,1
b. Prints: 1,3
c. Prints: 3,1
d. Prints: 3,3
e. Run-time error
f. Compile-time error
g. None of the above


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