Certified Java Programmer Mock Exam


Question 1

Which of the following statements are true?

a. A constructor can invoke another constructor of the same class using the alternate constructor invocation, "this(argumentListopt);".
b. A constructor can invoke itself using the alternate constructor invocation, "this(argumentListopt);".
c. The alternate constructor invocation, "this(argumentListopt);", can legally appear anywhere in the constructor body.
d. A constructor can invoke the constructor of the direct superclass using the superclass constructor invocation, "super(argumentListopt);".
e. The number of constructor invocations that may appear in any constructor body can equal but not exceed the number of alternate constructors declared in the same class.
f. A constructor is not permitted to throw an exception.

Question 2

Suppose that the superclass constructor invocation, "super(argumentListopt);", appears explicitly in a subclass constructor. If a compile-time error is to be avoided then the arguments for the superclass constructor invocation, "super(argumentListopt);", can not refer to which of the following?

a. Static variables declared in this class or any superclass.
b. Instance variables declared in this class or any superclass.
c. Static methods declared in this class or any superclass.
d. Instance methods declared in this class or any superclass.
e. The keyword this.
f. The keyword super.

Question 3

class A {void m1(String s1) {}}
class B extends A {
  void m1(String s1) {}  // 1
  void m1(boolean b) {}  // 2
  void m1(byte b) throws Exception {}  // 3
  String m1(short s) {return new String();} //4
  private void m1(char c) {} // 5
  protected void m1(int i) {} // 6
}

What is the result of attempting to compile the program?

a. Compile-time error at line 1
b. Compile-time error at line 2
c. Compile-time error at line 3
d. Compile-time error at line 4
e. Compile-time error at line 5
f. Compile-time error at line 6
g. None of the above

Question 4

class A {
  void m1() {System.out.print("A.m1");}
}
class B extends A {
  void m1() {System.out.print("B.m1");}
  static void m1(String s) {System.out.print(s+",");}
}
class C {
  public static void main (String[] args) {
    B.m1("main"); new B().m1();
}}

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

a. Prints: main,B.m1
b. Compile-time error
c. Run-time error
d. None of the above

Question 5

Which of the following are true statements?

a. The relationship between a class and its superclass is an example of a "has-a" relationship.
b. The relationship between a class and its superclass is an example of an "is-a" relationship.
c. The relationship between a class and an object referenced by a field within the class is an example of a "has-a" relationship.
d. The relationship between a class and an object referenced by a field within the class is an example of an "is-a" relationship.

Question 6

class A {
  String s1 = "A.s1"; String s2 = "A.s2";
}
class B extends A {
  String s1 = "B.s1";
  public static void main(String args[]) {
    B x = new B(); A y = (A)x;
    System.out.println(x.s1+" "+x.s2+" "+y.s1+" "+y.s2);
}}

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

a. Prints: B.s1 A.s2 B.s1 A.s2
b. Prints: B.s1 A.s2 A.s1 A.s2
c. Prints: A.s1 A.s2 B.s1 A.s2
d. Prints: A.s1 A.s2 A.s1 A.s2
e. Run-time error
f. Compile-time error
g. None of the above

Question 7

class C {
  void printS1() {System.out.print("C.printS1 ");}
  static void printS2() {System.out.print("C.printS2 ");}
}
class D extends C {
  void printS1(){System.out.print("D.printS1 ");}
  void printS2() {System.out.print("D.printS2 ");}
  public static void main (String args[]) {
    C c = new D(); c.printS1(); c.printS2();
}}

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

a. Prints: C.printS1 C.printS2
b. Prints: C.printS1 D.printS2
c. Prints: D.printS1 C.printS2
d. Prints: D.printS1 D.printS2
e. Run-time error
f. Compile-time error
g. None of the above

Question 8


class E {
  void printS1(){System.out.print("E.printS1 ");}
  static void printS2() {System.out.print("E.printS2");}
}
class F extends E {
  void printS1(){System.out.print("F.printS1 ");}
  static void printS2() {System.out.print("F.printS2");}
  public static void main (String args[]) {
    E x = new F(); x.printS1(); x.printS2();
}}

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

