Certified Java Programmer Mock Exam


Question 1

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 2

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 3

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 4

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 5

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 6

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 7

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 8

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 9

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 10

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 11

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 12

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 13

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