Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Chapter 7 Inheritance – absolute java test bank


 

Download  file with the answers

Not a member!
Create a FREE account here to get access and download this file with answers


Chapter 7
Inheritance
 Multiple Choice
1) Inheritance is the process by which a new class – known as a _________ – is created from another class, called the _____________.
(a) base class, derived class
(b) derived class, base class
(c) inherited class, base class
(d) base class, inherited class

2) Inheritance promotes code ___________.
(a) reinvention
(b) reuse
(c) repeats
(d) all of the above

3) The keyword extends indicates:
(a) encapsulation
(b) polymorphism
(c) inheritance
(d) none of the above

4) A derived class is also called a
(a) sub class
(b) super class
(c) base class
(d) all of the above

5) A super class is also called a
(a) derived class
(b) dominant class
(c) sub class
(d) base class

6) What does a derived class automatically inherit from the base class?
(a) instance variables
(b) static variables
(c) public methods
(d) all of the above

7) If the final modifier is added to the definition of a method, this means:
(a) The method may be redefined in the derived class.
(b) The method may be redefined in the sub class.
(c) The method may not be redefined in the derived class.
(d) None of the above.

8) A base class is synonymous with a:
(a) Child class
(b) Parent class
(c) Derived class
(d) Sub class

9) The special syntax for invoking a constructor of the base class is:
(a) super()
(b) base()
(c) parent()
(d) child()

10) An object of a derived class has the type of the derived class, and it also has the type of the base class, and more generally, has the type of every one of its ___________ classes.
(a) descendant
(b) child
(c) ancestor
(d) sub

11) In using the keyword this in place of super(), the invocation of this must be the ___________ action taken by the constructor.
(a) first
(b) last
(c) it does not matter
(d) none of the above

12) A method or instance variable modified by protected:
(a) can not be accessed by name inside its own class definitions.
(b) can not be accessed by name inside any class derived from it.
(c) can not be accessed by name in the definition of any class in the same package.
(d) can not be accessed by name in any other class (that is, other than classes named in a-c.).

13) If an instance variable is not modified by public, protected or private then it is said to have:
(a) Package access
(b) Default access
(c) Friendly access
(d) All of the above

14) The class __________ is an ancestor class of all Java classes.
(a) String
(b) Object
(c) Math
(d) JFrame

15) The Object class contains the method:
(a) getClass()
(b) toString()
(c) equals()
(d) all of the above

16) The equals method for a class should have _________ as the type of its one parameter.
(a) String
(b) Object
(c) Integer
(d) Double

 True/False
1) A derived class contains only public instance variables and public methods from the base class.

2) Inheritance refers to a very specialized form of a class.

3) A derived class is a class defined by adding instance variables and methods to an existing class.

4) When you define a derived class, you give only the added instance variables and the added methods as well as all the methods from the base class.

5) The keyword extends indicates polymorphism.

6) Overriding is when a derived class redefines a method from the base class.

7) A constructor for a derived class begins with an invocation of a constructor for the base class.

8) The call to the base class constructor (super) must always be the last action taken in a constructor definition.

9) You may substitute the keyword this for super() to call a constructor of the derived class.

10) An instance variable (or method) that is private in a base class is accessible by name in the definition of a method in any other class.

11) Private methods of the base class are not available for use by derived classes.

 Short Answer/Essay
