Certified Java Programmer Mock Exam


Question 1

class GFC402 {
  static int x=1;
  void m1(int i) {x++; i++;}
  public static void main (String[] args) {
    int y=3; m1(y);
    System.out.println(x + "," + y);
}}

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

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

Question 2

class EBH203 {
  static boolean a, b, c;
  public static void main (String[] args) {
    boolean x = a || (b = true) && (c = true);
    System.out.print(a + "," + b + "," + c);
}}

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. Run-time error
j. Compile-time error
k. None of the above

Question 3

class GFC102 {
  public static void main(String[] args) {
    byte b1 = -129;    // 1
    byte b2 = 127;     // 2
    short s1 = -32768; // 3
    short s2 = 32768;  // 4
    char c1 = -1;      // 5
    char c2 = 65535;   // 6
}}

Compile-time errors are generated at which lines?

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

Question 4

class GFC403 {
  private static int x=1;
  static void m1(int i) {x++; i++;}
  public static void main (String[] args) {
    int y=3; m1(y);
    System.out.println(x + "," + y);
}}

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

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

Question 5

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 6

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 7

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 8

class GFC217 {
  static String m(int i) {return "int";}
  static String m(float i) {return "float";}
  public static void main (String[] args) {
    long a1 = 1; double b1 = 2;
    System.out.print(m(a1)+","+ m(b1));
}}

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

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

Question 9

class EBH005 {
  public static void main (String[] s) {
    byte b = 127; b <<= 2;
    System.out.println(b);
}}

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

a. Prints: -4
b. Prints: -3
c. Prints: -2
d. Prints: 0
e. Prints: 1
f. Prints: 127
g. Prints: 508
h. Run-time error
i. Compile-time error
j. None of the above

Question 10

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 11

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 12

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 13

class GFC218 {
  static void m(Object x) {System.out.print("Object");}
  static void m(String x) {System.out.print("String");}
  public static void main(String[] args) {
    m(null);
}}

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

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

Question 14

class GFC303 {
  private String name;
  public GFC303(String name) {this.name = name;}
  public void setName(String name) {this.name = name;}
  public String getName() {return name;}
  public static void m1(GFC303 pet1, GFC303 pet2) {
    pet1 = new GFC303("Fish");
    pet2 = null;
  }
  public static void main (String[] args) {
    GFC303 pet1 = new GFC303("Dog");
    GFC303 pet2 = new GFC303("Cat");
    m1(pet1,pet2);
    System.out.println(pet1.getName() + "," + pet2.getName());
}}

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

a. Prints: Dog,Cat
b. Prints: Dog,Fish
c. Prints: Fish,Cat
d. Prints: Fish,Fish
e. Compile-time error
f. Run-time error
g. None of the above

Question 15

class EBH103 {
  public static void main (String[] args) {
    byte a = 1, b = 2, c, d, e;
    c = (byte)a++;
    d = (byte)++b;
    e = (byte)a + b;
    System.out.print(c + d + e);
}}

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

a. Prints: 1 2 3
b. Prints: 6
c. Prints: 2 3 5
d. Prints: 10
e. Prints: 1 3 4
f. Prints: 8
g. Prints: 1 3 5
h. Prints: 9
i. Run-time error.
j. Compile-time error.
k. None of the above

Question 16

class GFC304 {
  static void m1(int[] i1, int[] i2) {
    int[] i3 = i1; i1 = i2; i2 = i3;
  }
  public static void main (String[] args) {
    int[] i1 = {1}, i2 = {3}; m1(i1, i2);
    System.out.print(i1[0] + "," + i2[0]);
}}

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

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

Question 17

class EBH204 {
  static boolean m1(String s, boolean b) {
    System.out.print(s + (b ? "T" : "F"));
    return b;
  }
  public static void main(String[] args) {
    m1("A",m1("B",false) || m1("C",true) || m1("D",false));
}}

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

a. Prints: ATBFCT
b. Prints: ATBFCTDF
c. Prints: BFCTAT
d. Prints: BTCTDFAT
e. Run-time error
f. Compile-time error
g. None of the above

Question 18

class EBH104 {
  static int m(int i) {
    System.out.print(i + ", ");
    return i;
  }

  public static void main(String s[]) {
    int i = 1;
    m(m(++i) - m(i++) + m(-i) * m(~i));
}}

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

a. Prints: 2, 2, -3, -4, 8,
b. Prints: 2, 2, -3, -4, 12,
c. Prints: 2, 3, -3, -4, 7,
d. Prints: 1, 1, 1, 1, 0,
e. Prints: 2, 2, -2, -2, 4,
f. Prints: 2, 3, -2, -2, 3,
g. Prints: -1, -2, 2, 2, 0,
h. Run-time error
i. Compile-time error
j. None of the above

Question 19

class EBH003 {
  static int m(int i) {
    System.out.print(i + ", ");
    return i;
  }
  public static void main(String s[]) {
    m(m(~1) + m(1|2) + m(1&2) + m(1^3) + m(1<<1));
}}

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

a. Prints: -2, 3, 0, 3, 0, 6
b. Prints: -2, 3, 0, 2, 1, 4
c. Prints: -2, 3, 0, 2, 2, 5
d. Prints: -2, 3, 0, 3, 2, 6
e. Prints: -1, 3, 0, 3, 2, 7
f. Prints: -2, 0, 3, 3, 0, 6
g. Prints: -1, 0, 3, 2, 1, 4
h. Prints: -2, 0, 3, 2, 2, 5
i. Prints: -2, 0, 3, 3, 2, 6
j. Prints: -1, 0, 3, 3, 2, 7
k. Run-time error
l. Compile-time error
m. None of the above


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