Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 19:11:43 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 8: Class Inheritance and Interfaces

Sections 8.2-8.4
1   Suppose you create a class Cylinder to be a subclass of Circle. Analyze the following code:

class Cylinder extends Circle {
   double length;
  
   Cylinder(double radius) {
      Circle(radius);
   }
}

A. The program compiles fine, but you cannot create an instance of Cylinder because the constructor does not specify the length of the cylinder.
B. The program has a syntax error because you attempted to invoke the Circle class's constructor illegally.
C. The program compiles fine, but it has a runtime error because of invoking the Circle class's constructor illegally.

2   Analyze the following code:

public class Test extends A {
   public static void main(String[] args) {
     Test t = new Test();
     t.print();
   }
}

class A {
   String s;

   A(String s) {
     this.s = s;
   }

   public void print() {
     System.out.println(s);
   }
}

A. The program does not compile because Test does not have a default constructor Test().
B. The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed.
C. The program would compile if a default constructor A(){ } is added to class A explicitly.
D. The program compiles, but it has a runtime error due to the conflict on the method name print.

3   Analyze the following code:

import java.util.StringTokenizer;

public class A extends StringTokenizer {
}

A. The program has a compilation error because A does not have a default constructor.
B. The program has a compilation error because the default constructor of A invokes the default constructor of StringTokenizer, but StringTokenizer does not have a default constructor.
C. The program would compile fine if you add the following constructor into A: A(String s) { }
D. The program would compile fine if you add the following constructor into A: A(String s) { super(s); }

4   What is the output of running class C?

class A {
   public A() {
     System.out.println(
       "The default constructor of A is invoked");
   }
}

class B extends A {
   public B() {
     System.out.println(
       "The default constructor of B is invoked");
   }
}

public class C {
   public static void main(String[] args) {
     B b = new B();
   }
}

A. none
B. "The default constructor of B is invoked"
C. "The default constructor of A is invoked"
"The default constructor of B is invoked"
D. "The default constructor of B is invoked"
"The default constructor of A is invoked"
E. "The default constructor of A is invoked"

5   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     B b = new B();
     b.m(5);
     System.out.println("i is " + b.i);
   }
}

class A {
   int i;

   public void m(int i) {
     this.i = i;
   }
}

class B extends A {
   public void m(String s) {
   }
}

A. The program has a compilation error, because m is overridden with a different signature in B.
B. The program has a compilation error, because b.m(5) cannot be invoked since the method m(int) is hidden in B.
C. The program has a runtime error on b.i, because i is not accessble from b.
D. The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

Section 8.6 The protected and final Modifiers
6   What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it?

A. public
B. private
C. protected
D. Use the default modifier.

7   What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are accessible to any subclasses in any package?

A. public
B. private
C. protected
D. Use the default modifier.

8   The visibility of these modifiers increases in this order:

A. private, protected, none (if no modifier is used), and public.
B. private, none (if no modifier is used), protected, and public.
C. none (if no modifier is used), private, protected, and public.
D. none (if no modifier is used), protected, private, and public.

9   A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this?

A. The variable should be marked public.
B. The variable should be marked private.
C. The variable should be marked protected.
D. The variable should have no special access modifier.
E. The variable should be marked private and an accessor method provided.

10   Which of the following statements is false?

A. A public class can be accessed by a class from a different package.
B. A private method cannot be accessed by a class in a different package.
C. A protected method can be accessed by a subclass in a different package.
D. A method with no visibility modifier can be accessed by a class in a different package.

11   Which of the following classes cannot be extended?

A.
class A {
}

B.
class A { 
  private A();
}
C.
final class A {
}

D.
class A { 
  protected A();
}

12   Which is the advantage of encapsulation?

A. Only public methods are needed.
B. No exceptions need to be thrown from any method.
C. Making the class final causes no consequential changes to other code.
D. It changes the implementation without changing the interface and causes no consequential changes to other code.
E. It changes the interface without changing the implementation and causes no consequential changes to other code.

13   Which statement is true about a non-static inner class?

A. It must implement an interface.
B. It is accessible from any other class.
C. It can only be instantiated in the enclosing class.
D. It must be final if it is declared in a method scope.
E. It can access private instance variables in the enclosing object.

14   Which of the following statement is correct?

A. Every class should override the finalized method.
B. You should invoke the finalize method to finalize an object.
C. Every object can be cloned using the clone() method.
D. You can invoke the clone() method on an array object to create another array.
E. All of above are correct.

Section 8.7 Abstract Classes
15   Which of the following class definitions defines a legal abstract class?

A.
class A { 
  abstract void unfinished()
  { }
}
B.
class A { 
  abstract void unfinished();
}
C.
abstract class A { 
  abstract void unfinished();
}

