Practice Test Chapter 11: Inheritance and Polymorphism
Question 1
To determine whether a reference variable that points to an object is of a
particular class type, Java provides the operator instanceof.
100.0% a. True
Question 2
If there are three classes: Shape, Circle, and Square, what is the most likely
relationship between them?
100.0% b. Shape is a superclass, and Circle and Square are subclasses of
Shape.
Question 3
Inheritance implies an �is-a� relationship.
100.0% a. True
Question 4
Suppose that the class Mystery is derived from the class Secret. Consider the
following statements:
Secret secRef;
Mystery mysRef = new Mystery();
secRef = mysRef;
The value of the expression secRef instanceof Secret is true.
100.0% b. False
Question 5
In single inheritance, the subclass is derived from a single superclass.
100.0% a. True
Question 6
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;
100.0% a. Only (i)
Question 7
Based on the diagrams above, which of the following is the superclass?
100.0% a. Rectangle
Question 8
Any new class you create from an existing class is called a(n) ____.
100.0% c. derived class
Question 9
Which of the following statements about the reference super is true?
100.0% c. It must be the first statement of the constructor.
Question 10
An abstract class ____.
100.0% c. cannot be instantiated
Question 11
Inheritance is an example of what type of relationship?
100.0% a. is-a
Question 12
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 overrides the method print of DClass?
(i)
public void print()
{
System.out.print(x + " " + y);
}
(ii)
public void print()
{
super.print();
System.out.print(" " + y);
}
100.0% b. Only (ii)
Question 13
The private members of a superclass can be accessed by a subclass.
100.0% b. False
Question 14
An interface is a class that contains only abstract methods and/or named
constants.
100.0% a. True
Question 15
A subclass can directly access ____.
100.0% a. public members of a superclass
Question 16
In Java, you can automatically make a reference variable of a subclass type
point to an object of its superclass.
100.0% b. False
Question 17
An abstract method is a method that has only the heading with no body.
100.0% a. True
Question 18
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;
100.0% b. Only (ii)
Question 19
The subclass can override public methods of a superclass.
100.0% a. True
Question 20
In dynamic binding the method that gets executed is determined at the compile
time not at execution time. On the other hand, in run-time binding the method
that gets executed is determined at execution time not at compile time.
100.0% b. False
Question 21
You can instantiate an object of a subclass of an abstract class, but only if
the subclass gives the definitions of all the abstract methods of the
superclass.
100.0% a. True
Question 22
A polymorphic reference variable can refer to either an object of their own
class or an object of the subclasses inherited from its class.
100.0% a. True
Question 23
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;
}
100.0% a. Only (i)
Question 24
A subclass can directly access protected members of a superclass.
100.0% a. True
Question 25
An abstract method ____.
100.0% c. has no body
Question 26
In Java, stream classes are implemented using the inheritance mechanism.
100.0% a. True
Question 27
If a class is declared final, then no other class can be derived from this
class.
100.0% a. True
Question 28
Composition is a(n) ____ relationship.
100.0% b. has-a
Question 29
In multiple inheritance, the subclass is derived from more than one superclass.
100.0% a. True
Question 30
You must always use the reserved word super to use a method from the superclass
in the subclass.
100.0% b. False
Question 31
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();
What does the second statement in the description above do?
100.0% b. The speed method in Car will be called.
Question 32
Java uses late binding for methods that are private but not for methods that are
marked final.
100.0% b. False
Question 33
How many members are in the class Rectangle?
100.0% b. 10
Question 34
Based on the diagram above, the method setDimension in the class Box ____ the
method setDimension in the class Rectangle.
100.0% a. overloads
Question 35
If class Dog has a subclass Retriever, which of the following is true?
100.0% b. Because of single inheritance, Retriever can extend no other class
except Dog.
Question 36
Using the mechanism of inheritance, every public member of the class Object can
be overridden and/or invoked by every object of any class type.
100.0% a. True
Question 37
The class Object is directly or indirectly the superclass of every class in
Java.
100.0% a. True
Question 38
Based on the diagram above, the method area in the class Box ____ the method
area in the class Rectangle.
100.0% b. overrides
Question 39
The superclass inherits all its properties from the subclass.
100.0% b. False
Question 40
A subclass cannot directly access public members of a superclass.
100.0% b. False
Question 41
If a class implements an interface, it must ____.
100.0% a. provide definitions for each of the methods of the interface
Question 42
Java supports both single and multiple inheritance.
100.0% b. False
Question 43
Which operator is used to determine if an object is of a particular class type?
100.0% c. The instanceof operator
Question 44
Redefining a method of a superclass is also known as overloading a method.
100.0% b. False
Question 45
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;
100.0% a. True
Question 46
In Java, polymorphism is implemented using late binding.
100.0% a. True
Question 47
An abstract class can contain ____.
100.0% c. abstract and non-abstract methods
Question 48
Composition is a �has-a� relation.
100.0% a. True
Question 49
An abstract class can only contain abstract methods.
100.0% b. False
Question 50
A subclass inherits all its data members from the superclass; it has none of its
own.
100.0% b. False
Question 51
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 DObject has?
100.0% c. 4
Question 52
How many interfaces can a class implement?
100.0% d. There is no limit to the number of interfaces that can be
implemented by a single class.
Question 53
Interfaces are defined using the reserved word interface and the reserved word
class.
100.0% b. False
Question 54
The classes Reader and Writer are derived from the class ____.
100.0% d. Object
Question 55
In Java, a reference variable of a superclass type cannot point to an object of
its subclass.
100.0% b. False
Question 56
What type of inheritance does Java support?
100.0% a. single inheritance
Question 57
What is the correct syntax for defining a new class Parakeet based on the
superclass Bird?
100.0% d. class Parakeet extends Bird{ }
Question 58
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();
What will the first statement in the situation described above do?
100.0% a. The cost method in Honda will be called.
Question 59
The method toString() is a public member of the class ____.
100.0% a. Object
Question 60
Based on the diagram above, which of the following members will Box NOT have
direct access to?
100.0% c. length
Question 61
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.
100.0% b. False