Dan Chisholm's
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 cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.

Today, you can find my book at amazon.com.

If you have any questions or comments concerning my mock exams or my book, then please send an e-mail to me at scjpexam2000@yahoo.com.


Question 1

class SRC104 {
  public static void main (String[] args) {
    System.out.print(Math.round(Float.NaN));
}}

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

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

Question 2

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

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

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

Question 3

class A {
  public static void main (String args[]) {
    Integer i1 = new Integer(1);
    Integer i2 = new Integer(i1);
    System.out.print(i1.equals(i2));
}}

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 4

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 5

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 6

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 7

class C {
  public static void main(String[] args) {
    Boolean b1 = Boolean.valueOf(true);
    Boolean b2 = Boolean.valueOf(true);
    Boolean b3 = Boolean.valueOf("TrUe");
    Boolean b4 = Boolean.valueOf("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. Compile-time error
j. Run-time error
k. None of the above

Question 8

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

a. new Short("1")
b. new Short("-1")
c. new Short("+1")
d. new Short("1.0")
e. new Short("0x1")
f. new Short("011")

Question 9

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 10

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 11

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 12

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 13

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 14

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 15

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 16

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 17

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 18

class MWC104 {
  public static void main (String[] args) {
    char[] c = {'a','b','c','d'};
    String s1 = new String(c);
    boolean b = true;
    for (int i = 0; i < s1.length; i++) {
      b &= (c[i] == s1.charAt(i));
    }
    System.out.print(b);
}}

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 19

class MWC202 {
  public static void main (String[] args) {
    StringBuffer sb1 = new StringBuffer("ABC");
    StringBuffer sb2 = new StringBuffer("ABC");
    System.out.print((sb1==sb2)+","+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 20

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


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