D.
public class abstract A { 
  abstract void unfinished();
}

16   Which of the following declares an abstract method in an abstract Java class?

A. public abstract method();
B. public abstract void method();
C. public void abstract Method();
D. public void method() {}
E. public abstract void method() {}

17   Which of the following statements regarding abstract methods is not true?

A. An abstract class can have instances created using the constructor of the abstract class.
B. An abstract class can be extended.
C. A subclass of a non-abstract superclass can be abstract.
D. A subclass can override a concrete method in a superclass to declare it abstract.

18   Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a default constructor. Which of the following is correct?

A. A a = new A();
B. A a = new B();
C. B b = new A();
D. B b = new B();

Section 8.8 Polymorphism, Dynamic Binding, and Generic Programming
19   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     Object a1 = new A();
     Object a2 = new Object();
     System.out.println(a1);
     System.out.println(a2);
   }
}

class A {
   int x;

   public String toString() {
     return "A's x is " + x;
   }
}

A. The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString());
B. When executing System.out.println(a1), the toString() method in the Object class is invoked.
C. When executing System.out.println(a2), the toString() method in the Object class is invoked.
D. When executing System.out.println(a1), the toString() method in the A class is invoked.

20   Are the following two program displays the same result?

// Program 1:
public class Test {
   public static void main(String[] args) {
     Object a1 = new A();
     Object a2 = new A();
     System.out.println(a1.equals(a2));
   }
}

class A {
   int x;

   public boolean equals(A a) {
     return this.x == a.x;
   }
}


// Program 2
public class Test {
   public static void main(String[] args) {
     A a1 = new A();
     A a2 = new A();
     System.out.println(a1.equals(a2));
   }
}

class A {
   int x;

   public boolean equals(A a) {
     return this.x == a.x;
   }
}

A. true
B. false

21   Are the following two program displays the same result?

// Program 1
public class Test {
   public static void main(String[] args) {
     Object a1 = new A();
     Object a2 = new A();
     System.out.println(((A)a1).equals((A)a2));
   }
}

class A {
   int x;

   public boolean equals(A a) {
     return this.x == a.x;
   }
}


// Program 2
public class Test {
   public static void main(String[] args) {
     A a1 = new A();
     A a2 = new A();
     System.out.println(a1.equals(a2));
   }
}

class A {
   int x;

   public boolean equals(A a) {
     return this.x == a.x;
   }
}

A. true
B. false

Section 8.9 Casting Objects and the instanceof Operator
22   Analyze the following code:

Cylinder cy = new Cylinder(1, 1);
Circle c = cy;

A. The code has a syntax error.
B. The code has a runtime error.
C. The code is fine.

23   Analyze the following code:

Circle c = new Circle (5);
Cylinder c = cy;

A. The code has a syntax error.
B. The code has a runtime error.
C. The code is fine.

24   Given the following classes and their objects:

class C1 {};
class C2 extends C1 {};
class C3 extends C1 {};

C2 c2 = new C2();
C3 c3 = new C3();

Analyze the following statement:

c2 = (C2)((C1)c3);

A. c3 is cast into c2 successfully.
B. You will get a runtime error because you cannot cast objects from sibling classes.
C. You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form.
D. The statement is correct.

25   Given the following code:

class C1 {}
class C2 extends C1 { }
class C3 extends C2 { }
class C4 extends C1 {}

C1 c1 = new C1();
C2 c2 = new C2();
C3 c3 = new C3();
C4 c4 = new C4();

Which of the following expressions evaluates to false?

A. c1 instanceof C1
B. c2 instanceof C1
C. c3 instanceof C1
D. c4 instanceof C2

26   Analyze the following code:

public class Test {
   public static void main(String[] args) {
     String s = new String("Welcome to Java");
     Object o = s;
     String d = (String)o;
   }
}

A. When assigning s to o in Object o = s, a new object is created.
B. When casting o to s in String d = (String)o, a new object is created.
C. When casting o to s in String d = (String)o, the contents of o is changed.
D. s, o, and d reference the same String object.

27   You can assign _________ to a variable of Object[] type.

A. new char[100]
B. new int[100]
C. new double[100]
D. new String[100]
E. new java.util.Date[100]

Section 8.10 Hiding Fields and Static Methods (Optional)
28   What is the output of running A?
public class A extends B {
   int x = 1;
   public static void main(String[] args) {
     System.out.print(new A().x);
     System.out.print(new B().x);
   }
}

class B {
   int x = 5;
}

A. 15
B. 51
C. 11
D. 55

Section 8.11 Interfaces
29   Which of the following is a correct interface?

