Java程序设计复习题.doc

上传人:创****公 文档编号:2079855 上传时间:2019-04-18 格式:DOC 页数:11 大小:103.50KB
下载 相关 举报
Java程序设计复习题.doc_第1页
第1页 / 共11页
Java程序设计复习题.doc_第2页
第2页 / 共11页
Java程序设计复习题.doc_第3页
第3页 / 共11页
Java程序设计复习题.doc_第4页
第4页 / 共11页
Java程序设计复习题.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

1、Java 程序设计复习题一、单项选择题1下面哪些是 java 语言中的关键字? Asizeof Babstract CNULL DNative2下面语句哪个是正确的? Achar=abc; Blong l=oxfff; Cfloat f=0.23; Ddouble=0.7E-3;3以下程序测试 String 类的各种构造方法,试选出其运行效果。 class STRpublic static void main(String args)String s1=new String();String s2=new String(“String 2“);char chars=a, ,s,t,r,i,n,

2、g;String s3=new String(chars);String s4=new String(chars,2,6);byte bytes=0,1,2,3,4,5,6,7,8,9; StringBuffer sb=new StringBuffer(s3);String s5=new String(sb);System.out.println(“The String No.1 is “+s1);System.out.println(“The String No.2 is “+s2);System.out.println(“The String No.3 is “+s3);System.ou

3、t.println(“The String No.4 is “+s4);System.out.println(“The String No.5 is “+s5);AThe String No.1 is The String No.2 is String 2 The String No.3 is a string The String No.4 is string The String No.5 is a string BThe String No.1 isThe String No.2 is String 2 The String No.3 is a string The String No.

4、4 is tring The String No.5 is a string CThe String No.1 isThe String No.2 is String 2The String No.3 is a string The String No.4 is strin The String No.5 is a string D以上都不对4下面语句段的输出结果是什么? int i = 9; switch (i) default: System.out.println(“default“); case 0: System.out.println(“zero“); break; case 1:

5、 System.out.println(“one“); case 2: System.out.println(“two“); Adefault Bdefault, zero Cerror default clause not defined Dno output displayed5有关类 Demo,哪句描述是正确的? public class Demo extends Base private int count; public Demo() System.out.println(“A Demo object has been created“); protected void addOne

6、() count+; A当创建一个 Demo 类的实例对象时,count 的值为 0。B当创建一个 Demo 类的实例对象时,count 的值是不确定的。C超类对象中可以包含改变 count 值的方法。DDemo 的子类对象可以访问 count。6当编译和运行下列程序段时,会发生什么? class Base class Sub extends Base class Sub2 extends Base public class CEx public static void main(String argv) Base b = new Base(); Sub s = (Sub) b; A通过编译和

7、并正常运行。B编译时出现例外。C编译通过,运行时出现例外。7如果任何包中的子类都能访问超类中的成员,那么应使用哪个限定词? Apublic Bprivate Cprotected Dtransient8下面的哪个选项是正确的? class ExSuper String name; String nick_name; public ExSuper(String s,String t) name = s;nick_name = t; public String toString() return name; public class Example extends ExSuper public E

8、xample(String s,String t) super(s,t); public String toString() return name +“a.k.a“+nick_name; public static void main(String args)ExSuper a = new ExSuper(“First“,“1st“);ExSuper b = new Example(“Second“,“2nd“);System.out.println(“a is“+a.toString();System.out.println(“b is“+b.toString();A编译时会出现例外。B运

9、行结果为:a is Firstb is secondC运行结果为:a is Firstb is Secong a.k.a 2 ndD运行结果为:a is First a.k.a 1ndb is Second a.k.a 2nd9运行下列程序的结果是哪个?abstract class MineBase abstract void amethod(); static int i; public class Mine extends MineBasepublic static void main(String argv)int ar = new int5;for(i = 0;i 0) 8) 9) j

10、 = I * 2; 10) System.out.println (“ The value of j is “ + j ); 11) k = k + 1; 12) I-; 13) 14) 15) line 11 6请写出下面程序的运行结果。 public class Test extends TT public static void main(String args) Test t = new Test(“Tom“); public Test(String s) super(s); System.out.println(“How do you do?“); public Test() thi

11、s(“I am Tom“); class TT public TT() System.out.println(“What a pleasure!“); public TT(String s) this(); System.out.println(“I am “+s); 结果: What a pleasure! I am Tom How do you do?7给定下面的未完成的代码片断: public class Example int x,y; public Example(int a) x = a; public Example(int a, int b) / 和上面一个参数的构造方法做同样