a. Prints: E.printS1 E.printS2
b. Prints: E.printS1 F.printS2
c. Prints: F.printS1 E.printS2
d. Prints: F.printS1 F.printS2
e. Run-time error
f. Compile-time error
g. None of the above

Question 9

class P {
  static void printS1(){System.out.print("P.printS1 ");}
  void printS2() {System.out.print("P.printS2 ");}
  void printS1S2(){printS1();printS2();}
}
class Q extends P {
  static void printS1(){System.out.print("Q.printS1 ");}
  void printS2(){System.out.print("Q.printS2 ");}
  public static void main(String[] args) {
    new Q().printS1S2();
}}

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

a. Prints: P.printS1 P.printS2
b. Prints: P.printS1 Q.printS2
c. Prints: Q.printS1 P.printS2
d. Prints: Q.printS1 Q.printS2
e. Run-time error
f. Compile-time error
g. None of the above

Question 10

class R {
  private void printS1(){System.out.print("R.printS1 ");}
  protected void printS2() {System.out.print("R.printS2 ");}
  protected void printS1S2(){printS1();printS2();}
}
class S extends R {
  private void printS1(){System.out.print("S.printS1 ");}
  protected void printS2(){System.out.print("S.printS2 ");}
  public static void main(String[] args) {
    new S().printS1S2();
}}

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

a. Prints: R.printS1 R.printS2
b. Prints: R.printS1 S.printS2
c. Prints: S.printS1 R.printS2
d. Prints: S.printS1 S.printS2
e. Run-time error
f. Compile-time error
g. None of the above

Question 11

class T {
  private int i1, i2;
  void printI1I2() {System.out.print("T, i1="+i1+", i2="+i2);}
  T(int i1, int i2) {
    this.i1=i1; this.i2=i2;
}}
class U extends T {
  private int i1, i2;
  void printI1I2() {System.out.print("U, i1="+i1+", i2="+i2);}
  U(int i1, int i2) {this.i1=i1; this.i2=i2;}
  public static void main(String[] args) {
    T t = new U(1,2); t.printI1I2();
}}

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

a. Prints: U, i1=1, i2=2
b. Prints: T, i1=1, i2=2
c. Prints: U, i1=null, i2=null
d. Prints: T, i1=null, i2=null
e. Run-time error
f. Compile-time error
g. None of the above

Question 12


