You are on page 1of 5

Chapter 9 Object Oriented Programming: Inheritance

Section 9.1 Introduction


9.1 Q1: Which of the following statements is false?
a. A subclass is often larger than its superclass.
b. A superclass object is a subclass object.
c. The class following the extends keyword in a class declaration is the direct superclass of the class
being declared.
d. Java uses interfaces to provide the benefits of multiple inheritance.
ANS: b. A superclass object is a subclass object.

9.1 Q2: Inheritance is also known as the


a. knows-a relationship.
b. has-a relationship.
c. uses-a relationship.
d. is-a relationship.
ANS: d. is-a relationship

Section 9.2 Superclasses and Subclasses


9.2 Q1: Which of the following is not a superclass/subclass relationship?
a. Employee/Hourly Employee.
b. Vehicle/Car.
c. Sailboat/Tugboat.
d. None of the above.
ANS: c. Sailboat/Tugboat. A Sailboat is not a superclass for Tugboats. Both sailboat and tugboats
would be subclasses of Boat.

9.2 Q2: An advantage of inheritance is that:


a. All methods can be inherited.
b. All instance variables can be uniformly accessed by subclasses and superclasses.
c. Objects of a subclass can be treated like objects of their superclass.
d. None of the above.
ANS: c. Objects of a subclass can be treated like objects of their superclass.

Section 9.3 protected Members


9.3 Q1: Which of the following keywords allows a subclass to access a superclass method even when the
subclass has overridden the superclass method?
a. base.
b. this.
c. public.
d. super.
ANS: d. super.

9.3 Q2: Using the protected keyword also gives a member:


a. public access.
b. package access.
c. private access.
d. block scope.

© Copyright 1992-2015 by Deitel & Associates, Inc. and Pearson Education, Inc.
ANS: b. package access.

9.3 Q3: Superclass methods with this level of access cannot be called from subclasses.
a. private.
b. public.
c. protected.
d. package.
ANS: a. private.

Section 9.4 Relationship between Superclasses and


Subclasses
9.4 Q1: Which of the following statements is false?
a. A class can directly inherit from class Object.
b. It's often much more efficient to create a class by inheriting from a similar class than to create the class
by writing every line of code the new class requires.
c. If the class you're inheriting from declares instance variables as private, the inherited class can access
those instance variables directly.
d. A class's instance variables are normally declared private to enforce good software engineering.
ANS: c. If the class you're inheriting from declares instance variables as private, the inherited class
can access those instance variables directly. (Actually, if the class you're inheriting from declares
instance variables as protected, the inherited class can access those instance variables directly.)

Section 9.4.1 Creating and Using a CommissionEmployee


Class
9.4.1 Q1: Every class in Java, except ________, extends an existing class.
a. Integer.
b. Object.
c. String.
d. Class.
ANS: b. Object.

9.4.1 Q2: Overriding a method differs from overloading a method because:


a. Overloaded methods have the same signature.
b. Overridden methods have the same signature.
c. Both of the above.
d. Neither of the above.
ANS: b. Overridden methods have the same signature.

Section 9.4.2 Creating and Using a


BasePlusCommissionEmployee Class
9.4.2 Q1: To avoid duplicating code, use ________, rather than ________.
a. inheritance, the “copy-and-past” approach.
b. the “copy-and-paste” approach, inheritance.
c. a class that explicitly extends Object, a class that does not extend Object.
d. a class that does not extend Object, a class that explicitly extends Object.
ANS: a. inheritance, the “copy-and-past” approach.

© Copyright 1992-2015 by Deitel & Associates, Inc. and Pearson Education, Inc.
Section 9.4.3 Creating a CommissionEmployee-
BasePlusCommissionEmployee Inheritance Hierarchy
9.4.3 Q1: Consider the classes below, declared in the same file:
class A
{
int a;
public A()
{
a = 7;
}
}