1) Explain what a call to super() does in a constructor of a derived class.
Answer: When super() is used in a constructor of a derived class, the matching constructor in the immediate base class is invoked.
2) Define a base class to represent a Clock. Your class should have instance variables for hours, minutes and seconds.
Answer:
public class Clock
{
private int hour;
private int minute;
private int second;
public Clock()
{
//initialize to default values
hour = 0;
minute = 0;
second = 0;
}
public Clock(int h, int m, int s)
{
setHour(h);
setMinute(m);
setSecond(s);
}
/**
Valid integers for hour is in the range of 0 – 24
*/
public void setHour(int h)
{
if((h >= 0) && (h <= 24)) hour = h; else System.out.println(“Fatal error: invalid hour”); } /** Valid integers for minute is in the range 0 – 59 */ public void setMinute(int m) { if((m >= 0) && (m <= 59)) minute = m; else System.out.println(“Fatal error: invalid minute”); } /** Valid integers for second is in the range 0 – 59 */ public void setSecond(int s) { if((s >= 0) && (s <= 59)) second = s; else System.out.println(“Fatal error: invalid second”); } public int getHour() { return hour; } public int getMinute() { return minute; } public int getSecond() { return second; } //Facilitator methods public String toString() { return “The current time is: ” + hour + “:” + minute + “:” + second; } public boolean equals(Object o) { if(o == null) return false; else if(getClass() != o.getClass()) return false; else { Clock otherClock = (Clock) o; return((hour == otherClock.hour) && (minute == otherClock.minute) && (second == otherClock.second)); } } } 3) Define a derived class to represent an alarm clock. Use the Clock class, created in number 2 above, as your base class. Answer: /** An alarm clock should include a time to sound the alarm as well as methods to set the alarm. */ public class AlarmClock extends Clock { public int alarmHour; public int alarmMinute; public int alarmSecond; public AlarmClock() { super(); alarmHour = 0; alarmMinute = 0; alarmSecond = 0; } public AlarmClock(int theHour, int theMinute, int theSecond, int alarmH, int alarmM, int alarmS) { super(theHour, theMinute, theSecond); setAlarmHour(alarmH); setAlarmMinute(alarmM); setAlarmSecond(alarmS); } public void setAlarmHour(int alarmH) { if((alarmH >= 0) && (alarmH <= 24)) alarmHour = alarmH; else System.out.println(“Fatal error: invalid alarm hour”); } public void setAlarmMinute(int alarmM) { if((alarmM >= 0) && (alarmM <= 59)) alarmMinute = alarmM; else System.out.println(“Fatal error: invalid alarm minute”); } public void setAlarmSecond(int alarmS) { if((alarmS >= 0) && (alarmS <= 59)) alarmSecond = alarmS; else System.out.println(“Fatal error: invalid alarm second”); } public int getAlarmHour() { return alarmHour; } public int getAlarmMinute() { return alarmMinute; } public int getAlarmSecond() { return alarmSecond; } public String getCurrentAlarmTime() { return “The alarm is set to ” + alarmHour + “:” + alarmMinute + “:” + alarmSecond; } //Facilitators public String toString() { return “The current time is ” + getHour() + “:” + getMinute() + “:” + getSecond() + “\nThe alarm is set to ” + getAlarmHour() + “:” + getAlarmMinute() + “:” + getAlarmSecond(); } public boolean equals(Object o) { if(o == null) return false; else if(getClass() != o.getClass()) return false; else { AlarmClock otherClock = (AlarmClock) o; return((getHour() == otherClock.getHour()) && (getMinute() == otherClock.getMinute()) && (getSecond() == otherClock.getSecond()) && (alarmHour == otherClock.alarmHour) && (alarmMinute == otherClock.alarmMinute) && (alarmSecond == otherClock.alarmSecond)); } } } 4) Create a test driver to test the functionality of your AlarmClock class created in number 3 above. Answer: public class ClockTest { /** Test driver to exercise the AlarmClock class */ public static void main(String args[]) { AlarmClock myClock = new AlarmClock(12, 30, 0, 6, 30, 0); System.out.println(myClock.toString()); //Change the alarm time myClock.setAlarmHour(7); System.out.println(myClock.toString()); //Create another AlarmClock object and test equals AlarmClock mySecondClock = new AlarmClock(); if(myClock.equals(mySecondClock)) System.out.println(“The AlarmClocks are equal!”); else System.out.println(“The AlarmClocks are not equal!”); } } 5) Explain how parent and child classes are related to base and derived classes. Answer: A base class is often called the parent class, and a derived class is often called a child class. 6) Explain the difference between method overloading and method overriding. Answer: Overriding refers to redefining a method definition in a derived class. When a method definition is overridden, the new method definition has the exact same number and types of parameters. An overloaded method has the same name as another method in the class, but number and/or type of parameters differ. 7) What is an “is a” relationship? How does it apply to the world of objects? Answer: An “is a” relationship represents the principal of inheritance. Specifically, an “is a” relationship refers to the fact that every object not only is of its own type, but is also of the type of its ancestor classes. This is a reflection of what occurs in the everyday world. 8) What is a “has a” relationship? Answer: A “has a” relationship is a composition. It allows one to create closes that are composed of other classes. 9) What is encapsulation? Answer: Encapsulation is synonymous with data and detail hiding. Java supports such hiding through the private modifier. 10) Explain the modifiers public, protected and private. Answer: The public modifier signifies that an instance variable or method is inherited freely by the derived class. The protected modifier signifies that an instance variable or method is available by name inside its own class definition, as well as by any class derived from it or in the same package. The private modifier signifies that an instance variable or method is not available outside of its class definition. 11) What is package access? Answer: If an instance variable or method is not modified by public, protected or private, then it is said to have package access. Package access means that the instance variable or method can be accessed by name inside the definition of any class in the same package, but not outside of the package. 12) Write Java statements that compares Objects, O1 and O2, using the getClass() method. Answer: if(O1.getClass() == O2.getClass()) System.out.println(“Same class!”); else System.out.println(“Not the same class!”); 13) What does the instanceof operator do? Answer: The instanceof operator checks to see if an object is of the type given as its second argument. 14) What are the different ways in which you can check the class of an Object? Answer: The method getClass() as well as the operator instanceof may be used to check the class type of an Object. 15) Create a class to represent a Rectangle. Your class should contain instance variables for length and width, as well as member method to calculate the area and perimeter. Answer: public class Rectangle { private double length; private double width; public Rectangle() { length = 0; width = 0; } public Rectangle(double l, double w) { setLength(l); setWidth(w); } public void setLength(double l) { if(l >= 0)
length = l;
else
System.out.println(“Fatal error: Length may not be negative!”);
}
public void setWidth(double w)
{
if(w >= 0)
width = w;
else
System.out.println(“Fatal error: Width may not be negative!”);
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double getArea()
{
return length * width;
}
public double getPerimeter()
{
return 2 * (length + width);
}
public String toString()
{
return “Length = ” + length + “\nWidth = ” + width;
}
public boolean equals(Object o)
{
if(o == null)
return false;
else if(getClass() != o.getClass())
return false;
else
{
Rectangle otherRectangle = (Rectangle) o;
return((length == otherRectangle.length) &&
(width == otherRectangle.width));
}
}
}
16) Create a test driver to test the functionality of your Rectangle class created in number 14 above.
Answer:
public class RectangleTest
{
public static void main(String args[])
{
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle(5,6);
Rectangle r3 = r2;
System.out.println(r1.toString() + “\n” + r2.toString() +
“\n” + r3.toString());
r1.setLength(8);
r1.setWidth(10);
System.out.println(“The area of r1 = ” + r1.getArea());
System.out.println(“The perimeter of r1 = ” + r1.getPerimeter());
if(r1.equals(r2))
System.out.println(“r1 and r2 are equal!”);
else
System.out.println(“r1 and r2 are not equal!”);
if(r2.equals(r3))
System.out.println(“The area of r2 and r3 = ” + r2.getArea());
else
System.out.println(“r2 and r3 are not equal!”);
}
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!