Introduction to Java Programming, AP Version, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors from the Instructor's Resource Website.

Chapter 13 Abstract Classes and Interfaces


Section 13.2 Abstract Classes
13.1  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(); }

13.2  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() {}

13.3  Which of the following statements regarding abstract methods are 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.
E. An abstract class can be used as a data type.

13.4  Which of the following statements regarding abstract methods are true?
A. Abstract classes have constructors.
B. A class that contains abstract methods must be abstract.
C. It is possible to declare an abstract class that contains no abstract methods.
D. An abstract method cannot be contained in a nonabstract class.
E. A data field can be declared abstract.

13.5  Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a no-arg 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();

13.6  What is the output of running class Test?

public class Test {
  public static void main(String[] args) {
    new Circle9();
  }
}

public abstract class GeometricObject {
  protected GeometricObject() {
    System.out.print("A");
  }

  protected GeometricObject(String color, boolean filled) {
    System.out.print("B");
  }
}

public class Circle9 extends GeometricObject {
  /** No-arg constructor */
  public Circle9() {
    this(1.0);
    System.out.print("C");
  }

  /** Construct circle with a specified radius */
  public Circle9(double radius) {
    this(radius, "white", false);
    System.out.print("D");
  }

  /** Construct a circle with specified radius, filled, and color */
  public Circle9(double radius, String color, boolean filled) {
    super(color, filled);
    System.out.print("E");
  }
}
A. ABCD
B. BACD
C. CBAE
D. AEDC
E. BEDC

Section 13.3 Case Study: the Abstract Number Class
13.7  The java.lang.Number and its subclasses are introduced in Chapter 11. Analyze the following code.

    Number numberRef = new Integer(0);
    Double doubleRef = (Double)numberRef;
A. There is no such class named Integer. You should use the class Int.
B. The compiler detects that numberRef is not an instance of Double.
C. A runtime class casting exception occurs, since numberRef is not an instance of Double.
D. The program runs fine, since Integer is a subclass of Double.
E. You can convert an int to double, so you can cast an Integer instance to a Double instance.

13.8  Analyze the following code.

    Number[] numberArray = new Integer[2];
    numberArray[0] = new Double(1.5);
A. You cannot use Number as a data type since it is an abstract class.
B. Since each element of numberArray is of the Number type, you cannot assign an Integer object to it.
C. Since each element of numberArray is of the Number type, you cannot assign a Double object to it.
D. At runtime, new Integer[2] is assigned to numberArray. This makes each element of numberArray an Integer object. So you cannot assign a Double object to it.

13.9  Analyze the following code.

public class Test {
  public static void main(String[] args) {
    Number x = new Integer(3);
    System.out.println(x.intValue());
    System.out.println(x.compareTo(new Integer(4)));
  }
}
A. The program has a compile error because an Integer instance cannot be assigned to a Number variable.
B. The program has a compile error because intValue is an abstract method in Number.
C. The program has a compile error because x does not have the compareTo method.
D. The program compiles and runs fine.

13.10  Analyze the following code.

public class Test {
  public static void main(String[] args) {
    Number x = new Integer(3);
    System.out.println(x.intValue());
    System.out.println((Integer)x.compareTo(new Integer(4)));
  }
}
A. The program has a compile error because an Integer instance cannot be assigned to a Number variable.
B. The program has a compile error because intValue is an abstract method in Number.
C. The program has a compile error because x cannot be cast into Integer.
D. The program has a compile error because the member access operator (.) is executed before the casting operator.
E. The program compiles and runs fine.

13.11  Which of the following statements are correct?
A. Integer i = 4.5;
B. Double i = 4.5;
C. Object i = 4.5;
D. Number i = 4.5;

Section 13.4 Case Study: Calendar and GregorianCalendar
13.12  The java.util.Calendar and java.util.GregorianCalendar classes are introduced in Chapter 11. Analyze the following code.

1import java.util.*;
2public class Test {
3.   public static void main(String[] args) {
4.     Calendar[] calendars = new Calendar[10];
5.     calendars[0] = new Calendar();
6.     calendars[1] = new GregorianCalendar();
7.   }
8. }
A. The program has a compile error on Line 4 because java.util.Calendar is an abstract class.
B. The program has a compile error on Line 5 because java.util.Calendar is an abstract class.
C. The program has a compile error on Line 6 because Calendar[1] is not of a GregorianCalendar type.
D. The program has no compile errors.

13.13  Assume Calendar calendar = new GregorianCalendar(). __________ returns the month of the year.
A. calendar.get(Calendar.MONTH)
B. calendar.get(Calendar.MONTH_OF_YEAR)
C. calendar.get(Calendar.WEEK_OF_MONTH)
D. calendar.get(Calendar.WEEK_OF_YEAR)

