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 A {
  public static void main(String[] args) {
    char a = 'a', b = 'b'; // 'a' = 97, 'b' = 98
    System.out.print(a + b + "" + a + b);
}}

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

a. Prints: 390
b. Prints: 195195
c. Prints: 195ab
d. Prints: ab195
e. Prints: abab
f. Run-time error
g. Compile-time error
h. None of the above

Question 2

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 3

class Magenta {
  static byte a = (byte)127, b = (byte)128, c = (byte)255, 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 4

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Red {
  public static void main(String args[]) {
    Sub s1 = new Sub();
    I2 i2 = s1;          // 1
    I1 i1 = s1;          // 2
    Base base = s1;      // 3
    Sub s2 = (Sub)base;  // 4
}}

A compile-time error is generated at which line?

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

Question 5

class Green {
  public static void main (String args[]) {
    int[] i = null;  // 1
    Cloneable c = i; // 2
    i = (int [])c;   // 3
}}

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

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

Question 6

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 7

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Orange {
  public static void main(String args[]) {
    Base base = new Base();
    I1 i1 = base;           // 1
    Sub sub = (Sub)base;    // 2
}}

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

a. Compile-time error at line 1
b. Run-time error at line 1
c. Compile-time error at line 2
d. Run-time error at line 2
e. None of the above

Question 8

class Purple {
  public static void main (String []args) {
    int[] i = {1,2,3};  // 1
    Object obj = i;     // 2
    i = obj;            // 3
}}

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

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

Question 9

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 10

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Yellow {
  public static void main(String args[]) {
    Base base = new Sub();  // 1
    I1 i1 = base;           // 2
    Sub sub = (Sub)base;    // 3
    I2 i2 = (Sub)base;      // 4
}}

A compile-time error is generated at which line?

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

Question 11

import java.io.Serializable;
class Blue {
  public static void main (String args[]) {
    int[] i = {1,2,3};   // 1
    Serializable s = i;  // 2
    i = (int [])s;       // 3
}}

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

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

Question 12

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 13

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Gray {
  public static void main(String []args) {
  Base[] base = {new Base()};  // 1
  Sub sub[] = {new Sub()};     // 2
  Object obj = sub;            // 3
  base = obj;                  // 4
}}

A compile-time error is generated at which line?

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

Question 14

class Black {
  public static void main(String args[]) {
    int[] i = {1,2,3,4,5};                  // 1
    long[] l = new long[5];                 // 2
    for (int j = 0; j < l.length(); j++) {  // 3
      l[j] = i[j];                          // 4
}}}

A compile-time error is generated at which line?

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

Question 15

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 16

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Silver {
  public static void main(String []args) {
    Base[] base = {new Base()};
    Sub sub[] = new Sub[1];    // 1
    Object obj = base;         // 2
    sub = (Sub[])obj;          // 3
    I1 []i1 = (I1[])obj;       // 4
}}

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

a. Compile-time error at line 1
b. Run-time error at line 1
c. Compile-time error at line 2
d. Run-time error at line 2
e. Compile-time error at line 3
f. Run-time error at line 3
g. Compile-time error at line 4
h. Run-time error at line 4
i. None of the above

Question 17

class White {
  public static void main(String args[]) {
    int[] i = {1,2,3,4,5};     // 1
    long[] l1 = new long[5];   // 2
    long []l2 = l1;            // 3
    long l3[] = (long[])i;     // 4
    long l4[] = new long[5];   // 5
    l4[1] = i[1];              // 6
}}

A compile-time error is generated at which line?

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

Question 18

class UltraViolet {
  public static void main (String[] args) {
    char a = '\u002a', b = '\u0024'; // a = Asterisk *; b = 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

Question 19

class Amber {
  public static void main(String[] args) {
    int[][] a = {{1,2},{0,1,2},{-1,0,2}};  // 1
    Object[] obj = (Object[])a.clone();    // 2
    for(int i = 0; i < obj.length; i++) {  // 3
      int[] ia = (int[])obj[i];            // 4
      System.out.print(ia[i]);             // 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


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