Java Programmer Certification Mock Exam

Please Help Save a Tree!

The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer toner, and save money. If you prefer to work my exams from printed pages, then you can save a tree, save your printer toner, and save money if you buy my new book instead of attempting to print the pages of my web site.

Today, you can find my book on the retail web site of the company that prints it and distributes it.


Question 1

class C {
  public static void main (String args[]) {
    Long a = new Long(1);
    byte b = a.byteValue();
    short s = a.shortValue();
    char c = a.charValue();
    int d = a.intValue();
    long e = a.longValue();
    float f = a.floatValue();
    double g = a.doubleValue();
    System.out.print(b+s+c+d+e+f+g);
}}

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

a. Prints: 7
b. Prints: 7L
c. Prints: 7.0
d. Compile-time error
e. Run-time error
f. None of the above

Question 2

class B {
  public static void main (String args[]) {
    Double a = new Double(0xFFFF);
    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 * 0xFFFF));
}}

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

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

Question 3

Which of the instance creation expressions produce a run-time error?

a. new Float('A')
b. new Float("A")
c. new Float(1L)
d. new Float("1L")
e. new Float(0x10)
f. new Float("0x10")
g. new Float("010")

Question 4

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 5

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 6

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 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 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 11

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 12

class E {
  public static void main (String[] args) {
    Byte b1 = new Byte("1"), b2 = new Byte("1");
    int a = b1.hashCode(), b = b2.hashCode();
    System.out.print((b1.equals(b2))+","+(a==b));
}}

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 13

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 14

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 15

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 16

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 17

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 18

class E {
  static String m1(boolean b) {return "b";}
  static String m1(Boolean b) {return "B";}
  public static void main(String[] args) {
    Boolean b1 = new Boolean(true);
    System.out.print(m1(Boolean.valueOf(null)));
    System.out.print(m1(b1.booleanValue()));
    System.out.println(m1(Boolean.TRUE));
}}

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

a. Prints: bbb
b. Prints: bbB
c. Prints: bBb
d. Prints: bBB
e. Prints: Bbb
f. Prints: BbB
g. Prints: BBb
h. Prints: BBB
i. Compile-time error
j. Run-time error
k. None of the above


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