12、的操作,包括赋值 x=a y = b;如果要用最简捷的一行代码实现“/和上面一个参数的“注释所指出的功能,请写出你认为最合适的一行代码: this(a)三阅读程序回答问题 下面是 Java API 类库中 System 类的定义原型public final class java.lang.System extends java.lang.Objectpublic static PrintStream err;public static InputStream in;public static PrintStream out;public static void arraycopy(Object

13、 src,int src_position,Object dst,int dst_position,int length);public static long currentTimeMillis();public static void exit(int status);public static void gc();public static Properties getProperties();public static String getProperty(String key);public static String getProperty(String key,String de

14、f);public static SecurityManager getSecurityManager();public static void load(String filename);public static void loadLibrary(String libname);public static void runFinalization();public static void setProperties(Properties props);public static void setSecurityManager(SecurityManager s);请问: System 类的

15、父类是哪个类? System 类的构造方法是什么? System 类被声明为 “final”,这有什么含义? System 类中的方法成员被声明为“static” ,这有什么含义? System 类中出现了两个 getProperty 方法,这是怎么回事?四根据要求编写程序(每个程序 15分,共 30分)1编写一个满足下列要求的程序: 为学生创建一个类 Stud,这个类包括每个学生的姓名(name:长度为 8 的字符串) 、学号(ID:int)和年级(grade :取值为 1-4 的整型) ; 创建 20 个学生,姓名为NAME1,NAME2,NAME20,ID 和年级随机指定; 编写程序 S

16、tudent.java,找出所有三年级的学生并打印他们的姓名和 ID,运行界面如图 1 所示。 显示消息对话框的方法:showMessageDialog(null, Object message, String title, int messageType)2编写一个程序,用选择法对数组 a=20,10,50,40,30,70,60,80,90,100进行从大到小的排序。public class ArraySortpublic static void main(String args)int array=20,10,50,40,30,70,60,80,90,100;int i,j,k,t;in

17、t l=array.length;for(i=0;il-1;i+)k=i;for(j=i+1;jl;j+)if(arrayjarrayk) k=j;t=arrayk;arrayk=arrayi;arrayi=t;for(i=0;il;i+)System.out.println(“array“+i+“=“+arrayi);3为某研究所编写一个通用程序,用来计算每一种交通工具运行 1000 公里所需的时间,已知每种交通工具的参数都是 3 个整数 A、B、C 的表达式。现有两种工具:Car007 和 Plane,其中 Car007 的速度运算公式为:A*B/C,Plane 的速度运算公式为:A+B+

18、C 。需要编写三类:ComputeTime.java,Plane.java,Car007.java 和接口 Common.java,要求在未来如果增加第 3 种交通工具的时图 1 Student.java 的运行界面候,不必修改以前的任何程序,只需要编写新的交通工具的程序。其运行过程如下,从命令行输入ComputeTime 的四个参数,第一个是交通工具的类型,第二、三、四个参数分别时整数 A、B、C,举例如下: 计算 Plane 的时间: “java ComputeTime Plane 20 30 40“ 计算 Car007 的时间:“java ComputeTime Car007 23 34

19、 45“ 如果第 3 种交通工具为 Ship,则只需要编写 Ship.java,运行时输入:“java ComputeTime Ship 22 33 44“ 提示:充分利用接口的概念,接口对象充当参数。 实例化一个对象的另外一种办法:Class.forName(str).newInstance() ;例如需要实例化一个 Plane 对象的话,则只要调用 Class.forName(“Plane“).newInstance()便可。ComputerTime.java 源程序:import pkg1.pkg2.*;import java.lang.*;public class ComputerTi

20、me public static void main(String args) System.out.println(“交通工具: “+args0);System.out.println(“ 参数 A: “+args1);System.out.println(“ 参数 B: “+args2);System.out.println(“ 参数 C: “+args3); double A=Double.parseDouble(args1); double B=Double.parseDouble(args2); double C=Double.parseDouble(args3);double v,

21、t;try Common d=(Common) Class.forName(“pkg1.“+args0).newInstance(); v=d.runtimer(A,B,C);t=1000/v;System.out.println(“平均速度: “+v+“ km/h“); System.out.println(“运行时间:“+t+“ 小时“); catch(Exception e) System.out.println(“class not found“);包 pkg1 中 Car007.java 的源程序:package pkg1;import pkg1.pkg2.*;public class Car007 implements Common public double runtimer(double A,double B,double C) System.out.println(“runtime=“+(A*B/C); return A*B/C;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育教学资料库 > 课件讲义

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。