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 SRC113 {
  static String m(byte i) {return "byte";}
  static String m(short i) {return "short";}
  static String m(char i) {return "char";}
  static String m(int i) {return "int";}
  static String m(long i) {return "long";}
  static String m(float i) {return "float";}
  static String m(double i) {return "double";}
  public static void main (String[] args) {
    byte b = 0; short s = 0; char c = 0;
    System.out.print(m(Math.min(b,1L))+",");
    System.out.print(m(Math.min(s,1.0f))+",");
    System.out.print(m(Math.min(c,1.0)));
}}

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

a. Prints: int,int,int
b. Prints: byte,short,char
c. Prints: long,float,double
d. Prints: double,double,double
e. Prints: long,long,long
f. Compile-time error
g. Run-time error
h. None of the above

Question 2

class G {
  public static void main (String[] args) {
    Integer i1 = new Integer("1"), i2 = new Integer("1");
    StringBuffer sb1 = new StringBuffer("1");
    StringBuffer sb2 = new StringBuffer("1");
    System.out.print(sb1.equals(sb2)+","+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 3

class MWC111 {
  static void m1(String s1) {
    s1.replace('A','Y'); System.out.print(s1);
  }
  static void m2(String s1) {
    s1 = s1.replace('A','Z'); System.out.print(s1);
  }
  public static void main(String[] s) {
    String s1 = "A"; m1(s1); m2(s1);
    System.out.print(s1);
}}

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

a. Prints: AAA
b. Prints: YZA
c. Prints: YAA
d. Prints: AZA
e. Prints: AZZ
f. Compile-time error
g. Run-time error
h. None of the above

Question 4

class SRC114 {
  static String m(int i) {return "int";}
  static String m(long i) {return "long";}
  static String m(float i) {return "float";}
  static String m(double i) {return "double";}
  public static void main (String[] args) {
    System.out.print(m(Math.random()));
}}

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

a. Prints: int
b. Prints: long
c. Prints: float
d. Prints: double
e. Compile-time error
f. Run-time error
g. None of the above

Question 5

class H {
  public static void main (String[] args) {
    int i1, i2; Integer i3, i4;
    i1=new Integer(Integer.parseInt("1")).intValue();     // 1
    i3=new Integer(Integer.parseInt("1")).intValue();     // 2
    i2=Integer.parseInt(Integer.valueOf("1").toString()); // 3
    i4=Integer.parseInt(Integer.valueOf("1").intValue()); // 4
}}

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
f. Run-time error

Question 6

class MWC112 {
  static void m1(String s1) {
    s1 = s1.toUpperCase(); System.out.print(s1);
  }
  static void m2(String s1) {
    s1.toLowerCase(); System.out.print(s1);
  }
  public static void main(String[] s) {
    String s1 = "Ab";
    m1(s1); m2(s1);
    System.out.print(s1);
}}

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

a. Prints: AbAbAb
b. Prints: ABabab
c. Prints: ABabAb
d. Prints: ABAbAb
e. Prints: Ababab
f. Compile-time error
g. Run-time error
h. None of the above

Question 7

class SRC115 {
  static String m(int i) {return "int";}
  static String m(long i) {return "long";}
  static String m(float i) {return "float";}
  static String m(double i) {return "double";}
  public static void main (String[] args) {
    long seed = 1L;
    System.out.print(m(Math.random(seed)));
}}

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

a. Prints: int
b. Prints: long
c. Prints: float
d. Prints: double
e. Compile-time error
f. Run-time error
g. None of the above

Question 8

class MWC113 {
  public static void main(String[] s) {
    String s1 = "1", s2 = "2", s3 = s1 + s2;
    s1 += s2; s3 += s1;
    System.out.println(s3);
}}

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

a. Prints: 3
b. Prints: 6
c. Prints: 33
d. Prints: 1212
e. Compile-time error
f. Run-time error
g. None of the above

Question 9

class SRC116 {
  static String m(int i) {return "int";}
  static String m(long i) {return "long";}
  static String m(float i) {return "float";}
  static String m(double i) {return "double";}
  public static void main (String[] args) {
    System.out.print(m(Math.sin(0.0f))+",");
    System.out.print(m(Math.cos(0.0f))+",");
    System.out.print(m(Math.tan(0.0f)));
}}

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

a. Prints: int,int,int
b. Prints: long,long,long
c. Prints: float,float,float
d. Prints: double,double,double
e. Compile-time error
f. Run-time error
g. None of the above

Question 10

class MWC114 {
  public static void main(String[] s) {
    String s1 = new String("ABCDEFG"), s2 = new String("EFGHIJ");
    String s3 = s1.substring(4,7), s4 = s2.substring(0,3);
    System.out.println((s3 == s4) + "," + (s3 + s4).equals(s4 + s3));
}}

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 11

class SRC117 {
  public static void main (String[] args) {
    double d1 = Math.random();
    boolean b1 = (d1 < 0.0), b2 = (d1 <= 0.0), b3 = (d1 == 0.0);
    boolean b4 = (d1 >= 0.0), b5 = (d1 < 1.0), b6 = (d1 <= 1.0);
    boolean b7 = (d1 == 1.0), b8 = (d1 >= 1.0), b9 = (d1 > 1.0);
}}

Which of the boolean variables will never be initialized to the value true?

a. b1
b. b2
c. b3
d. b4
e. b5
f. b6
g. b7
h. b8
i. b9

Question 12

class MWC115 {
  public static void main(String[] s) {
    String s1 = new String("ABCDEFG"), s2 = s1.substring(0,3);
    String s3 = s1.substring(4,6); char c1 = s1.charAt(3);
    System.out.println(s2.concat(String.valueOf(c1)).concat(s3));
}}

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

a. Prints: "ABCDEFG"
b. Prints: "ABCEFG"
c. Prints: "ABCDDEFG"
d. Prints: "ABCDEF"
e. Prints: "ABCEF"
f. Prints: "ABCDDEF"
g. Compile-time error
h. Run-time error
i. None of the above

Question 13

Which method of the java.lang.Math class is not static?

a. abs
b. ceil
c. floor
d. max
e. min
f. random
g. round
h. sin
i. cos
j. tan
k. sqrt
l. None of the above

Question 14

Some of the following method names are overloaded and some are not. Which of the following method names are overloaded such that for each of the following four primitive types int, long, float and double there is at least one corresponding method that returns that type?

a. abs
b. ceil
c. floor
d. max
e. min
f. random
g. round
h. sin
i. cos
j. tan
k. sqrt

Question 15

class SRC118 {
  static String m1(int i) {return "I";}
  static String m1(long i) {return "L";}
  static String m1(float i) {return "F";}
  static String m1(double i) {return "D";}
  public static void main (String[] args) {
    System.out.print(m1(Math.abs(1.0)) + m1(Math.ceil(1.0f)));
    System.out.print(m1(Math.max(1.0,1.0)) + m1(Math.round(1.0)));
    System.out.print(m1(Math.sin(1.0)) + m1(Math.sqrt(1.0)));
}}

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

a. Prints: DDDDDD
b. Prints: LLLLLL
c. Prints: DLLLDD
d. Prints: DIDLDD
e. Prints: DLDLDD
f. Prints: DDDLDD
g. Prints: DIDIDD
h. None of the above

Question 16

class MWC118 {
  public static void main(String[] args) {
    byte[] b = {'a', 'b', 'c'};
    char[] c = {'a', 'b', 'c'};
    String s = "abc";
    StringBuffer sb = new StringBuffer("abc");
    byte b2 = 'a';
    char c2 = 'a';
    String s1 = new String(b);   // 1
    String s2 = new String(c);   // 2
    String s3 = new String(s);   // 3
    String s4 = new String(sb);  // 4
    String s5 = new String(b2);  // 5
    String s6 = new String(c2);  // 6
}}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6

Question 17

class SRC119 {
  static String m1(int i) {return "I";}
  static String m1(long i) {return "L";}
  static String m1(float i) {return "F";}
  static String m1(double i) {return "D";}
  public static void main (String[] args) {
    System.out.print(m1(Math.floor(1.0f)) + m1(Math.floor(1.0d)));
    System.out.print(m1(Math.ceil(1.0f)) + m1(Math.ceil(1.0d)));
}}

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

a. Prints: IIII
b. Prints: ILIL
c. Prints: LLLL
d. Prints: FDFD
e. Prints: DDDD
f. None of the above

Question 18

class MWC116 {
  public static void main(String[] args) {
    String s1 = " B C D E ";
    System.out.print('A' + s1.trim() + 'F');
}}

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

a. Prints: ABCDEF
b. Prints: A B C D E F
c. Prints: A BCDEF
d. Prints: ABCDE F
e. Prints: AB C D EF
f. Compile-time error
g. Run-time error
h. None of the above

Question 19

class MWC117 {
  public static void main (String[] args) {
    System.out.print(String.valueOf(1) + String.valueOf(2));
    String s1 = "S1";
    String s2 = s1.toString();
    System.out.print("," + (s1==s2));
}}

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

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


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