Certified Java Programmer Mock Exam


Question 1

class A {
  public static void main (String args[]) {
    byte primitiveByte = 1;            // 1
    Byte b1 = new Byte(primitiveByte); // 2
    Byte b2 = new Byte(1);             // 3 
    System.out.print(b1.byteValue() + b2.byteValue());
}}

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

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

Question 2

class A {
  public static void main (String args[]) {
    byte primitiveByte = 1;
    char primitiveChar = 'b'-'a';
    int primitiveInt = 1;
    short primitiveShort = 1;
    String s = "1";
    Integer i1 = new Integer(primitiveByte);
    Integer i2 = new Integer(primitiveChar);
    Integer i3 = new Integer(primitiveShort);
    Integer i4 = new Integer(primitiveInt);
    Integer i5 = new Integer(s);
    int p1 = i1.intValue() + i2.intValue() +
             i3.intValue() + i4.intValue() +
             i5.intValue();
    System.out.print(p1);
}}

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

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

Question 3

class A {
  public static void main (String args[]) {
    byte primitiveByte = 1;
    char primitiveChar = 1;
    double primitiveDouble = 1;
    String s = "1";
    Double d1 = new Double(primitiveByte);
    Double d2 = new Double(primitiveChar);
    Double d3 = new Double(primitiveDouble);
    Double d4 = new Double(s);
    double d5 = d1.doubleValue() + d2.doubleValue() +
                d3.doubleValue() + d4.doubleValue();
    System.out.print(d5);
}}

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

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

Question 4

class A {
  public static void main (String args[]) {
    byte primitiveByte = 1;
    int primitiveInt = 1;
    long primitiveLong = 1L;
    float primitiveFloat = 1f;
    String s = "1";
    Long i1 = new Long(primitiveByte);
    Long i2 = new Long(primitiveInt);
    Long i3 = new Long(primitiveLong);
    Long i4 = new Long(primitiveFloat);
    Long i5 = new Long(s);
    int i6 = i1.intValue() + i2.intValue() +
             i3.intValue() + i4.intValue() +
             i5.intValue();
    System.out.print(i6);
}}

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

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

Question 5

class C {
  public static void main (String[] args) {
    Float f1 = new Float(1.0);     // 1
    Float f2 = new Float("1.0");   // 2
    Float f3 = new Float("1.0f");  // 3
    Float f4 = new Float("1e1f");  // 4
    Float f5 = new Float(".1e1d"); // 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 6

class A {
  public static void main(String[] args) {
    Boolean b1 = new Boolean(true);    // 1
    Boolean b2 = new Boolean(false);   // 2
    Boolean b3 = new Boolean(TRUE);    // 3
    Boolean b4 = new Boolean(FALSE);   // 4
    Boolean b5 = new Boolean("TrUe");  // 5
    Boolean b6 = new Boolean("fAlSe"); // 6
}}

Compile-time errors are generated at which lines?

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

Question 7

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

a. new Short(1)
b. new Short('1')
c. new Short('b' - 'a')
d. new Short((short)1 - (short)2)
e. new Short((byte)1)
f. new Short((short)1)

Question 8

class B {
  public static void main (String args[]) {
    Byte b1 = new Byte(1);    // 1
    Byte b2 = new Byte('2');  // 2 
    Byte b3 = new Byte("3");  // 3
    byte i1 = b1.byteValue()+b2.byteValue()+b3.byteValue(); // 4
    System.out.print(i1);
}}

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

a. Prints: 6
b. Compile-time error at 1
c. Compile-time error at 2
d. Compile-time error at 3
e. Compile-time error at 4
f. Run-time error

Question 9

class A {
  public static void main (String args[]) {
    int primitiveInt = 1;
    long primitiveLong = 1L;
    float primitiveFloat = 1.0f;
    String s = "1";
    Integer i1 = new Integer(primitiveInt);
    Integer i2 = new Integer(primitiveLong);
    Integer i3 = new Integer(primitiveFloat);
    Integer i4 = new Integer(s);
    int i5 = i1.intValue() + i2.intValue() +
             i3.intValue() + i4.intValue();
    System.out.print(i5);
}}

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

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

Question 10

class A {
  public static void main (String args[]) {
    Double d1 = new Double(1.0);
    Double d2 = new Double(d1);
    System.out.print(d1.equals(d2));
}}

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 11

class B {
  public static void main (String args[]) {
    Long i1 = new Long(1);
    Long i2 = new Long(i1);
    System.out.print((i1==i2) + "," + 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 12

class F {
  public static void main (String[] args) {
    Float f1 = new Float('B' - 'A');  // 1
    Float f2 = new Float(010);        // 2
    Float f3 = new Float(0x10);       // 3                     
    Float f4 = new Float(.1e1);       // 4
    Float f5 = new Float(1.0);        // 5
    Float f6 = new Float(1.0d);       // 6
    System.out.print(f1+","+f2+","+f3+","+f4+","+f5+","+f6);
}}

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. Compile-time error at 6
g. Run-time error at 1
h. Run-time error at 2
i. Run-time error at 3
j. Run-time error at 4
k. Run-time error at 5
l. Run-time error at 6
m. Prints: 1.0,10.0,10.0,1.0,1.0,1.0
n. Prints: 1.0,8.0,16.0,1.0,1.0,1.0
o. None of the above

Question 13

class B {
  public static void main(String[] args) {
    Boolean b1 = new Boolean(true);
    Boolean b2 = new Boolean(true);
    Boolean b3 = new Boolean("TrUe");
    Boolean b4 = new Boolean("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. None of the above

Question 14

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

a. new Short("1")
b. new Short("-1")
c. new Short("1.0")
d. new Short("0x1")
e. new Short("011")
f. None of the above

Question 15

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 16

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 17

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 18

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


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