Certified Java Programmer Mock Exam


Question 1

class JSC102 {
  public static void main (String[] args) {
    private int x = 1;
    protected int y = 2;
    public int z = 3;
    System.out.println(x+y+z);
}}

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

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

Question 2

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

a. exit
b. strictfp
c. enum
d. super
e. abort
f. event
g. goto
h. native
i. exception

Question 3

class MCZ31 {
  public static void main (String[] args) {
    char a = '\t';  // 1
    char b = '\\';  // 2
    char c = '\"';  // 3
    char d = '\'';  // 4
    char e = '\?';  // 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 4

class E {
  static String m(int i) {return "int primitive";}
  static String m(Integer i) {return "Integer instance";}
  static String m(double i) {return "double primitive";}
  static String m(Double i) {return "Double instance";}
  static String m(String i) {return "String instance";}
  public static void main (String[] args) {
    System.out.print(m(Integer.parseInt("1")));
}}

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

a. Prints: int primitive
b. Prints: double primitive
c. Prints: Double instance
d. Prints: String instance
e. Compile-time error
f. Run-time error
g. None of the above

Question 5

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("1L")));
}}

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 6

Which of the following methods are static members of the java.lang.Double class?

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

Question 7

class D {
  public static void main (String[] args) {
    Boolean b1 = Boolean.valueOf("trUE");           // 1
    Boolean b2 = Boolean.valueOf("Even more true"); // 2
    Boolean b3 = Boolean.valueOf(null);             // 3
    System.out.print((b1==b2) + ",");
    System.out.print((b2==b3) + ",");
    System.out.println(b3==b1);
}}

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 8

