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

Which of the follow are true statements.

a. A nested class is any class that is declared within the body of another class or interface.
b. A nested class can not be declared within the body of an interface declaration.
c. An inner class is a nested class that is not static.
d. A nested class can not be declared static.
e. A named class is any class that is not anonymous.

Question 2

Which of the follow are true statements.

a. A local class is declared within a method, constructor or block.
b. An anonymous class is always a local class.
c. A local class is a nested class.
d. A local class is a member class.
e. A local class is always a named class.

Question 3

Which of the following are class modifiers? Select all that are applicable to a top-level class and all that are applicable to a nested class. It is not required that a modifier be applicable to both.

a. abstract
b. extends
c. final
d. implements
e. private
f. protected
g. public
h. static
i. synchronized
j. transient
k. volatile

Question 4

Which of the following modifiers can be applied to a member class?

a. abstract
b. final
c. public
d. protected
e. private
f. static
g. synchronized
h. transient

Question 5

Which of the following modifiers can be applied to a local class?

a. public
b. protected
c. private
d. abstract
e. static
f. final

Question 6

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

Which class declaration results in a compile-time error?

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

Question 7

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 8

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 9

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 10

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 11

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 12

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 13

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 14

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 15

class A {
  private static String s1 = "s1";
  final String s2 = "s2";
  A () { new Z("s5","s6");}
  class Z {
    final String s3 = "s3";
    String s4 = "s4";
    Z (final String s5, String s6) {
      System.out.print(???);
  }}
  public static void main(String args[]) {new A();}
}

Which variable can not be substituted for ??? without causing a compile-time error?

a. s1 
b. s2 
c. s3 
d. s4 
e. s5 
f. s6 
g. None of the above

Question 16

class B {
  private static String s1 = "s1";
  final String s2 = "s2";
  B () {new Z("s5","s6");}
  static class Z {
    final String s3 = "s3";
    static String s4 = "s4";
    Z (final String s5, String s6) {
      System.out.print(???);
  }}
  public static void main(String args[]) {new B();}
}

Which variable can not be substituted for ??? without causing a compile-time error?

a. s1 
b. s2 
c. s3 
d. s4 
e. s5 
f. s6 
g. None of the above

Question 17

class C {
  private static String s1 = "s1";
  String s2 = "s2";
  C() {m1("s5","s6");}
  void m1(final String s5, String s6) {
    final String s3 = "s3"; String s4 = "s4";
    class Z {Z() {System.out.print(???);}}
    new Z();
  }
  public static void main(String args[]) {new C();}
}

Which variable names can be substituted for ??? without causing a compile-time error?

a. s1
b. s2
c. s3
d. s4
e. s5
f. s6

Question 18

class D {
  D() {System.out.print("D");}
  class Z {Z(){System.out.print("Z");}}
  public static void main(String args[]) {
    new D.Z();
}}

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

a. Prints: D
b. Prints: Z
c. Prints: DZ
d. Prints: ZD
e. Run-time error
f. Compile-time error
g. None of the above

Question 19

class E {
  E() {System.out.print("E");}
  static class Z {Z(){System.out.print("Z");}}
  public static void main(String args[]) {
   new E.Z();
}}

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

a. Prints: E
b. Prints: Z
c. Prints: EZ
d. Prints: ZE
e. Run-time error
f. Compile-time error
g. None of the above

Question 20

class F {
  public void m1() {Z.m1();}      // 1
  private static class Y {
    private static void m1() {
      System.out.print("Y.m1 ");
  }}
  private static class Z {
    private static void m1(){
      System.out.print("Z.m1 ");
      Y.m1();                     // 2
  }}
  public static void main(String[] args) {
    new F().m1();
}}

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

a. Compile-time error at line 1
b. Compile-time error at line 2
c. Run-time error at line 1
d. Run-time error at line 2
e. Prints: Z.m1 Y.m1
f. None of the above

Question 21

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 22

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 23

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 24

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 25

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 26

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 27

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 28

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


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