Deck 10: Inheritance and Polymorphism
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
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/50
Play
Full screen (f)
Deck 10: Inheritance and Polymorphism
1
In Java, stream classes are implemented using the inheritance mechanism.
True
2
In Java, you can automatically make a reference variable of a subclass type point to an object of its superclass.
False
3
In dynamic binding, the method that gets executed is determined at compile time, not at execution time.
False
4
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
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
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
7
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
8
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
9
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
10
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;
Mystery mysRef = new Mystery();
secRef = mysRef;
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
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
13
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
14
Inheritance implies an "is-a" relationship.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
15
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
16
The superclass inherits all its properties from the subclass.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
17
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
18
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
19
If a class is declared final, then no other class can be derived from this class.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
20
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
21
What type of inheritance does Java support?
A) single inheritance
B) double inheritance
C) multiple inheritance
D) Java does not support inheritance.
A) single inheritance
B) double inheritance
C) multiple inheritance
D) Java does not support inheritance.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
22
A subclass can directly access ____.
A) public members of a superclass
B) private members of a superclass
C) all members of a superclass
D) none of the members of a superclass
A) public members of a superclass
B) private members of a superclass
C) all 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
23
Based on the diagram in the accompanying figure, the method area in the class Box ____ the method area in the class Rectangle.
A) overloads
B) overrides
C) overstates
D) deletes
A) overloads
B) overrides
C) overstates
D) deletes
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
24
Based on the diagram in the accompanying figure, which of the following members will Box NOT have direct access to?
A) getHeight()
B) setDimension()
C) length
D) print()
A) getHeight()
B) setDimension()
C) length
D) print()
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
25
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
{
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
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
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{ }
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
28
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.
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
29
Any new class you create from an existing class is called a(n) ____.
A) base class
B) superclass
C) derived class
D) extended class
A) base class
B) superclass
C) derived class
D) extended class
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
30
Composition is a "has-a" relationship.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
31
Based on the diagram in the accompanying figure, the method setDimension in the class Box ____ the method setDimension in the class Rectangle.
A) overloads
B) overrides
C) overstates
D) deletes
A) overloads
B) overrides
C) overstates
D) deletes
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
32
Suppose a class Car and its subclass Honda both have a method called speed as part of the class definition. rentalH refers to an object of the type Honda and the following statements are found in the code:rentalH.cost();
Super.speed();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.
Super.speed();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
33
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
B) two
C) three
D) four
{
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
B) two
C) three
D) four
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.
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
Inheritance is an example of what type of relationship?
A) is-a
B) has-a
C) was-a
D) had-a
A) is-a
B) has-a
C) was-a
D) had-a
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
36
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
37
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
38
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
39
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
{
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
40
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.
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
41
Composition is also called ____.
A) inheritance
B) aggregation
C) overloading
D) overriding
A) inheritance
B) aggregation
C) overloading
D) overriding
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
42
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
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
43
The method toString() is a public member of the class ____.
A) Object
B) String
C) Writer
D) Output
A) Object
B) String
C) Writer
D) Output
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
44
Which operator is used to determine if an object is of a particular class type?
A) The operator new
B) The dot (.) operator
C) The instanceof operator
D) The + operator
A) The operator new
B) The dot (.) operator
C) The instanceof operator
D) The + operator
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
45
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)
B) Only (ii)
C) Both (i) and (ii)
D) None of these
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)
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
46
An abstract class can contain ____.
A) only abstract methods
B) only non-abstract methods
C) abstract and non-abstract methods
D) nothing
A) only abstract methods
B) only non-abstract methods
C) abstract and non-abstract methods
D) nothing
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
47
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
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
48
The classes Reader and Writer are derived from the class ____.
A) Streams
B) Inputs
C) Outputs
D) Object
A) Streams
B) Inputs
C) Outputs
D) Object
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)
B) Only (ii)
C) Both (i) and (ii)
D) None of these
Mystery mysRef = new Mystery();Which of the following statements is legal in Java?
(i) secRef = mysRef;
(ii) mysRef = secRef;
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
50
An abstract class ____.
A) does not have any subclasses
B) is a superclass with many subclasses
C) cannot be instantiated
D) is the base class of all other classes
A) does not have any subclasses
B) is a superclass with many subclasses
C) cannot be instantiated
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