Deck 10: Inheritance and Polymorphism

Full screen (f)
exit full mode
Question
In Java, stream classes are implemented using the inheritance mechanism.
Use Space or
up arrow
down arrow
to flip the card.
Question
If a class is declared final, then no other class can be derived from this class.
Question
The superclass inherits all its properties from the subclass.
Question
The class Object is directly or indirectly the superclass of every class in Java.
Question
A polymorphic reference variable can refer to either an object of its own class or an object of the subclasses inherited from its class.
Question
In Java, polymorphism is implemented using late binding.
Question
A subclass cannot directly access public members of a superclass.
Question
You must always use the reserved word super to use a method from the superclass in the subclass.
Question
In dynamic binding, the method that gets executed is determined at compile time, not at execution time.
Question
A subclass inherits all its data members from the superclass; it has none of its own.
Question
To override a public method of a superclass in a subclass, the corresponding method in the subclass must have the same name but a different number of parameters.
Question
In multiple inheritance, the subclass is derived from more than one superclass.
Question
A subclass can directly access protected members of a superclass.
Question
Suppose that the class Mystery is derived from the class Secret. The following statements are legal in Java. Secret secRef; Mystery mysRef = new Mystery(); secRef = mysRef;
Question
Redefining a method of a superclass is also known as overloading a method.
Question
A subclass can override public methods of a superclass.
Question
In Java, you can automatically make a reference variable of a subclass type point to an object of its superclass.
Question
Inheritance implies an "is-a" relationship.
Question
The private members of a superclass can be accessed by a subclass.
Question
Java uses late binding for methods that are private but not for methods that are marked final.
Question
An interface is a class that contains only abstract methods and/or named constants.
Question
Interfaces are defined using the reserved word interface and the reserved word class.
Question
In the scenario described in the accompanying figure, what will the first statement do?

A) The cost method in Honda will be called.
B) The cost method in Car will be called.
C) Nothing will be called since the code will not compile as a result of multiple definitions of speed.
D) Overloading will be used to determine which cost method to use.
Question
If class Dog has a subclass Retriever, which of the following is true?

A) Because of single inheritance, Dog can have no other subclasses.
B) Because of single inheritance, Retriever can extend no other class except Dog.
C) The relationship between these classes implies that Dog "is-a" Retriever.
D) The relationship between these classes implies that Retriever "has-a" Dog.
Question
<strong>  Based on the diagram in the accompanying figure, which of the following members will Box NOT have direct access to?</strong> A) getHeight() C) length B) setDimension() D) print() <div style=padding-top: 35px>
Based on the diagram in the accompanying figure, which of the following members will Box NOT have direct access to?

A) getHeight()
C) length
B) setDimension()
D) print()
Question
To determine whether a reference variable that points to an object is of a particular class type, Java provides the operator instanceof.
Question
Consider the following class definitions. public class BClass {private int x; public void set(int a) { x = a; } public void print(){ }} public class DClass extends BClass {private int y; public void set(int a, int b) {//Postcondition: x = a; y = b;} public void print(){ }} Which of the following is the correct definition of the method set of the class DClass? (i) public void set(int a, int b) {super.set(a); y = b;} (ii) public void set(int a, int b) {x = a; y = b;}

A) Only (i)
B) Only (ii)
C) Both (i) and (ii)
D) None of these
Question
Composition is a "has-a" relationship.
Question
Inheritance is an example of what type of relationship?

A) is-a
C) was-a
B) has-a
D) had-a
Question
What is the correct syntax for defining a new class Parakeet based on the superclass Bird?

A) class Parakeet isa Bird{ }
B) class Bird defines Parakeet{ }
C) class Bird hasa Parakeet{ }
D) class Parakeet extends Bird{ }
Question
Consider the following class definitions. public class BClass {private int x; private double y; public void print() { }} public class DClass extends BClass {private int a; private int b; public void print() { }} Suppose that you have the following statement. DClass dObject = new DClass(); How many instance variables does dObject have?

A) zero
C) three
B) two
D) four
Question
What type of inheritance does Java support?

