Deck 9: Object-Oriented Programming: Inheritance
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/20
Play
Full screen (f)
Deck 9: Object-Oriented Programming: Inheritance
1
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.
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.
2
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.
A) base.
B) this.
C) public.
D) super.
D
3
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.
A) a compile-time error.
B) a syntax error.
C) infinite recursion.
D) a runtime error.
infinite recursion.
4
Superclass methods with this level of access cannot be called from subclasses.
A) private.
B) public.
C) protected.
D) package.
A) private.
B) public.
C) protected.
D) package.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
5
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.
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.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
6
Using the protected keyword also gives a member:
A) public access.
B) package access.
C) private access.
D) block scope.
A) public access.
B) package access.
C) private access.
D) block scope.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
7
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.
A) private instance variables and methods.
B) protected instance variables and methods.
C) private constructors.
D) protected constructors.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
8
Every class in Java, except ________, extends an existing class.
A) Integer.
B) Object.
C) String.
D) Class.
A) Integer.
B) Object.
C) String.
D) Class.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
9
Inheritance is also known as the
A) knows-a relationship.
B) has-a relationship.
C) uses-a relationship.
D) is-a relationship.
A) knows-a relationship.
B) has-a relationship.
C) uses-a relationship.
D) is-a relationship.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
10
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.
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.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
11
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.
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.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
12
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.
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.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
13
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.
A) Overloaded methods have the same signature.
B) Overridden methods have the same signature.
C) Both of the above.
D) Neither of the above.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
14
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.
A) Employee/Hourly Employee.
B) Vehicle/Car.
C) Sailboat/Tugboat.
D) None of the above.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
15
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.
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.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
16
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) 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.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
17
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-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.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
18
The default implementation of method clone of Object performs a ________.
A) empty copy.
B) deep copy.
C) full copy.
D) shallow copy.
A) empty copy.
B) deep copy.
C) full copy.
D) shallow copy.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
19
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.
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.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck
20
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.
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.
Unlock Deck
Unlock for access to all 20 flashcards in this deck.
Unlock Deck
k this deck