class E {
  static String m(float f) {return "f";}
  static String m(Float f) {return "F";}
  public static void main (String[] args) {
    System.out.print(m(Float.parseFloat("1")));
    System.out.print(m(Float.floatValue()));
    System.out.print(m(Float.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 9

class E {
  static String m(short s) {return "p";}
  static String m(Short s) {return "S";}
  public static void main (String[] args) {
    Short s1 = new Short("1");
    System.out.print(m(s1.shortValue())+",");
    System.out.print(m(Short.parseShort("1"))+",");
    System.out.print(m(Short.valueOf("1")));
}}

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

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

Question 10

 Stores key/value pairs.
 Duplicate entries replace old entries.
 Entries are sorted using a Comparator or 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 11

import java.util.*;
class GFC104 {
  public static void main (String args[]) {
    LinkedList a1 = new LinkedList();
    ArrayList b1 = new ArrayList();
    Vector c1 = new Vector();
    System.out.print((a1 instanceof List)+",");
    System.out.print((b1 instanceof List)+",");
    System.out.print(c1 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 12

class A {
  static void m1 (B a, B b, B c, B d, B e, B f, B g, B h) {
    if (a.equals(b)) {System.out.print("A");}
    if (!c.equals(d)) {System.out.print("B");}
    if (e.hashCode() == f.hashCode()) {System.out.print("C");}
    if (g.hashCode() != h.hashCode()) {System.out.print("D");}
}}

Suppose that method m1 is invoked with eight instances of the same class and the output is ABCD. If the B.equals and B.hashCode methods are implemented according to the hash code contract, then which of the following statements must always be true?

a. (a.hashCode() == b.hashCode())
b. (c.hashCode() != d.hashCode())
c. (e.equals(f))
d. (!g.equals(h))

Question 13

Which of the following statements are true?

a. A final method can not be overridden.
b. All methods declared in a final class are implicitly final.
c. The methods declared in a final class must be explicitly declared final or a compile-time error occurs.
d. It is a compile-time error if a private method is declared final.
e. A machine-code generator can inline the body of a final method.

Question 14

abstract class A {}   // 1
transient class G {}  // 2
private class C {}    // 3
static class E {}     // 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 15

class Z {
  abstract class A {}     // 1
  final class B {}        // 2
  private class C {}      // 3
  protected class D {}    // 4
  public class E {}       // 5
}

Which class declaration results in a compile-time error?

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

Question 16

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 = 4;
    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: 0,0,0,0,0,1
b. Prints: 0,0,0,0,1,0
c. Prints: 0,0,1,0,0,1
d. Prints: 0,0,1,0,1,1
e. Prints: 0,1,1,1,1,1
f. Prints: 1,1,1,1,1,1
g. Compile-time error
h. Run-time error
i. None of the above

Question 17

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. final
c. private
d. protected
e. public

Question 18

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

a. '\u0000' to '\u7fff'
b. '\u0000' to '\uffff'
c. 0 to 32767
d. 0 to 65535
e. -32768 to 32767
f. -65536 to 65535

Question 19

class EBH011 {
  public static void main (String[] args) {
    float a = Float.POSITIVE_INFINITY;
    double b = Double.POSITIVE_INFINITY;
    double c = Double.NaN;
    System.out.print((a == b)+","+(c == c)+","+(c != c));
}}

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. Run-time error
j. Compile-time error
k. None of the above

Question 20

class A {
  public static void main(String[] args) {
    char a = 'a'; // 'a' = 97
    char b = 'b'; // 'b' = 98
    System.out.print(a + b + "" + a + b);
}}

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

a. Prints: 390
b. Prints: 195195
c. Prints: 195ab
d. Prints: ab195
e. Prints: abab
f. Run-time error
g. Compile-time error
h. None of the above

Question 21

class JMM105 {
  public static void main(String args[]) {
    int x = 6; 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;
      }
    } while ((x != 3) || (success < 2));
}}

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

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

Question 22

class C {
  String m1(int i) {
    switch (i) {
      case 0: return "A";
      case 1: return "B";
      case 2: return "C";
      default:
        assert false;
    }
    return "E";
  }
  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 enabled it prints ABCE followed by an AssertionError message.
c. With assertions disabled it prints ABC
d. With assertions disabled it prints ABCE
e. Assertions should not be used within the default case of a switch statement.

Question 23

class I {
  private String name;
  public I(String s) {name = s;}
  private I other;
  public void other(I i) {other = i;}
}
class J {
  private I i1 = new I("A"), i2 = new I("B"), i3 = new I("C");
  private void m1() {
    i1.other(i2); i2.other(i1); i3.other(i3);
    i1 = i3; i2 = i3;
    m2();
  }
  private void m2() {/* Do amazing things. */}
  public static void main (String[] args) {
    new J().m1();
}}

Which of the three objects, A, B or C, is not eligible for garbage collection when method m2 begins to execute?

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

Question 24

class GFM16 {
  static int m1 (int i1, int i2) {
    int i3;
    if (i1 > 0) {i3 = i1 + i2;}
    return i3;
  }
  public static void main(String[] args) {
    System.out.println(m1(1,2));
}}

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

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

Question 25

class UltraViolet {
  public static void main (String[] args) {
    char a = '\u002a'; // Asterisk, *
    char b = '\u0024'; // Dollar Sign, $
    System.out.print(a + b);           // 1
    System.out.print(" ABC" + a + b);  // 2
}}

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

a. Prints: 78 ABC*$
b. Prints: *$ ABC*$
c. Prints: 78 ABC78
d. Compile-time error
e. Run-time error
f. None of the above

Question 26

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

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

a. Prints: 123456789
b. Prints: 147258369
c. Prints: 321654987
d. Prints: 369258147
e. Run-time error
f. Compile-time error
g. None of the above

Question 27


abstract class A {
  private int x = 4;
  private int y = 2;
  public A(int i1, int i2) {x=i1;y=i2;}
  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 int math() {return x()+y();}};
  static A a2 = new A(2,1) {public int math() {return x()-y();}};
  static A a3 = new A(2,1) {public int math() {return x()*y();}};
  static A a4 = new A(2,1) {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: 8
b. Prints: 3122
c. Compile-time error
d. Run-time error
e. None of the above

Question 28

class EBH106 {
  public static void main(String args[]) {
    int a = 1;
    a += ++a + a++;
    System.out.print(a);
}}

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

a. Prints: 3
b. Prints: 4
c. Prints: 5
d. Prints: 6
e. Prints: 7
f. Run-time error
g. Compile-time error
h. None of the above


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