A) single inheritance
C) multiple inheritance
B) double inheritance
D) Java does not support inheritance.
Question
Suppose there are three classes named Shape, Circle, and Square. What is the most likely relationship between them?

A) Square is a superclass, and Shape and Circle are subclasses of Square.
B) Shape is a superclass, and Circle and Square are subclasses of Shape.
C) Shape, Circle, and Square are all sibling classes.
D) These three classes cannot be related.
Question
Which of the following statements about the reference super is true?

A) It must be used every time a method from the superclass is called.
B) It must be the last statement of the subclass constructor.
C) It must be the first statement of the subclass constructor.
D) It can only be used once in a program.
Question
<strong>  Based on the diagram in the accompanying figure, the method area in the class Box ____ the method area in the class Rectangle.</strong> A) overloads C) overstates B) overrides D) deletes <div style=padding-top: 35px>
Based on the diagram in the accompanying figure, the method area in the class Box ____ the method area in the class Rectangle.

A) overloads
C) overstates
B) overrides
D) deletes
Question
Consider the following class definitions. public class BClass {private int x; public void set(int a) {x = a;} public void print() {System.out.print(x);}} public class DClass extends BClass {private int y; public void set(int a, int b) {//Postcondition: x = a; y = b;} public void print(){ }} Which of the following correctly redefines the method print of DClass? (i) public void print() {System.out.print(x + " " + y);} (ii) public void print() {super.print(); System.out.print(" " + y);}

A) Only (i)
B) Only (ii)
C) Both (i) and (ii)
D) None of these
Question
A subclass can directly access ____.

A) public members of a superclass
C) all members of a superclass
B) private members of a superclass
D) none of the members of a superclass
Question
<strong>  Based on the diagram in the accompanying figure, the method setDimension in the class Box ____ the method setDimension in the class Rectangle.</strong> A) overloads C) overstates B) overrides D) deletes <div style=padding-top: 35px>
Based on the diagram in the accompanying figure, the method setDimension in the class Box ____ the method setDimension in the class Rectangle.

A) overloads
C) overstates
B) overrides
D) deletes
Question
Any new class you create from an existing class is called a(n) ____.

A) base class
C) derived class
B) superclass
D) extended class
Question
An abstract method is a method that has only the heading with no body.
Question
An abstract class ____.

A) does not have any subclasses
C) cannot be instantiated
B) is a superclass with many subclasses
D) is the base class of all other classes
Question
Composition is also called ____.

A) inheritance
C) overloading
B) aggregation
D) overriding
Question
The classes Reader and Writer are derived from the class ____.

A) Streams
C) Outputs
B) Inputs
D) Object
Question
An abstract method ____.

A) is any method in the abstract class
B) cannot be inherited
C) has no body
D) is found in a subclass and overrides methods in a superclass using the reserved word abstract
Question
An abstract class can contain ____.

A) only abstract methods
C) abstract and non-abstract methods
B) only non-abstract methods
D) nothing
Question
Which operator is used to determine if an object is of a particular class type?

A) The operator new
C) The instanceof operator
B) The dot (.) operator
D) The + operator
Question
If a class implements an interface, it must ____.

A) provide definitions for each of the methods of the interface
B) override all constants from the interface
C) rename all the methods in the interface
D) override all variables from the interface
Question
Suppose that the class Mystery is derived from the class Secret. Consider the following statements. Secret mySecret = new Secret(); Secret secRef; Mystery myMystery = new Mystery(); Mystery mysRef; secRef = myMystery; Which of the following statements is legal in Java? (i) mysRef = (Mystery) mySecret; (ii) mysRef = (Mystery) secRef;

A) Only (i)
C) Both (i) and (ii)
B) Only (ii)
D) None of these
Question
Suppose that the class Mystery is derived from the class Secret. Consider the following statements. Secret secRef = new Secret(); Mystery mysRef = new Mystery(); Which of the following statements is legal in Java? (i) secRef = mysRef; (ii) mysRef = secRef;

A) Only (i)
C) Both (i) and (ii)
B) Only (ii)
D) None of these
Question
<strong>  The method toString() is a public member of the class ____.</strong> A) Object C) Writer B) String D) Output <div style=padding-top: 35px>
The method toString() is a public member of the class ____.

