JAVA练习题含答案-answer to practice 7.doc_第1页
JAVA练习题含答案-answer to practice 7.doc_第2页
JAVA练习题含答案-answer to practice 7.doc_第3页
JAVA练习题含答案-answer to practice 7.doc_第4页
JAVA练习题含答案-answer to practice 7.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Chapter 7Inheritance13Chapter 7InheritancenMultiple Choice1) 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 classAnswer: B 2) Inheritance promotes code _.(a) reinvention(b) reuse(c) repeats(d) all of the aboveAnswer: B3) The keyword extends indicates:(a) encapsulation(b) polymorphism(c) inheritance(d) none of the aboveAnswer: C 4) A derived class is also called a(a) sub class(b) super class(c) base class(d) all of the aboveAnswer: A5) A super class is also called a(a) derived class(b) dominant class(c) sub class(d) base classAnswer: D6) What does a derived class automatically inherit from the base class?(a) instance variables(b) static variables(c) public methods(d) all of the aboveAnswer: D7) 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.Answer: C 8) A base class is synonymous with a:(a) Child class(b) Parent class(c) Derived class(d) Sub classAnswer: B 9) The special syntax for invoking a constructor of the base class is:(a) super()(b) base()(c) parent()(d) child()Answer: A 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) subAnswer: C 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 aboveAnswer: A 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.).Answer: D 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 aboveAnswer: D 14) The class _ is an ancestor class of all Java classes.(a) String(b) Object(c) Math(d) JFrameAnswer: B 15) The Object class contains the method:(a) getClass()(b) toString()(c) equals()(d) all of the aboveAnswer: D 16) The equals method for a class should have _ as the type of its one parameter.(a) String(b) Object(c) Integer(d) DoubleAnswer: B nTrue/False1) A derived class contains only public instance variables and public methods from the base class.Answer: False 2) Inheritance refers to a very specialized form of a class.Answer: False 3) A derived class is a class defined by adding instance variables and methods to an existing class.Answer: True 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.Answer: False 5) The keyword extends indicates polymorphism.Answer: False 6) Overriding is when a derived class redefines a method from the base class.Answer: True 7) A constructor for a derived class begins with an invocation of a constructor for the base class.Answer: True8) The call to the base class constructor (super) must always be the last action taken in a constructor definition.Answer: False 9) You may substitute the keyword this for super() to call a constructor of the derived class.Answer: True 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.Answer: False 11) Private methods of the base class are not available for use by derived classes.Answer: True nShort Answer/Essay1) Define a base class to represent a Clock. Your class should have instance variables for hours, minutes and seconds.Answer:public class Clockprivate int hour;private int minute;private int second;public Clock()/initialize to default valueshour = 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 = 0) & (m = 0) & (s = 0)length = l;elseSystem.out.println(Fatal error: Length may not be negative!);public void setWidth(double w)if(w = 0)width = w;elseSystem.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;elseRectangle otherRectangle = (Rectangle) o;return(length = otherRectangle.length) & (width = otherRectangle.width);6) Create a test driver to test the functionality of your Rectangle class created in number 14 above.Answer:public class RectangleTestpublic 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.getPer

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论