interface I {String s1 = "I";}
class A implements I {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {
  String s1 = "C";
  void printIt() {
    System.out.print(((A)this).s1 + ((B)this).s1 +
                     ((C)this).s1 + ((I)this).s1);
  }
  public static void main (String[] args) {
    new C().printIt();
}}

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

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

Question 13


abstract class D {
  String s1 = "D"; String getS1() {return s1;}
}
class E extends D {
  String s1 = "E"; String getS1() {return s1;}
}
class F {
  public static void main (String[] s) {
    D x = new E();
    System.out.print(x.s1 + x.getS1());
}}

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

a. Prints: DD
b. Prints: DE
c. Prints: ED
d. Prints: EE
e. Run-time error
f. Compile-time error
g. None of the above

Question 14

class A {static void m() {System.out.print("A");}}
class B extends A {static void m() {System.out.print("B");}}
class C extends B {static void m() {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    C c = new C();
    c.m(); B b = c; b.m(); A a = b; a.m();
}}

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

a. Prints: AAA
b. Prints: ABC
c. Prints: CBA
d. Prints: CCC
e. Compile-time error
f. Run-time error
g. None of the above

Question 15


class A {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {String s1 = "C";}
class D {
  static void m1(A x) {System.out.print(x.s1);}
  static void m1(B x) {System.out.print(x.s1);}
  static void m1(C x) {System.out.print(x.s1);}
  public static void main(String[] args) {
    A a; B b; C c; a = b = c = new C();
    m1(a); m1(b); m1(c);
}}

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

a. Prints: AAA
b. Prints: ABC
c. Prints: CBA
d. Prints: CCC
e. Compile-time error
f. Run-time error
g. None of the above

Question 16

class Leg{}
class Fur{}
abstract class Pet {
  public abstract void eat();
  public abstract void sleep();
}
class Dog extends Pet {
  Leg leftFront = new Leg(), rightFront = new Leg();
  Leg leftRear = new Leg(), rightRear = new Leg();
  Fur fur = new Fur();
  public Fur shed() {return fur;}
  public void eat() {}
  public void sleep() {}
}
class Cat extends Dog {
  public void ignoreOwner() {}
  public void climbTree() {}
}

Which of the following statements is not a true statement?

a. A Cat object inherits an instance of Fur and four instances of Leg from the Dog superclass.
b. A Cat object is able to sleep and eat.
c. A Cat object is able to climb a tree.
d. The relationship between Dog and Pet is an example of an appropriate use of inheritance.
e. The relationship between Cat and Dog is an example of an appropriate use of inheritance.
f. None of the above.

Question 17


class A {
  A() {System.out.print("CA ");}
  static {System.out.print("SA ");}
}
class B extends A {
  B() {System.out.print("CB ");}
  static {System.out.print("SB ");}
  public static void main (String[] args) {
    B b = new B();
}}

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

a. Prints: SA CA SB CB
b. Prints: SA SB CA CB
c. Prints: SB SA CA CB
d. Prints: SB CB SA CA
e. Runtime Exception
f. Compiler Error
g. None of the above

Question 18


class A {String s1="A";}
class B extends A {String s1="B";}
class C extends B {String s1="C";}
class D extends C {
  String s1="D";
  void m1() {
    System.out.print(this.s1  + ",");      // 1
    System.out.print(((C)this).s1  + ","); // 2
    System.out.print(((B)this).s1  + ","); // 3
    System.out.print(((A)this).s1);        // 4
  }
  public static void main (String[] args) {
    new D().m1(); // 5
}}

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

a. Prints: D,D,D,D
b. Prints: D,C,B,A
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
g. Compile-time error at 5.
h. Run-time error
i. None of the above

Question 19

class SuperA {String s1="SuperA";}
class SuperB {String s1="SuperB";}
class A extends SuperA {
  String s1="A";
  class B extends SuperB {  // 1
    String s1="B";
    void m1() {
      System.out.print(this.s1 + ",");   // 2
      System.out.print(super.s1 + ",");  // 3
      System.out.print(A.this.s1 + ","); // 4
      System.out.print(A.super.s1);      // 5
    }
  }
  public static void main (String[] args) {
    new A().new B().m1();  // 6
}}

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

a. Prints: B,SuperB,B,SuperB
b. Prints: B,SuperB,A,SuperA
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
g. Compile-time error at 5.
h. Compile-time error at 6.
i. Run-time error
j. None of the above

Question 20


class A {void m1() {System.out.print("A");}}
class B extends A {void m1(){System.out.print("B");}}
class C extends B {void m1() {System.out.print("C");}}
class D extends C {
  void m1() {System.out.print("D");}
  void m2() {
    m1();
    ((C)this).m1(); // 1
    ((B)this).m1(); // 2
    ((A)this).m1(); // 3
  }
  public static void main (String[] args) {
    new D().m2(); // 4
}}

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

a. Prints: DCBA
b. Prints: DDDD
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
g. Run-time error
h. None of the above

Question 21

class A {public void m1() {System.out.print("A1");}}
class B extends A {
  public void m1() {System.out.print("B1");}
  public void m2() {System.out.print("B2");}
}
class C {
  public static void main(String[] args) {
    A a1 = new B();
    a1.m1();      // 1
    a1.m2();      // 2
   ((B)a1).m1();  // 3
   ((B)a1).m2();  // 4
}}

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

a. Prints: A1B2B1B2
b. Prints: B1B2B1B2
c. Compile-time error at 1
d. Compile-time error at 2
e. Compile-time error at 3
f. Compile-time error at 4
g. Run-time error
h. None of the above


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