A) Object
C) Writer
B) String
D) Output
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/50
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 10: Inheritance and Polymorphism
1
In Java, stream classes are implemented using the inheritance mechanism.
True
2
If a class is declared final, then no other class can be derived from this class.
True
3
The superclass inherits all its properties from the subclass.
False
4
The class Object is directly or indirectly the superclass of every class in Java.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
5
A polymorphic reference variable can refer to either an object of its own class or an object of the subclasses inherited from its class.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
6
In Java, polymorphism is implemented using late binding.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
7
A subclass cannot directly access public members of a superclass.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
8
You must always use the reserved word super to use a method from the superclass in the subclass.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
9
In dynamic binding, the method that gets executed is determined at compile time, not at execution time.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
10
A subclass inherits all its data members from the superclass; it has none of its own.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
11
To override a public method of a superclass in a subclass, the corresponding method in the subclass must have the same name but a different number of parameters.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
12
In multiple inheritance, the subclass is derived from more than one superclass.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
13
A subclass can directly access protected members of a superclass.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
14
Suppose that the class Mystery is derived from the class Secret. The following statements are legal in Java. Secret secRef; Mystery mysRef = new Mystery(); secRef = mysRef;
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
15
Redefining a method of a superclass is also known as overloading a method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
16
A subclass can override public methods of a superclass.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
17
In Java, you can automatically make a reference variable of a subclass type point to an object of its superclass.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
18
Inheritance implies an "is-a" relationship.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
19
The private members of a superclass can be accessed by a subclass.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
20
Java uses late binding for methods that are private but not for methods that are marked final.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
21
An interface is a class that contains only abstract methods and/or named constants.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
22
Interfaces are defined using the reserved word interface and the reserved word class.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
23
In the scenario described in the accompanying figure, what will the first statement do?

A) The cost method in Honda will be called.
B) The cost method in Car will be called.
C) Nothing will be called since the code will not compile as a result of multiple definitions of speed.
D) Overloading will be used to determine which cost method to use.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
24
If class Dog has a subclass Retriever, which of the following is true?

A) Because of single inheritance, Dog can have no other subclasses.
B) Because of single inheritance, Retriever can extend no other class except Dog.
C) The relationship between these classes implies that Dog "is-a" Retriever.
D) The relationship between these classes implies that Retriever "has-a" Dog.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
25
<strong>  Based on the diagram in the accompanying figure, which of the following members will Box NOT have direct access to?</strong> A) getHeight() C) length B) setDimension() D) print()
Based on the diagram in the accompanying figure, which of the following members will Box NOT have direct access to?

A) getHeight()
C) length
B) setDimension()
D) print()
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
26
To determine whether a reference variable that points to an object is of a particular class type, Java provides the operator instanceof.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
27
Consider the following class definitions. public class BClass {private int x; public void set(int a) { x = a; } public void print(){ }} public class DClass extends BClass {private int y; public void set(int a, int b) {//Postcondition: x = a; y = b;} public void print(){ }} Which of the following is the correct definition of the method set of the class DClass? (i) public void set(int a, int b) {super.set(a); y = b;} (ii) public void set(int a, int b) {x = a; y = b;}

A) Only (i)
B) Only (ii)
C) Both (i) and (ii)
D) None of these
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
28
Composition is a "has-a" relationship.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
29
Inheritance is an example of what type of relationship?

A) is-a
C) was-a
B) has-a
D) had-a
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
30
What is the correct syntax for defining a new class Parakeet based on the superclass Bird?

A) class Parakeet isa Bird{ }
B) class Bird defines Parakeet{ }
C) class Bird hasa Parakeet{ }
D) class Parakeet extends Bird{ }
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
31
Consider the following class definitions. public class BClass {private int x; private double y; public void print() { }} public class DClass extends BClass {private int a; private int b; public void print() { }} Suppose that you have the following statement. DClass dObject = new DClass(); How many instance variables does dObject have?

A) zero
C) three
B) two
D) four
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
32
What type of inheritance does Java support?

A) single inheritance
C) multiple inheritance
B) double inheritance
D) Java does not support inheritance.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
33
Suppose there are three classes named Shape, Circle, and Square. What is the most likely relationship between them?

