Certified Java Programmer Mock Exam


Question 1

class SRC109 {
  public static void main (String[] args) {
    short x3 = 0; short x4 = 1;
    short b1 = Math.min(x3,x4); // 1
    int c1 = Math.min(0,1);     // 2
    long d1 = Math.min(0L,+1L); // 3
    System.out.print(b1+","+c1+","+d1);
}}

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

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

Question 2

class C {
  public static void main (String args[]) {
    String s1 = "1", s2 = "2";
    Byte b1 = Byte.parseByte(s1);
    Byte b2 = Byte.parseByte(s2);
    System.out.print(b1.byteValue() + b2.byteValue());
}}

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

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

Question 3

class A {
  public static void main (String[] args) {
    String s = "11";
    int i1 = Integer.parseInt(s);
    System.out.print(new Integer(i1).equals(new Integer(i1)) + ",");
    System.out.print(new Integer(i1).equals(new Integer(s))  + ",");
    System.out.print(new Integer(i1) == new Integer(i1));
}}

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 4

Which of the method invocation expressions would produce a run-time error?

a. Long.parseLong("1")
b. Long.parseLong("1L")
c. Long.parseLong("010")
d. Long.parseLong("0x10")
e. Long.parseLong("1.0")

Question 5

Which method of the java.lang.Double class returns an instance of type Double?

a. doubleValue
b. floatValue
c. intValue
d. longValue
e. parseDouble
f. toString(double)
g. valueOf
h. None of the above

Question 6

class MWC107 {
  public static void main(String[] s) {
    String s1 = "A", s2 = " B ", s3 = "C";
    s2.trim(); s3.concat("D");
    System.out.print(s1 + s2 + s3);
}}

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

a. Prints: ABC
b. Prints: A B C
c. Prints: ABCD
d. Prints: ABDC
e. Prints: A B CD
f. Prints: A B DC
g. Compile-time error
h. Run-time error
i. None of the above

Question 7

class SRC110 {
  public static void main (String[] args) {
    byte x1 = 0; byte x2 = 1;
    byte a1 = Math.min(x1,x2);  // 1
    int c1 = Math.min(0,1);     // 2
    long d1 = Math.min(0L,+1L); // 3
    System.out.print(a1+","+c1+","+d1);
}}

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

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

Question 8

class B {
  public static void main (String[] args) {
    byte b1 = 11;
    System.out.print(new Integer(b1).equals(new Integer(b1)) + ",");
    System.out.print(new Integer(b1).equals(new Short(b1)) + ",");
    System.out.print(new Integer(b1).equals(new Byte(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 9

Which of the method invocation expressions would produce a run-time error?

a. Long.parseLong("-1")
b. Long.parseLong("+1")
c. Long.parseLong("01")
d. Long.parseLong("1L")
e. Long.parseLong("1.0")

Question 10

Which methods of the java.lang.Double class declare a parameter of type String?

a. doubleValue
b. floatValue
c. intValue
d. longValue
e. parseDouble
f. valueOf

Question 11

class MWC108 {
  public static void main(String[] s) {
    String s1 = "A", s2 = " B ", s3 = "C";
    s2.trim(); s3.append("D");
    System.out.print(s1 + s2 + s3);
}}

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

a. Prints: ABC
b. Prints: A B C
c. Prints: ABCD
d. Prints: ABDC
e. Prints: A B CD
f. Prints: A B DC
g. Compile-time error
h. Run-time error
i. None of the above

Question 12

class SRC111 {
  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(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,b))+",");
    System.out.print(m(Math.min(s,s))+",");
    System.out.print(m(Math.min(c,c)));
}}

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

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

Question 13

Which of the following methods of the java.lang.Integer class returns a primitive int type?

a. intValue
b. parseInt
c. valueOf

Question 14

Which methods of the java.lang.Double class declare a NumberFormatException in the throws clause?

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

Question 15

class SRC112 {
  static String m(byte i) {return "byte";}
  static String m(int i) {return "int";}
  static String m(long i) {return "long";}
  static String m(double i) {return "double";}
  public static void main (String[] args) {
    byte b = 0;
    System.out.print(m(Math.min(b,b))+",");
    System.out.print(m(Math.min(b,1))+",");
    System.out.print(m(Math.min(b,1L)));
}}

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

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

Question 16

class F {
  public static void main (String[] args) {
    Integer i1 = new Integer("1");
    Integer i2 = new Integer("1");
    int h1 = i1.hashCode(), h2 = i2.hashCode();
    StringBuffer sb1 = new StringBuffer("1");
    StringBuffer sb2 = new StringBuffer("1");
    int h3 = sb1.hashCode(), h4 = sb2.hashCode();
    System.out.print((h1==h2)+","+(h3==h4));
}}

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 17

class F {
  public static void main (String args[]) {
    Double d1 = new Double("0x10");         // 1
    double d2 = Double.parseDouble("0x10"); // 2
    Double d3 = Double.valueOf("0x10");     // 3
    System.out.print(d1+","+d2+","+d3);
}}

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

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

Question 18

class MWC110 {
  static void m1(String s1, String s2) {
    s1.insert(1,"D"); s1 = s1 + s2;
  }
  public static void main(String[] s) {
    String s1 = "A", s2 = "B";
    m1(s1,s2);
    System.out.print(s1 + s2);
}}

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

a. Prints: AB
b. Prints: ABB
c. Prints: ADB
d. Prints: ADBB
e. Compile-time error
f. Run-time error
g. None of the above

Question 19

class MWC109 {
  public static void main(String args[]) {
    String a = "A", b = "B", c = a+b, d = a+b;
    System.out.print(((a+b)==(a+b)) + ",");
    System.out.print((c==d) + ",");
    System.out.print(c.equals(d));
}}

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


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