Chapter 8 Polymorphism and Abstract Classes – absolute java test bank
Download file with the answers
Chapter 8 Polymorphism and Abstract Classes - absolute java test bank
1 file(s) 67.53 KB
Not a member!
Create a FREE account here to get access and download this file with answers
Chapter 8
Polymorphism and Abstract Classes
Multiple Choice
1) The principals of object oriented programming include:
(a) encapsulation
(b) inheritance
(c) polymorphism
(d) all of the above
2) ____________ refers to the process of associating a method definition with a method invocation.
(a) Binding
(b) Encapsulation
(c) Inheritance
(d) Polymorphism
3) __________ binding refers to the method definition being associated with the method invocation when the code is compiled.
(a) Dynamic
(b) Late
(c) Early
(d) None of the above
4) __________ refers to the ability to associate many meanings to one method name by means of the late binding mechanism.
(a) Inheritance
(b) Encapsulation
(c) Polymorphism
(d) None of the above
5) A method marked as final means the compiler uses ________ binding.
(a) dynamic
(b) early
(c) late
(d) none of the above
6) Java does not use late binding for methods marked as:
(a) final
(b) static
(c) private
(d) all of the above
7) Assigning an object of a derived class to a variable of a base class is called:
(a) static binding
(b) dynamic binding
(c) upcasting
(d) downcasting
8) Assigning an object of an ancestor class to a descendent class is called:
(a) static binding
(b) dynamic binding
(c) upcasting
(d) downcasting
9) The clone method has ________ parameters.
(a) zero
(b) one
(c) two
(d) three
10) If you choose to use the method clone in your code, you must ___________ the clone method.
(a) overload
(b) encapsulate
(c) override
(d) protect
11) The clone method return type is:
(a) the same as the cloned object
(b) Object
(c) String
(d) none of the above
12) You cannot create an object using a/an:
(a) superclass constructor
(b) subclass constructor
(c) ancestor class constructor
(d) abstract class constructor
13) An abstract method cannot be modified by:
(a) public
(b) protected
(c) private
(d) none of the above
14) A class that has at least one abstract method is called an:
(a) concrete class
(b) encapsulated class
(c) abstract class
(d) private class
15) A class with no abstract methods is called a
(a) concrete class
(b) encapsulated class
(c) abstract class
(d) private class
16) An abstract class must have the modifier ___________ included in the class heading.
(a) static
(b) abstract
(c) final
(d) private
True/False
1) Polymorphism refers to the ability to associate many meanings to one method through dynamic binding.
2) Java allows an instance of an abstract class to be instantiated.
3) Late binding refers to the method definition being associated with the method invocation when the method is invoked at run time.
4) Early binding enables the compiler to be more efficient.
5) The final modifier is included before the definition of the method, then the method can be redefined in a derived class.
6) Java uses late binding with private methods, methods marked final, or static methods.
7) The type of the variable naming an object determines which method names can be used in an invocation with that calling object.
8) Downcasting should be used only in situations where it makes sense.
9) The method clone has one parameter and should return a copy of the calling object.
10) An abstract class is a class that has some methods without complete definitions.
11) An abstract method serves as a placeholder for a method that must be defined in all derived classes.
Short Answer/Essay
1) Explain the difference between early and late binding.
Answer: Early binding occurs when the method definition is associated with the method invocation when the code is compiled. Late binding occurs when the method definition is associated with the method invocation at run time.
2) What is polymorphism and how does it relate to late binding?
Answer: Polymorphism refers to the ability to associate multiple meanings to one method name by means of the late binding mechanism. Polymorphism and late binding are technically the same topic.
3) What are the advantages of polymorphism?
Answer: An advantage of polymorphism is that it allows the programmer to generalize on common operations of the specific data type as opposed to having to define all the specific details.
4) Write a decision statement to determine if an object should be downcast.
Answer:
MyObject mo = new MyObject();
if(someObject instance of MyObject)
{
mo = (MyObject) someObject;
System.out.println(“mo was changed to ” + someObject);
}
else
System.out.print(“mo was not changed.”);
5) Describe the limitations of the copy constructor.
Answer: There are times in which the copy constructor does not work as it was intended, and the method clone() is a much better choice because of the principal of polymorphism.
6) What is an abstract method?
Answer: Abstract methods do not provide implementations. Abstract methods enable generic processing of related class objects.
7) What is wrong with the following method definition?
public abstract void doSomething(int count)
Answer: The semi-colon is missing from the end of the method definition. The correct definition follows:
public abstract void doSomething(int count);
8) Why should the instanceOf operator be used in conjunction with downcasting?
Answer: The instanceOf operator can be used to test if it is sensible to downcast an object. This makes type casts safer.
9) Draw an inheritance hierarchy to represent a shoe object. The base class should have derived classes of Dress Shoes, Tennis Shoes and Boots.
Answer:
10) Implement the base class in the shoe hierarchy in number 8 above.
Answer:
public class Shoe
{
private String color;
private String designer;
private double size;
public Shoe()
{
color = “”;
designer = “”;
size = 0;
}
public Shoe(String c, String d, double s)
{
setColor(c);
setDesigner(d);
setSize(s);
}
public void setColor(String c)
{
color = c;
}
public void setDesigner(String d)
{
designer = d;
}
public void setSize(double s)
{
if(s > 0)
size = s;
}
public String getColor()
{
return color;
}
public String getDesigner()
{
return designer;
}
public double getSize()
{
return size;
}
}
11) Derive a class named Dress Shoes from the base class created in number 9 above.
Answer:
public class DressShoe extends Shoe
{
private String type; //valid types include pumps, heels and flats
public DressShoe()
{
super();
type = “”;
}
public DressShoe(String c, String d, double s, String t)
{
super(c, d, s);
setType(t);
}
public void setType(String t)
{
type = t;
}
public String getType()
{
return type;
}
public String toString()
{
return “Dress shoe designed by ” + this.getDesigner() +
“\nColor: ” + this.getColor() + “\nSize: ” +
getSize() + “\nType: ” + getType();
}
public boolean equals(Object o)
{
if(o == null)
return false;
else if(getClass() != o.getClass())
return false;
else
{
DressShoe otherDressShoe = (DressShoe) o;
return (getDesigner().equals(otherDressShoe.getDesigner()) &&
getColor().equals(otherDressShoe.getColor()) &&
getSize() == otherDressShoe.getSize() &&
type.equals(otherDressShoe.type));
}
}
}
12) Derive a class named Tennis Shoes from the base class created in number 9 above.
Answer:
public class TennisShoe extends Shoe
{
private String soleType;
private String canvasType;
public TennisShoe()
{
super();
soleType = “”;
canvasType = “”;
}
public TennisShoe(String c, String d, double s, String st, String ct)
{
super(c, d, s);
setSoleType(st);
setCanvasType(ct);
}
public void setSoleType(String st)
{
soleType = st;
}
public void setCanvasType(String ct)
{
canvasType = ct;
}
public String getSoleType()
{
return soleType;
}
public String getCanvasType()
{
return canvasType;
}
public String toString()
{
return (“This tennis shoe is designed by: ” + this.getDesigner() +
“\nColor: ” + getColor() + “\nSize: ” + getSize() +
“\nSole type: ” + getSoleType() +
“\nCanvas type: ” + getCanvasType());
}
public boolean equals(Object o)
{
if(o == null)
return false;
else if(getClass() != o.getClass())
return false;
else
{
TennisShoe otherTennisShoe = (TennisShoe) o;
return
(getDesigner().equals(otherTennisShoe.getDesigner())
&&
getColor().equals(otherTennisShoe.getColor()) &&
getSize() == otherTennisShoe.getSize() &&
soleType.equals(otherTennisShoe.soleType) &&
canvasType.equals(otherTennisShoe.canvasType));
}
}
}
13) Derive a class named Boots from the base class created in number 9 above.
Answer:
public class Boot extends Shoe
{
private String heelType; //valid types include pumps, heels and flats
public Boot()
{
super();
heelType = “”;
}
public Boot(String c, String d, double s, String ht)
{
super(c, d, s);
setHeelType(ht);
}
public void setHeelType(String ht)
{
heelType = ht;
}
public String getHeelType()
{
return heelType;
}
public String toString()
{
return “Boot designed by ” + this.getDesigner() +
“\nColor: ” + this.getColor() + “\nSize: ” +
getSize() + “\nType: ” + getHeelType();
}
public boolean equals(Object o)
{
if(o == null)
return false;
else if(getClass() != o.getClass())
return false;
else
{
Boot otherBoot = (Boot) o;
return (getDesigner().equals(otherBoot.getDesigner()) &&
getColor().equals(otherBoot.getColor()) &&
getSize() == otherBoot.getSize() &&
heelType.equals(otherBoot.heelType));
}
}
}
14) Override the clone method inherited in the Dress Shoes class created in number 10 above.
Answer:
public Object clone()
{
return new DressShoe(this.getColor(), this.getDesigner(), this.getSize(),
this.getType());
}
15) Override the clone method inherited in the Tennis Shoes class created in number 11 above.
Answer:
public Object clone()
{
return new TennisShoe(this.getColor(), this.getDesigner(), this.getSize(),
this.getSoleType(), this.getCanvasType());
}
16) Override the clone method inherited in the Boots class created in number 12 above.
Answer:
public Object clone()
{
return new Boot(this.getColor(), this.getDesigner(), this.getSize(),
this.getHeelType());
}
Comment ( 1 )
good