A.
interface A { 
  void print() { };
}

B.
abstract interface A { 
  print();
}

C.
abstract interface A { 
  abstract void print() { };
}

D.
interface A { 
  void print();
}

30   Show the output of running the class Test in the following code lines:

interface A {
}

class C {
}

class B extends D implements A {
}

public class Test extends Thread {
   public static void main(String[] args) {
     B b = new B();
     if (b instanceof A)
       System.out.println("b is an instance of A");
     if (b instanceof C)
       System.out.println("b is an instance of C");
   }
}

class D extends C {
}
    
A. Nothing.
B. b is an instance of A.
C. b is an instance of C.
D. b is an instance of A followed by b is an instance of C.

31   The java.lang.Comparable interface is introduced in Chapter 8. Analyze the following code:

public class Test1 {
   public Object max(Object o1, Object o2) {
     if ((Comparable)o1.compareTo(o2) >= 0) {
       return o1;
     }
     else {
       return o2;
     }
   }
}

A. The program has a syntax error because Test1 does not have a main method.
B. The program has a syntax error because o1 is an Object instance and it does not have the compareTo method.
C. The program has a syntax error because you cannot cast an Object instance o1 into Comparable.
D. The program would compile if ((Comparable)o1.compareTo(o2) >= 0) is replaced by (((Comparable)o1).compareTo(o2) >= 0).
E. b and d are both correct.

32   Suppose A is an interface, B is a concrete class with a default constructor that implements A. Which of the following is correct?

A. A a = new A();
B. A a = new B();
C. B b = new A();
D. B b = new B();
E. both b and d are fine.

33   The java.lang.Cloneable interface is introduced in Chapter 9. Analyze the following code.

public class Test {
   public static void main(String[] args) {
     java.util.Date x = new java.util.Date();
     java.util.Date y = x.clone();
     System.out.println(x = y);
   }
}

A. A java.util.Date object is not cloneable.
B. x = y in System.out.println(x = y) causes a syntax error because you cannot have an assignment statement inside a statement.
C. x = y in System.out.println(x = y) causes a runtime error because you cannot have an assignment statement inside a statement.
D. The program has a syntax error because the return type of the clone() method is java.lang.Object.

34   The GeometricObject and Circle classes are defined in Chapter 9. Analyze the following code.

public class Test {
   public static void main(String[] args) {
     GeometricObject x = new Circle(3);
     GeometricObject y = (Circle)(x.clone());
     System.out.println(x);
     System.out.println(y);
   }
}

A. The program has a syntax error because the clone() method is protected in the Object class.
B. After you override the clone() method and make it public in the Circle class, the problem can compile and run just fine, but y is null if Circle does not implement the Cloneable interface.
C. To enable a Circle object to be cloned, the Circle class has to override the clone() method and implement the java.lang.Cloneable interface.
D. All of the above.

35   Analyze the following code.


1. public class Test {
2. public static void main(String[] args) {
3. Fruit[] fruits = {new Fruit(2), new Fruit(3), new Fruit(1)};
4. java.util.Arrays.sort(fruits);
5. }
6. }

class Fruit {
   private double weight;
  
   public Fruit(double weight) {
     this.weight = weight;
   }
}


A. The program has a syntax error because the Fruit class does not have a default constructor.
B. The program has a runtime error on Line 3 because the Fruit class does not have a default constructor.
C. The program has a syntax error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.
D. The program has a runtime error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.

Section 8.13 Initialization Blocks (Optional)
36   Analyze the following code:

public class Test {
   int x;

   { x++; }
}

A. The program cannot be compiled, because the statement x++ must be placed inside a method or a constructor.
B. You cannot construct an instance of Test, because it deos not have a constructor.
C. When you construct an instance of Test, the value of x becomes 0;
D. When you construct an instance of Test, the value of x becomes 1;

37   Analyze the following code:

public class Test {
   int x;

   static { x++; }
}

A. The program cannot be compiled, because the statement x++ must be placed inside a method or a constructor.
B. The program cannot be compiled, because x is non-static, but is used in a static initilization block.
C. When you construct an instance of Test, the value of x becomes 0;
D. When you construct an instance of Test, the value of x becomes 1;

38   What is the output of the following program?
public class Test {
   public static void main(String[] args) {
     new A();
   }
  
   {
     System.out.print("Z");
   }
}

class A extends B {
   A() {
     System.out.print("A");
   }
  
   {
     System.out.print("X");
   }
}

class B {
   B() {
     System.out.print("B");
   }
  
   {
     System.out.print("Y");
   }
}
A. XYAB
B. ABXYZ
C. YBXA
D. BXAY
E. YBXAZ