面向对象程序设计复习题及参考答案.doc

上传人:yyf 文档编号:6304225 上传时间:2020-10-23 格式:DOC 页数:18 大小:83.50KB
返回 下载 相关 举报
面向对象程序设计复习题及参考答案.doc_第1页
第1页 / 共18页
面向对象程序设计复习题及参考答案.doc_第2页
第2页 / 共18页
面向对象程序设计复习题及参考答案.doc_第3页
第3页 / 共18页
面向对象程序设计复习题及参考答案.doc_第4页
第4页 / 共18页
面向对象程序设计复习题及参考答案.doc_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《面向对象程序设计复习题及参考答案.doc》由会员分享,可在线阅读,更多相关《面向对象程序设计复习题及参考答案.doc(18页珍藏版)》请在三一文库上搜索。

1、.网络教育课程考试复习题及参考答案面向对象程序设计一、填空题:1.创建类的对象时,使用运算符_给对象分配内存空间。2.Java通过 来区分重载函数。3.在子类中使用保留字_ _可调用被子类覆盖的父类中的方法。4.使用保留字 可以从一个构造方法中调用同一个类的另一个构造方法。5.抽象类用修饰符 定义。6.类的数据成员的访问权限修饰符一般为 7.访问权限修饰符按照访问权限的大小从大到小分别为 、 、 、 。8.定义类的构造方法不能有 ,其名称与 名相同。9.抽象方法是的特征是 。10.Java中的所有异常都是从 继承来的。11.对象引用中存储的内容是 。12.下列程序段执行后,String str

2、1 = new String(Java);String str2 = new String(Java);if (str1.equals(str2) System.out.println(They are equal); else System.out.println(They are not equal);输出结果为: 。精品.13.下面循环执行后的sun值为 int count =0, sum = 0;while ( count 10 ) sum += count; count +;14.Java语言中关键字_ _表示双精度类型。15.保留字_ _用于导入包中的类到程序中,供程序中使用。16

3、.Java语言中继承是用保留字 表示。17.面向对象程序设计中,类是指 。18.对象包含 和 。19.若有类定义:class B extends A 则类B是类A的_ 。20.Java语言中, 通常把可能发生异常的方法调用语句放到try块中,并用紧跟其后的_ 块来捕获和处理异常。21.多态是指 。22.声明常量时使用修饰符 。23.Java中异常抛出使用保留字 。24.一个类成员或者方法前面加上了 修饰符,那说明该数据成员和方法可以直接通过类名来访问和调用。25.如果类成员前面没有访问权限修饰符,则该类成员具有 访问权限。精品.26.下面 构造方法是非法的a):public int Class

4、A(int one) b):public ClassB(int one,int two) c):ClassC() 27.程序填空:public void getData() String str = JoptionPane.showInputDialog(null,”Input:”); if (str.equals(“”) throw new IOException(); )28.对象称为类的 。29.Java程序的源文件以 为扩展名,编译后的文件以 为扩展名。二、简答题:精品.1.类和对象的概念和关系是什么?2.请说明对象声明和对象生成之间的区别,并使用内存状态图举例说明这种区别。3.thi

5、s和super两个保留字的意义和作用是?4.构造器方法有什么特点和作用?5.保留字throw和throws有什么区别?6.将下面的while 循环改写为for循环int count =1, sum = 0;while ( count = 30 ) sum += count; count +=3;7.Java语言编译和执行的过程是?8.检查型异常和非检查型异常有何区别?9.请改造下面的构造方法,使第一个构造方法调用第二个构造方法。public ClassOne(int alpha) this.alpha = alpha; this.beta = 0;public ClassOne(int alp

6、ha , int beta) this.alpha = alpha; this.beta = beta;10.Java有哪几个访问权限修饰符,各起到什么作用?11.请说明实例方法、类方法和构造器方法的特点和区别。精品.三、请写出下面的代码段的输出结果:1.class Q2main public static void main(string args) QuestionTwo q2; q2= new QuestionTwo(); q2.init(); q2.increment(); q2.increment(); system.out.println(q2.getCount(); class

7、QuestionTwo private int count; public void int() count=1; public void increment() count=count+1; public int getCount() return count; 2.int gradeLevel; switch (gradeLevel) case 1: System.out.print(Go to the 101);case 2: System.out.print(Go to 202); break;case 3: System.out.print(Go to 303);case 4: Sy

8、stem.out.print(Go to 404); break;精品.default: System.out.print(default);如果变量gradeLevel 在switch语句之前为以下数值,上述程序代码段执行后,将分别输出什么?a) 2b) 3c) 4d) 53.int x;for (int width = 1; width =20, width+) for (int length = 5, length =25, length+=5) x = width * length; System.out.print ( + x); System.out.println();输出结果为

