Secction 9
Inheritance is also known as the a. knows-a relationship. b. has-a relationship. c. uses-a relationship. d. is-a relationship
. is-a relationship
Which method changes the text the label displays?
setText
Class ________ represents an image that can be displayed on a JLabel. a. Image. b. Icon. c. ImageIcon. d. IconImage
ImageIcon.
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
Overridden methods have the same signature
: 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.
The program compiles and runs because the instance variables are initialized to their default values.
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
by calling public or protected methods declared in the superclass.
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.
inheritance, the "copy-and-past" approach.
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
keyword super, followed by a set of parentheses containing the superclass constructor arguments.
Superclass methods with this level of access cannot be called from subclasses. a. private. b. public. c. protected. d. package.
private.
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.
protected instance variables and methods.
The default implementation of method clone of Object performs a ________. a. empty copy. b. deep copy. c. full copy. d. shallow copy
shallow copy
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.
super.
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.
whether two references refer to the same object in memory
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.
. A reference of type A can be treated as a reference of type B.
: 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.
. Sailboat/Tugboat. A Sailboat is not a superclass for Tugboats. Both sailboat and tugboats would be subclasses of Boat.
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 ________.
. infinite recursion
Using the protected keyword also gives a member:
. package access.
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.
A superclass object is a subclass object.
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. c. We may need to modify all the subclasses of the superclass if the superclass implementation changes. d. All of the above.
All of the above.
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.
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.)
Every class in Java, except ________, extends an existing class. a. Integer. b. Object. c. String. d. Class.
Object.
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.
Objects of a subclass can be treated like objects of their superclass.