Certified Java Programmer Mock Exam


Question 1

class B {
  private String name;
  public B(String s) {name = s;}
  public void finalize() {System.out.print(name);}
}
class E {
  public static void m() {
    B x1 = new B("X"), y1 = new B("Y");
  }
  public static void main(String[] args) {
    m(); System.gc();
}}

Which of the following could not be a result of attempting to compile and run the program?

a. Prints: XY
b. Prints: YX
c. Prints: XXYY
d. Nothing is printed.
e. None of the above

Question 2

void m1() {
  Q q1 = null;
  for (int i = 0; i < 10; i++) {
    q1 = new Q();   // 1
    m2(q1);         // 2
  }
  System.out.print("All done"); // 3
}

When the processing of line 3 begins, how many objects of type Q that were created at line 1 are eligible for garbage collection?

a. 0
b. 1
c. 9
d. 10
e. Indeterminate.
f. Compile-time error
g. Run-time error
h. None of the above

Question 3

class Q {
  private int id;
  public void finalize() {System.out.print(id);}
  public Q(int i) {id = i;}
}
class R {
  public static void main(String[] args) {
    Q q1 = null;
    for (int i = 0; i < 10; i++) {
      q1 = new Q(i); // 1
    }
    System.gc();     // 2
}}

When the processing of line 2 begins, how many objects of type Q that were created at line 1 are eligible for garbage collection?

a. 0
b. 1
c. 9
d. 10
e. Indeterminate.
f. Compile-time error
g. Run-time error
h. None of the above

Question 4

class I {
  private I other;
  public void other(I i) {other = i;}
}
class J {
  private void m1() {
    I i1 = new I(), i2 = new I();
    I i3 = new I(), i4 = new I();
    i1.other(i3); i2.other(i1);
    i3.other(i2); i4.other(i4);
  }
  public static void main (String[] args) {
    new J().m1();
}}

Which object is not eligible for garbage collection after method m1 returns?

a. i1
b. i2
c. i3
d. i4
e. Compile-time error
f. Run-time error
g. None of the above

Question 5

class I {
  private String name;
  public I(String s) {name = s;}
  private I other;
  public void other(I i) {other = i;}
}
class J {
  private I i1 = new I("A"), i2 = new I("B"), i3 = new I("C");
  private void m1() {
    i1.other(i2); i2.other(i1); i3.other(i3);
    i1 = i3; i2 = i3;
    m2();
  }
  private void m2() {/* Do amazing things. */}
  public static void main (String[] args) {
    new J().m1();
}}

Which of the three objects, A, B or C, is not eligible for garbage collection when method m2 begins to execute?

a. A
b. B
c. C
d. None of the above

Question 6

class I {
  private String name;
  public void finalize() {System.out.print(name);}
  public I(String s) {name = s;}
}
class J {
  private static void m1(I[] a1) {
    a1[0] = a1[1] = a1[2] = null;
  }
  public static void main (String[] args) {
    I[] a1 = new I[3];   // 1
    a1[0] = new I("A");  // 2
    a1[1] = new I("B");  // 3
    a1[2] = new I("C");  // 4
    m1(a1);
    System.gc();
}}

After method m1 returns, the object created on which line is not eligible for garbage collection?

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

Question 7

class I {
  private String name;
  public String toString() {return name;}
  public I(String s) {name = s;}
}
class J {
  private static void m1(I[] a1) {a1 = null;}
  public static void main (String[] args) {
    I[] a1 = new I[3];   // 1
    a1[0] = new I("A");  // 2
    a1[1] = new I("B");  // 3
    a1[2] = new I("C");  // 4
    m1(a1);
    for (int i = 0; i < a1.length; i++) {
      System.out.print(a1[i]);
}}}

After method m1 returns, the object created on which line is eligible for garbage collection?

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

Question 8

class I {
  private String name;
  public String name() {return name;}
  public I(String s) {name = s;}
}
class J {
  public static void m1(I i) {i = null;}
  public static void main (String[] args) {
    I i = new I("X");           // 1
    m1(i);                      // 2
    System.out.print(i.name()); // 3
}}

Which of the following is a true statement?

a. The object created a line 1 is eligible for garbage collection after line 2.
b. A NullPointerException is generated at line 3.
c. The program compiles, runs and prints X.
d. The program fails to compile.
e. None of the above


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