class B extends A
{
int b;
public B()
{
b = 8;
}
}

Which of the statements below is false?


a. Both variables a and b are instance variables.
b. After the constructor for class B executes, the variable a will have the value 7.
c. After the constructor for class B executes, the variable b will have the value 8.
d. A reference of type A can be treated as a reference of type B.
ANS: d. A reference of type A can be treated as a reference of type B.

9.4.3 Q2: Which of the following is the superclass constructor call syntax?
a. keyword super, followed by a dot (.) .
b. keyword super, followed by a set of parentheses containing the superclass constructor arguments.
c. keyword super, followed by a dot and the superclass constructor name.
d. None of the above.
ANS: b. keyword super, followed by a set of parentheses containing the superclass constructor
arguments.

Section 9.4.4 CommissionEmployee-


BasePlusCommissionEmployee Inheritance Hierarchy
Using protected Instance Variables
9.4.4 Q1: Which superclass members are inherited by all subclasses of that superclass?
a. private instance variables and methods.
b. protected instance variables and methods.
c. private constructors.
d. protected constructors.
ANS: b. protected instance variables and methods.

9.4.4 Q2: Which statement is true when a superclass has protected instance variables?
a. A subclass object can assign an invalid value to the superclass’s instance variables, thus leaving an
object in an inconsistent state.
b. Subclass methods are more likely to be written so that they depend on the superclass’s data
implementation.

© Copyright 1992-2015 by Deitel & Associates, Inc. and Pearson Education, Inc.
c. We may need to modify all the subclasses of the superclass if the superclass implementation changes.
d. All of the above.
ANS: d. All of the above.

Section 9.4.5 CommissionEmployee-


BasePlusCommissionEmployee Inheritance Hierarchy
Using private Instance Variables
9.4.5 Q1: private fields of a superclass can be accessed in a subclass
a. by calling private methods declared in the superclass.
b. by calling public or protected methods declared in the superclass.
c. directly.
d. All of the above.
ANS: b. by calling public or protected methods declared in the superclass.

9.4.5 Q2: When overriding a superclass method and calling the superclass version from the subclass
method, failure to prefix the superclass method name with the keyword super and a dot (.) in the superclass
method call causes ________.
a. a compile-time error.
b. a syntax error.
c. infinite recursion.
d. a runtime error.
ANS: c. infinite recursion.

Section 9.5 Constructors in Subclasses


9.5 Q1: When a subclass constructor calls its superclass constructor, what happens if the superclass’s
constructor does not assign a value to an instance variable?
a. A syntax error occurs.
b. A compile-time error occurs.
c. A run-time error occurs.
d. The program compiles and runs because the instance variables are initialized to their default values.
ANS: d. The program compiles and runs because the instance variables are initialized to their default
values.

Section 9.6 Class Object


9.6 Q1: The default implementation of method clone of Object performs a ________.
a. empty copy.
b. deep copy.
c. full copy.
d. shallow copy.
ANS: d. shallow copy.

9.6 Q2: The default equals implementation of class Object determines:


a. whether two references refer to the same object in memory.
b. whether two references have the same type.
c. whether two objects have the same instance variables.
d. whether two objects have the same instance variable values.
ANS: a. whether two references refer to the same object in memory.

© Copyright 1992-2015 by Deitel & Associates, Inc. and Pearson Education, Inc.
Section 9.7 (Optional) GUI and Graphics Case Study:
Displaying Text and Images Using Labels
9.7 Q1: Class ________ represents an image that can be displayed on a JLabel.
a. Image.
b. Icon.
c. ImageIcon.
d. IconImage.
ANS: c. ImageIcon.

9.7 Q2: Which method changes the text the label displays?
a. changeText.
b. setText.
c. changeLabel.
d. setLabel.
ANS: b. setText.

© Copyright 1992-2015 by Deitel & Associates, Inc. and Pearson Education, Inc.

You might also like