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 Z {
  static class F {}       // 1
  synchronized class G {} // 2
  transient class H {}    // 3
  volatile class I {}     // 4
}

Which class declaration does not result in a compile-time error?

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

Question 2

class Z {
  void m1() {
    abstract class A {}      // 1
    final class B {}         // 2
    private class C {}       // 3
    protected class D {}     // 4
    public class E {}        // 5
  }
}

Which class declarations result in compile-time errors?

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

Question 3

class Z {
  void m1() {
    static class F {}        // 1
    synchronized class G {}  // 2
    transient class H {}     // 3
    volatile class I {}      // 4
    abstract class A {}      // 5
    final class B {}         // 6
  }
}

Compile-time errors are generated at which lines?

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

Question 4

class A {
  A() {}        // 1
  int A;        // 2
  void A() {}   // 3
  class A {}    // 4
}

Which line results in a compile-time error?

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

Question 5

class A {
  int B;        // 1
  void B() {}   // 2
  class B {}    // 3
}

Which line results in a compile-time error?

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

Question 6

abstract class A {               // 1 
  private abstract void m1();    // 2
  private abstract class B {}    // 3
  private class C extends B {}   // 4
}

Which line results in a compile-time error?

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

Question 7

abstract class A {            // 1
  abstract final void m1();   // 2
  abstract final class B {}   // 3
  class C extends B {}        // 4
}

Which line does not result in a compile-time error?

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

Question 8

abstract class A {                   // 1
  abstract synchronized void m1();   // 2
  abstract synchronized class B {}   // 3
  synchronized class C extends B {}  // 4
}

Which line does not result in a compile-time error?

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

Question 9

class G {
  final String s1 = "G.s1";
  class Z {
    String s1;
    void m1() {System.out.println(???);}
  }
  public static void main(String args[]) {
    G g = new G(); g.new Z().m1();
}}

Which name or expression could be used in place of ??? to cause the program to print "G.s1"?

a. s1
b. G.s1
c. ((G)this).s1
d. G.this.s1
e. G.super.s1
f. None of the above

Question 10


class A {
  private static int f1 = 1;
  private int f2 = 2;
  void m1(int p1, final int p2) {
    int l1 = 5;
    final int l2 = 6;
    Object x = new Object() {
      int a = f1;  // 1
      int b = f2;  // 2
      int c = p1;  // 3
      int d = p2;  // 4
      int e = l1;  // 5
      int f = l2;  // 6
};}}

Compile-time errors are generated at which lines?

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

Question 11

class Outer {
  static class StaticNested {
    static final int a = 25; // 1
    static final int b;      // 2
    static int c;            // 3
    int d;                   // 4
    static {b = 42;}         // 5
  }
  class NonStaticInner {
    static final int e = 25; // 6
    static final int f;      // 7
    static int g;            // 8
    int h;                   // 9
    static {f = 42;}         // 10
}}

Compile-time errors are generated at which lines?

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

Question 12

class Red {
  private static final int a = 10;  // 1
  protected static int b = 20;      // 2
  int c = 30;                       // 3
  static class StaticNested {
    int d = a;                      // 4
    int e = b;                      // 5 
    int f = c;                      // 6
  }
  class NonStaticInner {
    int g = a;                      // 7
    int h = b;                      // 8
    int i = c;                      // 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

Question 13

class Red {
  static class StaticNested {interface ABC {}}  // 1
  class NonStaticInner {interface DEF {}}       // 2
  interface GHI {}                              // 3
}

A compile-time error is generated at which line?

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

Question 14

class A {
  private static int counter;
  public static int getCounter(){return counter++;}
  private static int innerCounter;
  public static int getInnerCounter(){return innerCounter++;}
  private String name;
  A() {name = "A" + getCounter();}
  class B {
    private String name;
    B() {
      name = "B" + getInnerCounter();
      System.out.print(A.this.name + name); // 1
  }}
  public static void main(String[] args) {
    new A().new B();  // 2
    A a1 = new A();
    a1.new B();       // 3
    a1.new B();       // 4
}}

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

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

Question 15

class A {
  private static int counter;
  public static int getCounter(){return counter++;}
  private static int innerCounter;
  public static int getInnerCounter(){return innerCounter++;}
  private String name;
  A() {name = "A" + getCounter();}
  class B {
    private String name;
    B() {
      name = "B" + getInnerCounter();
      System.out.print(A.this.name + name); // 1
  }}
  void m1() {new A().new B();}  // 2
  void m2() {this.new B();}     // 3
  void m3() {new B();}          // 4
  public static void main(String[] args) {
    A a1 = new A();
    a1.m1(); a1.m2(); a1.m3();
}}

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

a. Prints: A0B0A1B1A1B2
b. Prints: A0B0A1B1A2B2
c. Prints: A1B0A0B1A0B2
d. Compile-time error at line 1
e. Compile-time error at line 2
f. Compile-time error at line 3
g. Compile-time error at line 4
h. Other compile-time error.
i. Run-time error
j. None of the above

Question 16

class A {
  private static int counter;
  public static int getCounter(){return counter++;}
  private static int innerCounter;
  public static int getInnerCounter(){return innerCounter++;}
  private String name;
  A() {name = "A" + getCounter();}
  class B {
    private String name;
    B() {
      name = "B" + getInnerCounter();
      System.out.print(A.this.name + name);
  }}
  void m1() {new A().new B();}  // 1
  void m2() {new A.B();}        // 2
  void m3() {new B();}          // 3
  public static void main(String[] args) {
    A a1 = new A();
    a1.m1(); a1.m2(); a1.m3();
}}

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

a. Prints: A0B0A1B1A1B2
b. Prints: A0B0A1B1A2B2
c. Prints: A1B0A0B1A0B2
d. Compile-time error at line 1
e. Compile-time error at line 2
f. Compile-time error at line 3
g. Compile-time error at line 4
h. Other compile-time error.
i. Run-time error
j. None of the above

Question 17

class A {
  private static int counter;
  public static int getCounter(){return counter++;}
  private static int innerCounter;
  public static int getInnerCounter(){return innerCounter++;}
  private String name;
  A() {name = "A" + getCounter();}
  class B {
    private String name;
    B() {
      name = "B" + getInnerCounter();
      System.out.print(A.this.name + name); // 1
  }}
  static void m1() {new A().new B();}  // 2
  static void m2() {this.new B();}     // 3
  static void m3() {new B();}          // 4
  public static void main(String[] args) {
    m1(); m2(); m3();
}}

What are the results of attempting to compile and run the program?

a. Prints: A0B0A1B1A1B2
b. Prints: A0B0A1B1A2B2
c. Prints: A1B0A0B1A0B2
d. Compile-time error at line 1
e. Compile-time error at line 2
f. Compile-time error at line 3
g. Compile-time error at line 4

Question 18


abstract class A {
  private int x = 4, y = 2;
  public A(int i1, int i2) {x=i1;y=i2;}
  public int x() {return x;}
  public void x(int x) {this.x = x;}
  public int y() {return y;}
  public void y(int y) {this.y = y;}
  public abstract int math();
}
class B {
  static A a1 = new A(2,1) {public int math() {return x()+y();}};
  static A a2 = new A(2,1) {public int math() {return x()-y();}};
  static A a3 = new A(2,1) {public int math() {return x()*y();}};
  static A a4 = new A(2,1) {public int math() {return x()/y();}};
  public static void main(String[] args) {
    System.out.print("" + a1.math() + a2.math() +
                          a3.math() + a4.math());
}}

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

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


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