Chapter 9 Inheritance

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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.

a. Inheritance, the "copy and paste" approach.

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

a. Private level access

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.

a. Whether two references refer to the same object in memory.

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

b. A super class object is a subclass object.

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.

b. By calling public or protected methods in the super class.

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.

d. A reference of type A can be treated as a reference of type B.

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.

C. Objects of a subclass can be treated like objects of a superclass.

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.

b. Keyword super, followed by a set of parentheses containing the superclass constructor arguments.

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

b. Object

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.

b. Overridden methods have the same signature.

Using the protected keyword also gives a member: a. public access. b. package access. c. private access. d. block scope.

b. Package access

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.

b. protected instance variables and methods.

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

b. setText

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.

c. If the class you're inheriting from declares instance variables as private the inherited class can access those instance variable directly. (They have to be declared as protected for the inherited class to be able to access them)

Class ________ represents an image that can be displayed on a JLabel. a. Image. b. Icon. c. ImageIcon. d. IconImage.

c. ImageIcon

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.

c. Infinite Recursion

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.

c. Sailboat/Tugboat (they are both different type of boats, they would both be subclasses of superclass boat.)

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.

d. All of the above are true

Inheritance is also known as the _______ a. knows-a relationship. b. has-a relationship. c. uses-a relationship. d. is-a relationship.

d. Inheritance is known as the is-a relationship.

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

d. Shallow Copy

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.

d. The program compiles and runs because the instance variables are initialized to their default values.

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.

d. super (must be called in the default constructor of the subclass)


Ensembles d'études connexes

Gastrointestinal system medication wk 11

View Set

The Market System and Circular flow

View Set

Melanie Chiluisa & Emily Carrion

View Set

Fundamentals exam 1 online questions chapter 9

View Set

Римське приватне право

View Set