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

Which of the following is a true statement?

a. An anonymous class can extend only the Object class.
b. An anonymous class can not implement an interface.
c. An anonymous class declaration can not have an implements clause.
d. An anonymous class declaration can name more than one interface in the implements clause.
e. The class instance creation expression for an anonymous class must never include arguments.
f. None of the above

Question 8

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 9

Which of the following are true statements?

a. An anonymous class is implicitly abstract.
b. An anonymous class is implicitly final.
c. An anonymous class is implicitly static.
d. A static reference variable can reference an instance of an anonymous class.
e. An anonymous class declaration must have at least one explicit constructor declaration.
f. An anonymous class declaration can have more than one explicit constructor declaration.

Question 10

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 11

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 12

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 13

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 14

abstract class A {
  private int x = 4, y = 2;
  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 A(int i1, int i2) {x(i1);y(i2);};
    public int math() {return x()+y();}
  };
  public static void main(String[] args) {
    System.out.print(a1.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

Question 15

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 16

abstract class A {
  private int x = 1, y = 1;
  public A(int x, int y) {this.x = x; 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: 3122
b. Prints: 2011
c. Compile-time error
d. Run-time error
e. None of the above

Question 17


abstract class A {
  private int x = 4, y = 2;
  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() {public int math() {return x()+y();}}
  static A a2 = new A() {public int math() {return x()-y();}}
  static A a3 = new A() {public int math() {return x()*y();}}
  static A a4 = new A() {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: 18
b. Prints: 6282
c. Compile-time error
d. Run-time error
e. None of the above

Question 18


class A {String m1() {return "A.m1";}}
interface B {String m2();}
class C {
  static class D extends A implements B {
    public String m1() {return "D.m1";}
    public String m2() {return "D.m2";}
  }
  static A a1 = new A() implements B {
    public String m1() {return "m1";}
    public String m2() {return "m2";}
  };
  public static void main(String[] args) {
    System.out.print(a1.m1() + "," + new C.D().m2());
}}

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

a. Prints: m1,D.m2
b. Prints: A.m1,D.m2
c. Compile-time error
d. Run-time error
e. None of the above


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