1、测试题(四)答案 出卷人:王菲菲 时间:120分钟 一、选择题(每题5分,共50分) 1、下面哪个是Java语言中正确的标识符( C ) A、3com B、import C、that D、this 2、下面哪个语句(初始化数组)是不正确的:( B ) A.int x[] = {1,2,3}; B.int x[3] = {1,2,3}; C.int[] x = {1,2,3}; D.int x[] = new int[]{1,2,3}; 3、下述概念中不属于面向对象方法的是( D )。
2、 A.对象 B.继承、多态 C.类、封装 D.过程调用 4、下面的代码段中,执行之后i 和j 的值是什么? ( B ) int i = 1; int j; j = i++*2+3*--i; A.1, 2 B.1, 5 C. 2, 1 D. 2, 2 5.java.lang包的()方法比较两个对象是否相等,相等返回true。 A.toString() B.equals() C.compare() D.以上所有选项都不正确 答案:B 6.使用___()方法可以获得C
3、alendar类的实例。 A.get() B.equals() C.getTime() D.getInstance() 答案: D 7、给出下面代码: public class Person{ static int arr[] = new int[10]; public static void main(String a[]) { System.out.println(arr[1]); } } 哪个语句是正确的? ( B ) A.编译时将产生错误; B
4、输出零; C.编译时正确,运行时将产生错误; D.输出空。 8、下面关于java中类的说法哪个是不正确的?( C ) A.类体中只能有变量定义和成员方法的定义,不能有其他语句。 B.构造函数是类中的特殊方法。 C.类一定要声明为public的,才可以执行。 D.一个java文件中可以有多个class定义。 9、在Java 中如果要在字符串类型对象s=“JAVA”,中找出„V‟出现的位置可使用下面哪个方法:(C) A.mid(2,s); B.charAt(2); C.s.indexOf(“V‟); D.index
5、Of(s,‟V‟); 10、A派生出子类B ,B派生出子类C,并且在Java源代码中有如下声明: 1) A a0=new A(); 2) A a1 =new B(); 3) A a2=new C(); 问以下哪个说法是正确的? ( D ) A.只有第1行能通过编译 B.第1、2行能通过编译,但第3行编译出错 C.第1、2、3行能通过编译,但第2、3行运行时出错 D.第1行、第2行和第3行的声明都是正确的 二、填空题(每空4分,共20分) 1. 编译Java Application 源程序文件将产生相应的
6、字节码文件,这些字节码文件的扩展名为 类名.class 。 2.构造方法是一个特殊的方法,主要用于 创建对象时初始化对象 。构造方法的方法名要求与 ___类名_____相同,而且无返回值,构造方法无返回值,并不是要在构造方法名前加上 void/返回值 。 3. 异常处理过程中,一个try程序块可以对应 多 个catch块。 4. Java程序在extends之后只能有一个父类,即extends只能实现 1 个/单 继承。 三、编程题(每题10分,共30分) 1、编写一个程序,实现下列功能: (1)
7、测试两个字符串String str1=”It is”和String str2=”It is”是否相等 ; (2)、将” a book.”与其中的str1字符串连接 ; (3)、用m替换新字符串中的i ; 参考代码: public class Ex11 { public static void main(String[] args) { String str1=”It is”; String str2=”It is”; //比较字符串 System.out.println(“str1==str2的结果:“+(str1==str2)); System.out.p
8、rintln(“str1.equals(str2)的结果:“+str1.equals(str2)); //连接字符串 String str3=str1.concat(“a book”); System.out.println(“连接后的字符串为:“+str3); //替换字符 String str4=str3.replace(„i‟,'m‟); System.out.println(“替换后的字符串为:“+str4); } } 2、编程计算距当前时间10天后的日期和时间,并用“xxxx年xx月xx日”的格式输出新的日期和时间。 参考代码: import
9、 java.util.Calendar; public class Ex12 { public static void main(String[] args) { Calendar cal=Calendar.getInstance(); cal.add(Calendar.DAY_OF_YEAR,10); String strDate=cal.get(Calendar.YEAR)+”年“ +(cal.get(Calendar.MONTH)+1)+”月“ +cal.get(Calendar.DATE)+”日“; System.out.println(“10天后的日期
10、为:“+strDate); } } 3、请定义一个交通工具(Vehicle)的类其中有: 属性速度(speed)体积(size)等等 方法移动(move())设置速度(setSpeed(int speed))加速speedUp(),减速speedDown()等等. 最后在测试类Vehicle中的main()中实例化一个交通工具对象并通过方法给它初始化speed,size的值并且通过打印出来。另外调用加速减速的方法对速度进行改变。 参考代码: public class Vehicle { private int speed; priv
11、ate String size; public void move() { System.out.println("i'm moving"); } public void setSpeed(int speed) { System.out.println("now i'm running with"+speed+"per hour"); } public void speedUp() { Vehicle v=new Vehicle(); v.setSpeed(1000); } public void speedDown() { Vehicle v=new Vehicle(); v.setSpeed(20); } public static void main(String[] dsa) { Vehicle v=new Vehicle(); v.speed=100; v.size="50/m^3"; System.out.println("the initial speed is"+v.speed+"and my size is "+v.size); v.speedUp(); v.speedDown(); } }