9、: 4.class MyException1 extends Exception public MyException1() public MyException1(String msg) super(msg); public class ExceptionTest public static void f() throws MyException1 System.out.println(The 1st line of f();精品. throw new MyException1(Exception1:Originated in f(); public static void main(Str

10、ing args) System.out.println(The 1st line of main(); try System.out.println(The 2nd line of main(); f(); System.out.println(The 3rd line of main(); catch(MyException1 e) System.out.println(e.getMessage();finally System.out.println(The 4th line of main(); System.out.println(The 5th line of main(); 输出

11、结果为:5.import java.io.*;class Base Base() System.out.println(Base(); void m1() System.out.println(Base.m1();精品. class Derived extends Base Derived() this(default); System.out.println(Derived(); Derived(String ss) System.out.println(ss); void m1() System.out.println(Derived.m1(); public class Applicat

12、ion1 public static void main(String args) Base b; b=new Derived(); b.m1(); 输出结果为:6.class Shape 精品. void draw() System.out.println(Shape.draw(); class Circle extends Shape void draw() System.out.println(Circle.draw(); class Square extends Shape void draw() System.out.println(Square.draw(); public cla

13、ss Shapes public static void main(String args) Shape s = new Shape3;s0=new Shape();s1=new Circle();s2=new Square() for(int i = 0; i 3; i+) si.draw(); 精品.输出结果为:7.trynumber = Integer. parseInt(“-30”);i f (number 0) throw new Exception(“No negative”);catch(NumberFormatException e) System.out.println(“C

14、an not covert to int”);catch (Exception e ) System.out.println(“Error:”+e.getMessage();finally System.out.println(“DONE”);输出结果为:8.class Value int i=10; class Tester public static void test(int x) x=20; public static void test(Value v) v.i =20; 精品. public static void main(String args) Value v1=new Va

15、lue(); int x=10; Tester.test(x); Tester.test(v1); System.out.println(x); System.out.println(v1.i); 9.class Rock Rock() System.out.println(Creating Rock); Rock(int i) System.out.println(Creating Rock number + i); public class SimpleConstructor public static void main(String args) for(int i = 0; i 3;

16、i+) if(i=1) new Rock(); else new Rock(i);精品. lass BicycleRegistration public static void main(String args) Bicycle bike1,bike2; bike1 = new Bicycle( ); bike2= new Bicycle(“xxxx”); bike1.setOwnerName(Test); System.out.println(bike1.getOwnerName( ) + owns a bicycle.); System.out.println(bike2.getOwner

17、Name( ) + also owns a bicycle.); class Bicycle private String ownerName; public Bicycle( ) ownerName = Unknown; public Bicycle(String name ) ownerName = name; public String getOwnerName( ) return ownerName; 精品. public void setOwnerName(String name) ownerName = name; 输出结果为:四、编程题:1.编写一段Java程序代码,用一个对话在

18、分开的两行中显示两条消息:“I Can Design”和“And I Can Program”。2.编写一个Java程序,对于给定的年份,回答“Leap Year”(闰年)或者“Not a Leap Year”(平年)。如果一个年份能被4整除,但是不能被100整除,它是闰年;如果一个年份能被100整除,也能被400整除,它也是闰年。需要定义名为LeapYear的服务提供类3.编写一个do-while循环以计算正整数中前30个奇数的累加和。4.编写可以根据用户年龄和身高给出推荐的体重的的Java程序,利用下面的公式计算出推荐的体重:recommendedWeight = (height 100

19、+ age/10) *0.9需要定义名为Height(身高)的服务提供类,它应该会有可以根据身高得到推荐提供的方法。5.请按照以下要求写出完整的程序代码(要求能上机编译运行):1)创建一个抽象类AAbstractBase,该类至少包含一个抽象方法;2)创建一个类TestClass,该类继承AAbstractBase,并包含两个构造器方法;6.定义一个Vehicle类,类中包含一个Person类型的数据成员owner、一个获得owner值的访问方法和一个设置owner值的访问方法。定义VehicleManage个类,该类包含主方法,并在主方法中创建Vehicle类的对象,并调用其设置owner和

20、获取owner的方法。精品.参考答案一、填空题:1.new 2.不同的参数列表3.super 4.this5.abstract 6.private7.public、protected、包访问权限、private 8.返回值类型 、类9.只有方法声明没有方法体 10.Throwable11.对象的存储地址 12.They are equal13.50 14.double15.import 16.extends17.一个或多个相似对象的抽象描述 18.数据、执行在数据上的操作19.子类(导出类) 20.catch21.允许一个变量指向不同类的对象 22.final23.throw 24.stati

21、c25.包 26.a27.throws Exception 28.实例29.java 、class二、简答题:1.对象是系统中用来描述客观事物的一个实体,它是构成系统的一个基本单位。一个对象由一组属性和对这组属性进行操作的一组服务组成,类是具有相同属性和服务的一组对象的集合。类是对象的模板,对象是类的实例2.对象声明是为对象的引用创建一个空间,而对象生成则是创建一个类的实例,即为对象分配空间,如果需要的话,其还会将对象空间的地址赋给其应用。如 Tester t1;t1t1 = new Tester();t1 :Tester3.this它代表当前对象名,可用来调用本类中另一种形成的构造函数(应该

22、为构造函数中的第一条语句),在程序中易产生二义性之处,应使用this来指明当前对象;super:它引用当前对象的直接父类中的成员,可以用来调用基类中的某一个构造函数(应该为构造函数中的第一条语句,)也可以用来在基类与派生类中有相同成员定义时直接访问父类中被隐藏的父类中成员数据或函数。4.构造器一般用于对类进行初始化。其特点如下:a) 其定义形式跟类中其他方法基本类似b) 其方法名与类名完全相同(包括大小写)。c) 其在对象被创建时自动被Java调用,而无需用户干预d) 其没有返回值类型。e) 一个类可以定义多个构造方法5.throw用于抛出一个异常,throws则是用于声明未必处理的运行时异常

23、。6.sum=0;for(int count=1;count=30;count+=3) sum+=count;7.Java 编译器将以.java为后缀名的Java源代码文件编译成以.class结尾的字节码文件。Java的虚拟机再根据不同的软硬件平台将.class文件翻译成机器码文件予以执行。精品.8.因为编程错误而导致的异常,或者是不能期望程序捕获的异常(解除引用一个空指针,数组越界,除零,等等),为了使开发人员免于处理这些异常,一些异常被命名为非检查型异常(即那些继承自 RuntimeException 的异常)并且不需要进行声明,而检查型异常如果没有捕获则需在方法头部进行声明。9.publ

24、ic ClassOne(int alpha) this(alpha,0);public ClassOne(int alpha , int beta) this.alpha = alpha; this.beta = beta;10.public:表明该成员变量和方法是共有的,能在任何情况下被访问。 protected:必须在同一包中或者该类的导出类中才能被访问;private: 只能在本类中访问;缺省的为包访问权限,在同一个包中可以访问11.类方法前面有static保留字,其不需要创建类的实例,通过类名即可访问;实例方法则为普通方法,其需要创建类的实例后通过实例访问;构造方法的名称与类名完全相同

25、,其在创建类的实例是自动执行。三、请写出下面的代码段的输出结果:1.32.(a)Go to 202(b)Go to 303 Go to 404(c)Go to 404(d)default3. 5 10 15 20 2510 20 30 40 5015 30 45 60 7520 40 60 80 10025 50 75 100 1254. The 1st line of main()The 2nd line of main()The 1st line of f()Exception1:Originated in f()The 4th line of main()The 5th line of

26、main()5.Base()defaultDerived()Derived.m1()6. Shape.draw()Circle.draw()Square.draw()7.Error:No negativeDONE8. 10209. Creating Rock number 0Creating Rock精品.Creating Rock number 210.Test owns a bicycle.xxxx also owns a bicycle.四、编程题:1.import javax.swing.*;public class Test public static void main(Strin

27、g args) JOptionPane.showMessageDialog(null,I Can DesignnAnd I Can Program); 2.public class Test public static void main(String args) LeapYear ly=new LeapYear(); System.out.println(puteLeapYear(1998); System.out.println(puteLeapYear(1900); System.out.println(puteLeapYear(2000); class LeapYearpublic b

28、oolean computeLeapYear(int year)if (year % 4 = 0 & year % 100 != 0 )return true;if (year % 100 = 0 & year % 400 = 0 )return true;return false;3.public class Test public static void main(String args) int sum=0,i=0; do sum=(i*2 + 1); i+; while(i=30); System.out.println(sum); 4.public class Test public

29、 static void main(String args) Weight w1 = new Weight(); System.out.println( w1.getRecommendedWeight(30,170); class Weightpublic double getRecommendedWeight(int age,int height) return (height - 100 + age/10) *0.9; 5.public class Test public static void main(String args)精品. TestClass t1,t2; t1=new Te

30、stClass(); t1.testFun(); t2=new TestClass(10); t2.testFun(); abstract class AAbstractBaseabstract void testFun();class TestClass extends AAbstractBaseint i;TestClass()this(0);TestClass(int i)this.i=i;void testFun()System.out.println(i);6.public class VehicleManage public static void main(String args) Vehicle v1=new Vehicle(); Person p1,p2;p1=new Person(); v1.setOwner(p1); p2=v1.getOwner(); class VehiclePerson owner; public void setOwner(Person p1) owner=p1; public Person getOwner() return owner; class Person String ownerName;如有侵权请联系告知删除,感谢你们的配合!精品

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1