五、编程题(第1,2,3题10分,第4题15分)
1. 编写输出0—30之间的能被7整除的数的积的程序。3章4节3目39页
class Xxx{
public static void main(String[] args)
int t=1;
for(int i=7;i<=30;i=i+7) t=t*i; System.out.println(t);
}
}
{
2.编程,用二重循环对如下所示的5行5列矩阵赋值,并求主对角线上各元素之和。3章4节4目45页
class ArrayTest{ }
public static void main(String[] args) { }
int s=0,i,j;
int arr[][]=new int[5][5]; for(i=0;i<=4;i++) {for(j=0;j<=4;j++) {arr[i][j]=i*j;
System.out.print(arr[i][j]); if (i==j) s=s+arr[i][j]; }
System.out.println(); }
System.out.println(s);
3. 编程,从一给定字符串中统计出其中的大写英文字母和小写英文字母的个数,字符串变量可以在命令行参数中获得,也可以在程序中赋值。3章4节2目
class CountUpperandLower{ }
public static void main(String[] args) }
String str=args[0]; int s=0;
for(int i=0;i if ((str.charAt(i)>='a' &&str.charAt(i)<='z')|| (str.charAt(i)>='A' &&str.charAt(i)<='z')) s++; { System.out.print(s); 4.设计一个表示一个点的CCPoint类,该类包括以下属性和方法。6章2节1目84页 x1:点的横坐标;y1:点的纵坐标; CCPoint(double x1, double y1):构造函数,创建对象的同时为属性x1,y1赋初值; getX1() :获得点的横坐标; getY1(): 获得点的纵坐标; distance1(CCPoint p): 返回当前点与点p之间的距离。 测试点p1(100,100)到点p2(200,300)之间的距。 class CCPoint { } private double x1; private double y1; CCPoint(double x1,double y1) {this.x1=x1;this.y1=y1;} {return this.x1;}; public double getX1() public double getY1() {return this.y1;} { } return Math.sqrt((x1-p.x1)*(x1-p.x1)+(y1-p.y1)*(y1-p.y1)); double distance1(CCPoint p) public static void main(String[] args) { } CCPoint p1=new CCPoint(100,200); CCPoint p2=new CCPoint(200,300); System.out.println(p1.distance1(p2)); 第二套 一、单项选择题(每题1分,共15分) 1.下列哪个选项是正确的?D 6章1节2目76页 A.一个源文件可以有一个以上的public类 B.一个源文件只能供一个程序使用 C.一个源文件中只能有一个方法 D.一个程序可以包括多个源文件 2.下列关于抽象类的说法中,哪个选项是正确的?C 6章1节1目76页 A.某个抽象类的父类是抽象类,则这个子类必须重载父类的所有抽象方法 B.接口和抽象类是同一回事 C.绝对不能用抽象类去创造对象 D.抽象类中不可能有非抽象类的方法 3.下列关于main函数的定义,哪个选项是正确的?c 6章1节3目77页 A. public static void main(String args) B. void public static main(String args []) C. static public void main(String message[]) D. public static void Main(String [] arg) 4. 下列哪个选项是正确的? B 2章2节4目17页 A. byte b = 200; B. float f = 18.8f; C. char c = -28; D. int i = 079; 5.给出下列代码段,运行结果正确的选项是哪个?C 2章3节1目20页 public class Test2009{ public static void main(String args[]){ System.out.println((3==Math.sqrt(9))?4:5); } } A.2 B.3 C.4 D.5 6.若数组aaa定义为int[][] aaa=new int[3][4],aaa.length的值是什么?A 5章2节1目57页 A.3 B.4 C.12 D.1 7.给出下面代码段,下列哪个选项不会输出 \?D 3章3节2目35页 if ( x > 2) { System.out.println(\}else { if ( x > 5 ) { System.out.println(\ }else { } System.out.println(\} A. x = 0 B. x = 2 C. x = -2 D. x = 5 8.给出下面代码段,下面哪个选项是正确的? A 3章4节63页 public class test{ static int a[] = new int[10]; public static void main(String args[]) { System.out.println(a[10]); } } A.编译时将产生错误 C.输出零 switch(m) { B.编译时正确,运行时将产生错误 D.输出空 9.给出下面代码段,当m=0时,输出结果是什么? A 3章3节2目36页 case 0: System.out.println(\ case 1: System.out.println(\ case 2: default: System.out.println(\ } A.case 0 case1 B.case 0 C.default D.case 0 case 1 default 10.用来导入已定义好的类或包的语句是什么?B 6章1节3目77页 A.main B.import C.public class D.class 11.下列代表八进制整数的是哪个选项?A 2章2节3目16页 A.0123 B.1800 C.fa00 D.0xa2 12.下列叙述中,哪个选项是错误的?D 7章2节1目99页 A.父类不能替代子类 B.子类能够替代父类 C.子类继承父类 D.父类包含子类 13.给出下列的代码段,哪行在编译时可能会有错误? C 2章2节4目17页 1) public void modify(){ 2) int i, j, k; 3) i = 100; 4) while ( i > 0 ){ 5) j = i * 2; 6) System.out.println (\7) k = k + 1; 8) } 9) } A.第4行 class Test { B.第6行 C.第7行 D.第8行 14.给出下列代码,如何使成员变量m 被方法fun()直接访问? C 7章1节2目93页 private int m; public static void fun() { ... } } A.将private int m 改为protected int m B.将private int m 改为 public int m C.将private int m 改为 static int m D.将private int m 改为 int m 15.给出一java 源程序文件如下,保存它的源文件名应当是什么?a 1章4节1目84页 public class AAA{ ?? } class BBB{ `?? } A.AAA.java B.BBB.class C.任意合法的文件名 D.AAA.class 二、填空题(每题1分,共8分) 1.Java中访问限定符有 ,public,private和default等。 Protected 2章2节2目16页 2.下面代码段的运行结果是 。660 5章4节63页 public class Sum { public static void main(String args[]){ int i ,s=0; int a [] = { 10,20,30,40,50,60,70,80,90,100 }; } for (i = 0 ; i <= a.length/2 ; i ++) } s=s+a[i]+a[a.length-i-1]; System.out.print(s); 3.Java语言使用的字符码集是 。 Unicode 2章2节2目16页 4.执行下列语句后的输出结果是 。 4 8 3章4节3目39页 int a[] = { 1,2,3 ,4,5,6,7,8,9 }; for (int i=0 ; i if(a[i]%4==0 ) System.out.println(a[i]+\ 5.执行下列代码段的运行结果是 。0 2章3节1目20页 public class Test2{ public static void main (string args[]){ System.out.println(99%3);} } 6.设x为float型变量, y为 double型变量, a为 int型变量,已知 x=2.5f, a=7 ,y=4.22 则表达式x+a%3*(int)x%(int)y的值为________________。4.5 2章3节1目22页 7.设 a = 8,则表达式 a << 2 的值是 。 32 2章3节2目24页 8.运行下列代码段,while语句成功执行的次数是 。 3 2章4节1目37页 int x=3; while(x<9) x+=2; x++; 三、程序分析题(每题4分,共20分) 1.下列代码段运行时,在命令提示符后输入的命令是:Java Base Hello World 该段程序能否正确编译或输出?如果能,输出结果是什么?如果不能,为什么? _____________________________________________能正确编译并输出World 5章5节66页 class TestArgs { public static void main(String args[]) { System.out.println(args[1]); } } 2.下列代码段运行后,输出结果是______________________________________Default 3章3节2目35页 char x = '2'; switch (x) { case 1: System.out.println(\ break; default: System.out.println(\