A) Square is a superclass, and Shape and Circle are subclasses of Square.
B) Shape is a superclass, and Circle and Square are subclasses of Shape.
C) Shape, Circle, and Square are all sibling classes.
D) These three classes cannot be related.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
34
Which of the following statements about the reference super is true?

A) It must be used every time a method from the superclass is called.
B) It must be the last statement of the subclass constructor.
C) It must be the first statement of the subclass constructor.
D) It can only be used once in a program.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
35
<strong>  Based on the diagram in the accompanying figure, the method area in the class Box ____ the method area in the class Rectangle.</strong> A) overloads C) overstates B) overrides D) deletes
Based on the diagram in the accompanying figure, the method area in the class Box ____ the method area in the class Rectangle.

A) overloads
C) overstates
B) overrides
D) deletes
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
36
Consider the following class definitions. public class BClass {private int x; public void set(int a) {x = a;} public void print() {System.out.print(x);}} public class DClass extends BClass {private int y; public void set(int a, int b) {//Postcondition: x = a; y = b;} public void print(){ }} Which of the following correctly redefines the method print of DClass? (i) public void print() {System.out.print(x + " " + y);} (ii) public void print() {super.print(); System.out.print(" " + y);}

A) Only (i)
B) Only (ii)
C) Both (i) and (ii)
D) None of these
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
37
A subclass can directly access ____.

A) public members of a superclass
C) all members of a superclass
B) private members of a superclass
D) none of the members of a superclass
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
38
<strong>  Based on the diagram in the accompanying figure, the method setDimension in the class Box ____ the method setDimension in the class Rectangle.</strong> A) overloads C) overstates B) overrides D) deletes
Based on the diagram in the accompanying figure, the method setDimension in the class Box ____ the method setDimension in the class Rectangle.

A) overloads
C) overstates
B) overrides
D) deletes
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
39
Any new class you create from an existing class is called a(n) ____.

A) base class
C) derived class
B) superclass
D) extended class
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
40
An abstract method is a method that has only the heading with no body.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
41
An abstract class ____.

A) does not have any subclasses
C) cannot be instantiated
B) is a superclass with many subclasses
D) is the base class of all other classes
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
42
Composition is also called ____.

A) inheritance
C) overloading
B) aggregation
D) overriding
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
43
The classes Reader and Writer are derived from the class ____.

A) Streams
C) Outputs
B) Inputs
D) Object
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
44
An abstract method ____.

A) is any method in the abstract class
B) cannot be inherited
C) has no body
D) is found in a subclass and overrides methods in a superclass using the reserved word abstract
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
45
An abstract class can contain ____.

A) only abstract methods
C) abstract and non-abstract methods
B) only non-abstract methods
D) nothing
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
46
Which operator is used to determine if an object is of a particular class type?

A) The operator new
C) The instanceof operator
B) The dot (.) operator
D) The + operator
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
47
If a class implements an interface, it must ____.

A) provide definitions for each of the methods of the interface
B) override all constants from the interface
C) rename all the methods in the interface
D) override all variables from the interface
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
48
Suppose that the class Mystery is derived from the class Secret. Consider the following statements. Secret mySecret = new Secret(); Secret secRef; Mystery myMystery = new Mystery(); Mystery mysRef; secRef = myMystery; Which of the following statements is legal in Java? (i) mysRef = (Mystery) mySecret; (ii) mysRef = (Mystery) secRef;

A) Only (i)
C) Both (i) and (ii)
B) Only (ii)
D) None of these
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
49
Suppose that the class Mystery is derived from the class Secret. Consider the following statements. Secret secRef = new Secret(); Mystery mysRef = new Mystery(); Which of the following statements is legal in Java? (i) secRef = mysRef; (ii) mysRef = secRef;

A) Only (i)
C) Both (i) and (ii)
B) Only (ii)
D) None of these
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
50
<strong>  The method toString() is a public member of the class ____.</strong> A) Object C) Writer B) String D) Output
The method toString() is a public member of the class ____.

A) Object
C) Writer
B) String
D) Output
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 50 flashcards in this deck.