13.14  Assume Calendar calendar = new GregorianCalendar(). __________ returns the week of the year.
A. calendar.get(Calendar.MONTH)
B. calendar.get(Calendar.MONTH_OF_YEAR)
C. calendar.get(Calendar.WEEK_OF_MONTH)
D. calendar.get(Calendar.WEEK_OF_YEAR)

13.15  Assume Calendar calendar = new GregorianCalendar(). __________ returns the number of days in a month.
A. calendar.get(Calendar.MONTH)
B. calendar.get(Calendar.MONTH_OF_YEAR)
C. calendar.get(Calendar.WEEK_OF_MONTH)
D. calendar.get(Calendar.WEEK_OF_YEAR)
E. calendar.getActualMaximum(Calendar.DAY_OF_MONTH)

Section 13.5 Interfaces
13.16  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();}

13.17  Which of the following is incorrect?
A. An abstract class contains constructors.
B. The constructors in an abstract class should be protected.
C. The constructors in an abstract class are private.
D. You may declare a final abstract class.
E. An interface may contain constructors.

13.18  _______ is a reference type.
A. A class type
B. An interface type
C. An array type
D. A primitive type

13.19  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 {
  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.

13.20  Suppose A is an interface, B is a concrete class with a no-arg 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();

Section 13.6 The Comparable Interface
13.21  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 compile error because Test1 does not have a main method.
B. The program has a compile error because o1 is an Object instance and it does not have the compareTo method.
C. The program has a compile 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).

13.22  Which of the following statements are true?
A. The String class implements Comparable.
B. The Date class implements Comparable.
C. The Double class implements Comparable.
D. The BigInteger class implements Comparable.

13.23  Analyze the following code.

1public 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 compile error because the Fruit class does not have a no-arg constructor.
B. The program has a runtime error on Line 3 because the Fruit class does not have a no-arg constructor.
C. The program has a compile 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 13.7 Interfaces vs. Abstract Classes
13.24  Which of the following statements are true?
A. If you compile an interface without errors, a .class file is created for the interface.
B. If you compile a class without errors but with warnings, a .class file is created.
C. If you compile a class with errors, a .class file is created for the class.
D. If you compile an interface without errors, but with warnings, a .class file is created for the interface.

13.25  Which of the following statements are true?
A. Inheritance models the is-a relationship between two classes.
B. A strong is-a relationship describes a direct inheritance relationship between two classes.
C. A weak is-a relationship describes that a class has certain properties.
D. A strong is-a relationship can be represented using class inheritance.
E. A weak is-a relationship can be represented using interfaces.

13.26  What is the best suitable relationship between Employee and Faculty?
A. Composition
B. Aggregation
C. Inheritance
D. None.

13.27  Assume an employee can work for only one company. What is the best suitable relationship between Company and Employee?
A. None
B. Aggregation
C. Inheritance
D. Composition

13.28  The relationship between an interface and the class that implements it is
A. Composition
B. Aggregation
C. Inheritance
D. None

Section 13.9 Case Study: The Rational Class
13.29  The Rational class in this chapter is defined as a subclass of java.lang.Number. Which of the following expressions is correct?
A. Rational.doubleValue();
B. Rational.doubleValue("5/4");
C. new Rational(5, 4).doubleValue();
D. new Rational(5, 4).toDoubleValue();
E. new Rational(5, 4).intValue();

13.30  The Rational class in this chapter extends java.lang.Number and implements java.lang.Comparable. Analyze the following code.

1public class Test {
2.   public static void main(String[] args) {
3.     Number[] numbers = {new Rational(12), new Integer(4), new Double(5.6)};
4.     java.util.Arrays.sort(numbers);
5.   }
6. } 
A. The program has a compile error because numbers is declared as Number[], so you cannot assign {new Rational(1, 2), new Integer(4), new Double(5.6)} to it.
B. The program has a runtime error because numbers is declared as Number[], so you cannot assign {new Rational(1, 2), new Integer(4), new Double(5.6)} to it.
C. The program has a compile error because numbers is declared as Number[], so you cannot pass it to Arrays.sort(Object[]).
D. The program has a runtime error because the compareTo methods in Rational, Integer, and Double classes do not compare the value of one type with a value of another type.

Section 13.10 Class Design Guidelines
13.31  Which of the following statements are true?
A. A class should describe a single entity and all the class operations should logically fit together to support a coherent purpose.
B. A class should always contain a no-arg constructor.
C. The constructors must always be public.
D. The constructors may be protected.

13.32  Which of the following is poor design?
A. A data field is derived from other data fields in the same class.
B. A method must be invoked after/before invoking another method in the same class.
C. A method is an instance method, but it does not reference any instance data fields or invoke instance methods.
D. A parameter is passed from a constructor to initialize a static data field.