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 toner, and save money. If you prefer to work my exams from printed pages, then you can save a tree, save your printer toner, and save money if you buy my new book instead of attempting to print the pages of my web site.

Today, you can find my book on the retail web site of the company that prints it and distributes it.


Question 1

class B {
  private String name;
  public B(String s) {name = s;}
  protected 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 have become 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;
  protected 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 have become 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;
  protected 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 A {
  private String name;
  private A otherA;
  public A(String name) {this.name = name;}
  public void other(A otherA) {this.otherA = otherA;}
  public A other() {return otherA;}
  public String toString() {return name;}
  protected void finalize() {System.out.print(name);}
}
class B {
  public static void m1() {
    A a1 = new A("A1"), a2 = new A("A2"), a3 = new A("A3"), a0 = a3;
    a1.other(a2); a2.other(a3); a3.other(a1);
    for(int i = 0; i<4; i++){System.out.print(a0 = a0.other());}
  }
  public static void main(String[] args) {m1(); System.gc();}
}

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

a. A1A2A3A1
b. A0A0A0A0A1A2A3
c. A1A2A3A1A2A3
d. A1A2A3A1A1A2A3
e. A1A2A3A1A3A2A1
f. A0A1A2A3A1A2A3

Question 9

class B {
  private String name;
  public B(String name) {this.name = name;}
  public String toString() {return name;}
  protected void finalize() {System.out.print(name);}
}
class H {
  static B ba = new B("Ba");
  static int i = 1;
  static B m1(B b) {return b = new B("B" + i++);}
  public static void main (String[] args) {
    B x = m1(ba); m1(x); 
    System.out.println(", " + ba + ", " + x);
}}

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

a. Ba, B1, B2
b. B1, Ba, B2
c. , Ba, B1
d. B2, Ba, B1
e. BaB1b2, null, null
f. B1B2, ba, null

Question 10

class B {
  private String name;
  public B(String name) {this.name = name;}
  public String toString() {return name;}
  protected void finalize() {System.out.print(name);}
}
class J {
  static B bc;
  static int i = 1;
  static B m1(B b) {bc = b; return new B("B" + i++);}
  public static void main (String[] args) {
    B x = m1(new B("Ba")), y = m1(new B("Bb"));
    System.out.println(", " + x + ", " + y + ", " + bc);
}}

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

a. BaBb, B1, B2, B2
b. B1B2, null, null, Bb
c. , Ba, Bb, Bb
d. BaBbB1B2, null, null, null
e. Ba, B1, B2, Bb
f. Compile-time error
g. Run-time error
h. None of the above

Question 11

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-2004, Dan Chisholm
All rights reserved.