Certified Java Programmer Mock Exam


Question 1

class Black {
  public static void main(String[] args) {
    short s1 = 1;        //1
    char c1 = 1;         //2
    byte b1 = s1;        //3
    byte b2 = c1;        //4
    final short s2 = 1;  //5
    final char c2 = 1;   //6
    byte b3 = s2;        //7
    byte b4 = c2;        //8
}}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8

Question 2

class Magenta {
  static byte a = (byte)127;
  static byte b = (byte)128;
  static byte c = (byte)255;
  static byte d = (byte)256;
  public static void main(String args[]) {
    System.out.print(a + " " + b + " " + c + " " + d);
}}

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

a. Prints: 127 128 255 256
b. Prints: 127 128 255 0
c. Prints: 127 -1 -127 0
d. Prints: 127 -128 -1 0
e. Run-time error
f. Compile-time error
g. None of the above

Question 3

class Primitives {
  static void printFloat(float f) {System.out.println(f);}
  static void printDouble(double d) {System.out.println(d);}
  public static void main(String[] args) {
    byte b = 1;     // 1
    short s = b;    // 2
    char c = s;     // 3
    int i = c;      // 4
    long l = i;     // 5
    float f = l;    // 6
    printFloat(i);  // 7
    printFloat(l);  // 8
    printDouble(l); // 9
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. 8
i. 9
j. None of the above

Question 4

class Maroon {
  public static void main (String[] args) {
    int a = 1;    // 1
    short b = 1;  // 2
    long c = 1;   // 3
    a = c + a;    // 4
    c = b + a;    // 5
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above

Question 5

class Teal {
  public static void main (String[] args) {
    byte b = 1;    // 1
    long l = 1000; // 2
    b += l;        // 3
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. None of the above

Question 6

class Sienna {
  static double a;
  static float b;
  static int c;
  static char d;
  public static void main(String[] args) {
    a = b = c = d = 'a';
    System.out.println(a+b+c+d == 4 * 'a');
}}

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

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

Question 7

class UltraViolet {
  public static void main (String[] args) {
    char a = '\u002a'; // Asterisk, *
    char b = '\u0024'; // Dollar Sign, $
    System.out.print(a + b);           // 1
    System.out.print(" ABC" + a + b);  // 2
}}

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

a. Prints: 78 ABC*$
b. Prints: *$ ABC*$
c. Prints: 78 ABC78
d. Compile-time error
e. Run-time error
f. None of the above


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