《java程序设计》期末复习题 下载本文

2009-2010第二学期《java程序设计》复习题

可能题型:选择、填空、简答、判断

练习一(两类java程序) ................................................ 1 练习二(数据类型) ..................................................... 2 练习三(运算符与表达式) ............................................... 3 练习四(条件,分支语句) ................................................ 4 练习五(条件,分支语句) ................................................ 5 练习六(控制结构与循环体内的跳转) ..................................... 8 练习七(方法) ....................................................... 8 练习八(方法重载) ..................................................... 9 练习九(数组) ......................................................... 9 练习十 (继承:类,属性和方法的继承,属性隐藏和方法覆盖,构造函数的继承,父类对象和子类对象的关系) ............................................... 11 练习十一(多态:抽象类和抽象方法,最终类和最终方法,接口) ............... 16 练习十二(异常处理) ................................................ 17 练习十三(杂) ...................................................... 19 练习十四(io) ...................................................... 22 练习十五(线程) .................................................... 23 练习十六(网络) .................................................... 25 练习十七(gui) ..................................................... 27 练习十八(杂) ...................................................... 28

练习一(两类java程序)

1.Java是低级语言还是高级语言 (高级) 2.Java是面向对象的程序设计语言吗 (是)

3. Java是编译型的计算机语言还是解释型的计算机语言 (解释型)

4. Java语言的程序设计包含哪3个步骤 (编写源程序,编译源程序,解释运行编译后形成的字节码文件)

5. Java源程序文件的后缀是什么 (.java)

6. Java源程序经编译后生成什么文件 其后缀是什么 (字节码文件,.class) 7. Java程序有哪两类 (java应用程序,java小程序) 8.类的定义由哪两部分组成 (类头和类体)

9.方法的定义由哪两部分组成 (方法头和方法体)

10.一个Java源程序中可以有多个公共类(主类)吗 (不能)

11.一个Java源程序中可以有多个类吗 若有多个类时,程序文件名是什么 (能,与公共类名一致)

12. Java应用程序(Application)的主类的类头应如何写(public class 类名), 它的主方法的方法头应如何写(public static void main(String args[])), Application 的入口在哪(main())

13.Java应用程序(Application)的主类中一定要有主方法(main)吗 ?是 14.Java中的的字符使用什么编码 (unicode)

15. 在Java语言中,将源代码翻译成____________时产生的错误称为编译错误,而将程序在

1

运行中产生的错误称为运行错误.(字节码)

16. 为了使包sos中的所有类在当前程序中可见,可以使用的语句是( A ). A)import sos.*; B)package sos.*; C)sos import; D)sos package;

17. 下面关于封装性的描述中,错误的是( d )。

A.封装体包含属性和行为 B.被封装的某些信息在外不可见

C.封装提高了可重用性 D.封装体中的属性和行为的访问权限相同

练习二(数据类型)

1.以下的选项中能正确表示Java语言中的一个整型常量的是( B ). A) 12. B) -20 C) 1,000 D) 4 5 6 2.下列的变量定义中,错误的是( A). A) int 6_a; B) float a;

C) int i=Integer.MAX_VALUE; D) static int i=100; 3.下列的变量定义中,错误的是(A). A) int a;b; B) float a,b1=1.23f;

C) char ch1='d',ch2='\\''; D) public int i=100,j=2,k; 4.下列的变量定义中,错误的是(D).

A) int _a=123; B) long j=12345678900L; C) int m,n; D) static i=100; 5.下列的变量定义中,正确的是(D).

A) boolean b1=\C) byte i=200; D) double y; 6.以下字符常量中不合法的是(C). A) '@' B) '&' C) \整'

7.以下的选项中能正确表示Java语言中的一个整型常量的是( A ). A) Integer.MAX_VALUE B) 8.8F C) 2.0E16 D)true 8.下列最终属性(常量)i 的定义中,正确的是(C). A) static final double i; B) abstract final int i=8; C) final double i=3.14159; D) float i=1.2f;

9.以下的选项中能正确表示Java语言中的一个double型常量的是(C). A) Float.MIN_VALUE B) 1.8e-6F C) 2.0 D)'abcf' 10.以下的变量定义语句中,合法的是(C). A) float $_*5= 3.4F; B) byte b1= 15678;

C) double a =Double. MAX_VALUE; D) int _abc_ = 3721L; 11.以下字符常量中不合法的是(C). A) '|' B) '\\'' C) \我’ 12. Java语言中的浮点型数据根据数据存储长度和数值精度的不同,进一步分为哪两种具体类型,各占二进制的多少位 (float 32位,double 64位) 13. Java的复合数据类型有那几种 (类,数组和接口) 14.在Java语言中,逻辑常量只有哪两个值 (true,false) 15.字符串 \的长度是几(即有几个字符) (3个字符)

16. Java语言中的逻辑变量可以和整型变量相互强制转换吗 (不能) 17. 在程序中可对用final修饰的变量的值进行更改吗 (不能)

2

18.Java的字符类型采用的是Unicode编码方案,每个Unicode码占用(B )个比特位。 A、8 B、16 C、32 D、64

练习三(运算符与表达式)

1.已定义a为int类型的变量.以下选项中,合法的赋值语句是( B ). A) a +1==2; B) a+=a*6; C) a=8.8f; D) int a=8;

2.以下选项中的变量都已正确定义,不合法的表达式是(C ).

A) a >= 4 = = b3; B)t=!false; C)t=(true|false); D)t=(2==3) true:false; 3.以下变量均已正确定义并赋值,下面符合Java语言语法的赋值语句是(A ). A) m = n>=5; B) m = k % c=h+y;

C) j=t+ % 100; D) j = y > 2= x + 3;

4.设 i, j, k, m为已赋值的 int 型变量,下列表达式的结果属于逻辑值的是( A). A) i==k & j+k >=m B) i+k C) m++ D) m=i+j

5.变量a定义为int类型.以下选项中,合法的赋值语句是( B ). A) a +1==2; B) a+=1; C) a=8.8f; D) a=new int(8);

6.若有定义int x = 13;则执行完语句x %= 8; 后,x的值是(C ). A) 0 B) 1 C) 5 D) 8

7.以下选项中,合法的赋值语句是( B ).

A) a = = 1; B) ++ i; C) a=a + 1= 5; D) !(++a!=b- -); 8.若所用变量都已正确定义,以下选项中,非法的表达式是( C). A) a != 4||b= =1 B) 'a' % 3 C) 'a' = 1/2 D) 'A' + 32

9.若有定义int a = 2;则执行完语句 a -= a * a; 后,a的值是( C). A) 0 B) 4 C) -2 D) –4

10.现有一变量声明为boolean aa;下面赋值语句中正确的是( A ). A) aa=false; B) aa=False; C) aa=\11. 下列程序段执行后 b3 的结果是(D). boolean b1=true, b2=true, b3; b3= !b1 b1 :!b2;

A) 0 B) 1 C) true D) false

12. 设有定义 int i=123; long j=456; 下面赋值不正确的语句是(D). A) j=i; B) j=(long)i; C) i=(int)j; D) i=j;

13.若以下变量均已正确定义并赋值,下面符合Java语言语法的语句是( A). A)b=a!=7; B)a=7+b+c=9; C)i=12.3* %4; D)a=a+7=c+b; 14.下列程序段执行后t5的结果是(B ). int t1 = 9, t2 = 11, t3=8; int t4,t5;

t4 = t1 > t2 t1 : t2+ t1; t5 = t4 > t3 t4 : t3; A) 8 B) 20 C) 11 D) 9

15.执行下面程序段后,b, x, y的值分别是( D). int x=6,y=8; boolean b;

b=x>y&&++x==--y;

A)true , 6, 8 B)false , 7, 7 C)true, 7, 7 D)false, 6, 8

3

16.执行下列程序段后,m,x,y的值分别是( B). int x=2,y=4; boolean m; m=++x>y--;

A)true , 2,4 B)false , 3,3 C) true, 3,3 D) false, 2,4 17.下面语句序列执行后,a的值是(D ). int a=13; a%=a/5;

A) 3 B) 13 C) 169 D) 1

18.设x,y,max,min均为int型变量,x,y已赋值.用三目条件运算符求x,y的最大值和最小值,并分别赋给变量max和min,这两个赋值语句分别是______和______.(max=x>y x:y; min=x

练习四(条件,分支语句)

1.下列语句序列执行后,m 的值是( C). int a=10, b=3, m=5;

if( a==b ) m+=a; else m=++a*m; A) 15 B) 50 C) 55 D) 5

2.下列语句序列执行后,x 的值是(d). int a=2, b=4, x=5; if( a<--b ) x*=a;

A) 5 B) 20 C) 15 D) 10

3.下列语句序列执行后,k 的值是(C ). int i=10, j=18, k=30; switch( j - i ) { case 8 : k++; case 9 : k+=2; case 10: k+=3; default : k/=j; }

A) 31 B) 32 C) 2 D) 33

4.若a和b均是整型变量并已正确赋值,正确的switch语句是(D ). A) switch(a+b); { ...... } B) switch( a+b*3.0 ) { ...... } C) switch a { ...... } D) switch ( a%b ) { ...... } 5.下列语句序列执行后,r 的值是( C). char ch='8'; int r=10; switch( ch+1 ) {

case '7': r=r+3; case '8': r=r+5;

case '9': r=r+6; break; default: r=r+8; }

A) 13 B) 15 C) 16 D) 18

6.设int 型变量 a,b,float 型变量 x,y,char 型变量 ch 均已正确定义并赋值,正确的

4

switch语句是(B ).

A) switch (x + y) { ...... } B) switch ( ch + 1 ) { ...... } C) switch ch { ...... } D) switch ( a + b ); { ...... } 7.下列语句序列执行后,k 的值是(D ). int x=6, y=10, k=5; switch( x%y ) {

case 0: k=x*y; case 6: k=x/y; case 12: k=x-y; default: k=x*y-x; }

A) 60 B) 5 C) 0 D) 54

8.下列语句序列执行后,r 的值是(B ). char ch='A'; int r=6; switch( ch+5 ) {

case 'A': r=r+3; case 'B': r=r+5;

case 'C': r-=6; break; default : r/=2; }

A) 11 B) 3 C) 2 D) 9

9.如果变量x已正确定义,写出实现以下功能的语句:如果变量x是偶数,则将其增加2. ______________________ ;.(if(x%2= =0) x+=2;) 10. 设ch1, ch2是 char 型变量: if ( ch1 == 'a' ) { if ( ch2 == 'b' )

System.out.print(\} else

System.out.print(\System.out.println(\

问题: 1)若执行前 ch1 的值为 'a', ch2 的值为 'c' 该程序段输出是什么 (end) 2)若执行前 ch1 的值为 'w', ch2 的值为 'b' 该程序段输出是什么 (ch1!='a'end)

3)若执行前 ch1 的值为 'a', ch2 的值为 'b' 该程序段输出是什么 (ch1='a',ch2='b'end)

练习五(条件,分支语句)

1.下列语句序列执行后,j 的值是( A ). int j=1;

for( int i=5; i>0; i-=2 ) j*=i; A) 15 B) 1 C) 60 D) 0

2.以下由 for 语句构成的循环执行的次数是(B ).

5

for ( int i = 0; true ; i++) ; A)有语法错,不能执行 B)无限次 C)执行1次 D)一次也不执行 3.下面语句执行后,i 的值是(B).

for( int i=0, j=1; j < 5; j+=3 ) i=i+j; A) 4 B) 5 C) 6 D) 7

4.下列语句序列执行后,k的值是(D). int j=8, k=15;

for( int i=2; i!=j; i++ ) { j-=2; k++; } A) 18 B) 15 C) 16 D) 17

5.下列语句序列执行后,k 的值是(B). int m=3, n=6, k=0;

while( (m++) < ( -- n) ) ++k; A) 0 B) 1 C) 2 D) 3

6.下列语句序列执行后,i 的值是( D). int s=1,i=1;

while( i<=4 ) {s*=i;i++;} A) 6 B) 4 C) 24 D) 5

7.下列语句序列执行后,j 的值是( C ). int j=8, i=6;

while( i >4 ) i-=2; - -j;

A) 5 B) 6 C) 7 D) 8

8.下列语句序列执行后,j 的值是( B ). int j=9, i=6;

while( i-- >3 ) --j; A) 5 B) 6 C) 7 D) 8

9.下列语句序列执行后,j 的值是(D). int j=3, i=2;

while( --i!=i/j ) j=j+2; A) 2 B) 4 C) 6 D) 5 10. 若有循环: int x=5,y=20;

do { y-=x; x+=2; }while(x

A) 2次 B) 1次 C) 0次 D) 3次

11.下列语句序列执行后,i的值是(D). int i=10;

do { i/=2; } while( i--> 1 ); A) 1 B) 5 C) 2 D) -1

12.下列语句序列执行后,i的值是(C ). int i=10;

do { i-=2; } while( i>6 ); A) 10 B) 8 C) 6 D) 4

6

13.下列语句序列执行后,i的值是(D). int i=16;

do { i/=2; } while( i > 3 ); A) 16 B) 8 C) 4 D) 2

14.以下由do-while语句构成的循环执行的次数是(B). int k = 0;

do { ++k; }while ( k < 1 );

A)一次也不执行 B)执行1次 C)无限次 D)有语法错,不能执行 15. 若有循环: int x=5,y=20;

do{ y-=x; x++; } while(++x<--y); 则循环体将被执行(C ). A)0次 B)1次 C)2次 D)3次

16.下面是一个java应用程序(Application),它的功能是在屏幕上输出26个小写英文字母,其中每个字母相隔一个制表符,请完成程序填空. (class (c++)+\或 (c++)+'\\t') public _____ Class1 {

public static void main( String args[] ) {

char c='a';

for (int i=1;i<=26;i++)

{ System.out.print(______); } } }

17.下面是一个java应用程序(Application),它的功能是计算 s=1+2+3+...+10 ,请完成程序填空.(class i) public _____ Class1 {

public static void main( String args[] ) {

int s=0;

for (int i=1;i<=10;i++) {

s+=______; }

System.out.println(\} }

18.下面是一个java Application 的主类的定义,其功能是输出乘法口诀表第一列,请完成程序填空.(i<=9 j*i) import java.io.*; public class MyClass {

7

public static void main(String[] args) {

int j=1;

for(int i=1; _______; i++) {

System.out.println(i+\} }}

练习六(控制结构与循环体内的跳转)

1.结构化程序设计的三种基本流程控制结构是:_______,_______,________. (略) 2. 以下语句中能构成分支的语句是(B). A) for 语句 B) if 语句

C) continue 语句 D) 方法调用语句

3. 以下语句中能构成多分支的语句是( C ). A) for 语句 B) while 语句

C) switch 语句 D) do - while 语句

4. 以下语句中能构成多分支的语句是( C ). A)for语句 B)while语句

C)if__else语句的嵌套 D)do__while语句 5. 以下语句中是循环语句的是( D ).

A)break 语句 B)switch 语句 C)if 语句 D)do - while 语句 6. 以下语句中不能构成循环的语句是( C ).

A) for 语句 B) while 语句 C) switch 语句 D) do__while 语句 7.能从循环语句的循环体中跳出的语句是(B).

A)for语句 B)break语句 C) continue语句 D)return语句 8.阅读程序后回答问题 . public class ContinueTest

{public static void main( String args[]) {char c='i';

for (int n=6; n < 11; n++ ) {if ( n == 8 ) continue;

System.out.print(\}}}

问题:1)程序的输出结果是什么 (i j k l)

2)若将if中的continue语句替换为break语句,输出结果是什么 (i j)

练习七(方法)

1.以下方法m的功能是求两参数之积的整数部分. (return (int)(x*y)) int m ( float x, float y ) { __________________; } 2. 方法可以没有返回值吗 可以有多个返回值吗 (可以;不能)

3. 一个方法如果没有返回值,方法头定义中的返回值类型为何 (void)

4. 一个方法或一个复合语句内定义的变量是否可以在方法外或复合语句外使用 这种变量

8

称为什么变量 (不能;局部变量)

5. 采用_______的程序设计原则,使程序结构清晰简单,设计容易,有助于软件可靠性的提高.(模块化)

6.下面方法的功能是判断一个整数是否为偶数,将程序补充完整.(boolean true) public ________ isEven(int a) { if(a%2==0) return _____; else

return false; }

练习八(方法重载)

1.同一个类中多个方法具有相同的方法名,不同的_____________称为方法的重载.(参数列表)

2.在某个类中存在一个方法:void getSort(int x),以下能作为这个方法的重载的声明的是(C).

A)public getSort(float x) B)int getSort(int y)

C)double getSort(int x,int y) D)void get(int x,int y)

3.在某个类中存在一个方法:void sort(int x),以下不能作为这个方法的重载的声明的是(B).

A)public float sort(float x) B)int sort(int y) C)double sort(int x,int y) D)void sort(double y) 4. 为了区分类中重载的同名的不同方法,要求( A). A) 采用不同的形式参数列表 B) 返回值类型不同 C) 调用时用类名或对象名做前缀 D) 参数名不同

5. 重载的方法可以通过它们的返回值类型的不同来区分吗 (不能) 6. 编译程序如何区分重载方法的调用 (根据参数列表不同来区分)

练习九(数组)

1. 在一个应用程序中定义了数组a:int[ ] a={1,2,3,4,5,6,7,8,9,10};,为了打印输出数组a的最后一个数组元素,下面正确的代码是( B).

A) System.out.println(a[10]); B) System.out.println(a[9]);

C) System.out.println(a[a.length]); D) System.out.println(a(8)); 2.下面关于数组定义语句不正确的是( D ).

A)int[ ] a1,a2; B)int a0[ ]={11,2,30,84,5};

C)double[] d=new double[8]; D)float f[ ]=new {2.0f,3.5f,5.6f,7.8f};

3.设有定义语句int a[ ]={3,9,-9,-2,8}; 则以下对此语句的叙述错误的是(C ). A) a数组有5个元素 B) 数组中的每个元素是整型

C) a的值为3 D) 对数组元素的引用a[a.length-1]是合法的

4.设有定义语句int a[ ]={66,88,99}; 则以下对此语句的叙述错误的是(C). A) 定义了一个名为a的一维数组 B) a数组有3个元素 C) a数组的元素的下标为1~3 D)数组中的每个元素是整型

5. 为了定义三个整型数组a1,a2,a3,下面声明正确的语句是( B ). A)intArray [ ] a1,a2; int a3[ ]={1,2,3,4,5};

9

B)int [ ] a1,a2; int a3[ ]={1,2,3,4,5}; C)int a1,a2[ ]; int a3={1,2,3,4,5}; D)int [ ] a1,a2; int a3=(1,2,3,4,5);

6.设 i,j 为int型变量名,a 为int型数组名,以下选项中,正确的赋值语句是(B). A) i = i + 2 B) a[0] = 7; C) i++ - --j; D) a(0) = 66; 7. 若已定义byte[ ] x={11,22,33,-66};

其中0≤k≤3,则对x数组元素错误的引用是( C). A) x[5-3] B) x[k] C) x[k+5] D) x[0]

8.设有定义 int[ ] a=new int[4]; ,a的所有数组元素是(B). A) a0, a1, a2, a3 B) a[0], a[1], a[2], a[3]

C) a[1], a[2], a[2], a[4] D) a[0], a[1], a[2], a[3], a[4]

9.设有整型数组的定义:int a[ ]=new int[8]; ,则a.length的值为_____.(8)

10. 定义数组,需要完成以下三个步骤,即:________,_______和________ .(声明数组;创建数组空间;初始化)

11. 在Java语言中,所有的数组都有一个_________属性,这个属性存储了该数组的元素的个数(数组长度).(length)

12. 若有定义 int[ ] a=new int[8]; 则a的数组元素中第7个元素和第8个元素的下标分别是____和____.(6 7)

13.定义一个整型数组y,它有5个元素分别是1,2,3,4,5.用一个语句实现对数组y的声明,创建和赋值:__________________;. (int y[]={1,2,3,4,5};)

14. 数组一定占用连续的内存空间吗 (是)

15.下面是一个类的定义,请将其补充完整.(class static) ___________ A { String s;

_____ int a=666;

A(String s1) { s=s1; }

static int geta( ) { return a; } }

22.阅读程序,回答问题. public class Test52 {

String static str1=\String static str2=\

public static void main(String args[])

{ System.out.print(str1); System.out.println(str2); } }

问题:1)这是哪种形式的 java 程序 (java应用程序) 2)程序的输出是什么 输出的结果在 Applet 窗口中吗 (Hello, Java world! Hello, students! 不在)

10

练习十 (继承:类,属性和方法的继承,属性隐藏和方法覆盖,构造函数的继承,父类对象和子类对象的关系)

1. Java语言的类间的继承关系是( B ).

A) 多重的 B) 单重的 C) 线程的 D) 不能继承 2. 以下关于Java语言继承的说法正确的是( C ).

A)Java中的类可以有多个直接父类 B)抽象类不能有子类 C)Java中的接口支持多继承 D)最终类可以作为其它类的父类 3. 下面是有关子类继承父类构造函数的描述,其中正确的是( B). A) 如果子类没有定义构造函数,则子类无构造函数

B) 子类构造函数必须通过super关键字调用父类的构造函数 C)子类必须通过this关键字调用父类的构造函数 D)子类无法继承父类的构造函数

4. 现有两个类A,B,以下描述中表示B继承自A的是(D). A) class A extends B B) class B implements A C) class A implements B D) class B extends A

5. 子类可以继承父类的__________成员(非私有).构造函数也能被继承和重载.

6. 如果子类中的某个变量的变量名与它的父类中的某个变量完全一样,则称子类中的这个变量________了父类的同名变量.(隐藏)

8. 如果子类中的某个方法的名字,返回值类型和________与它的父类中的某个方法完全一样,则称子类中的这个方法覆盖了父类的同名方法.(参数列表) 9. 抽象,封装,_____和多态是Java语言的四大特性.(继承) 11. Java 仅支持类间的____重继承.(单重)

12. 子类的成员变量能与其父类的成员变量同名吗 (能)

13. 什么是方法覆盖 (子类重新定义继承自父类的同名方法,使之实现与父类方法不同的功能;方法覆盖要求子类方法头定义必须与父类方法头定义完全一样.)

14. 一个子类可以重新定义从父类那里继承来的同名方法.但是,允许它们有不同类型的返回值吗 (不能)

15. 子类对象如何引用被它隐藏了的父类的属性和调用被它覆盖了的父类的方法 (使用super关键字做前缀)

16. 子类的对象能直接向其父类对象赋值吗 反之呢 子类对象一定是父类的对象吗 (能;不能;是;)

18. java中,一个子类可以有多个父类吗 可以为一个(父)类定义多个子类吗 (不能;可以) 19. 保留字 this 代表什么 (this表示类当前对象本身)

21.下面是一个类的定义,完成程序填空.(Youwrite this.x=x;) public class Youwrite { int x;

_______( ) {x=0;} Youwrite ( int x) { _____; } }

22.现有类说明如下,请回答问题: public class A {

11

String str1=\String str2=\public String toString( ) { return str1+str2; } }

public class B extends A {

String str1=\public String toString( ) { return super.str1+str1; } } 问题:

1)类A和类B是什么关系 (继承关系)

2)类A和类B都定义了str1属性和方法toString( ), 这种现象分别称为什么 (属性的隐藏;方法的覆盖)

3)若a是类A的对象,则a.toString( )的返回值是什么 (Hello! How are you )

4)若b是类B的对象,则b.toString( )的返回值是什么 (Hello,Bill.) 23.现有一个类定义如下,请回答问题: class Employee {

String name; int age; double wage; static int No=0;

Employee(String a1,int a2,double a3) {

name=a1; age=a2; wage=a3; No++; } }

在使用该类时,已使用下面语句生成了该类的对象: Employee e1,e2;

e1=new Employee(\王劲\e2=new Employee(\张山\问题:

1)e2.name,e2.age,e2.wage的值各是什么 (张山;30;3800.0)

2)生成对象e1,e2后,e1.No值为多少 能否通过类名做前缀引用属性No (2; 能) 25.阅读程序,回答问题. public class InheritTest1 {

public static void main (String[] args) {

12

A aa; B bb;

aa=new A( ); bb=new B( ); aa.show( ); bb.show(); }}

class A {

int a=1;

double d=2.0; void show( )

{ System.out.println(\}

class B extends A {

float a=3.0f;

String d=\int b=4;

void show( ) {

System.out.println(\super.show( );

System.out.println(\}}

问题:1)这是哪一类java程序 (java应用程序) 2)类A和类B是什么关系 (类B是类A的子类) 3)按程序输出的格式写出程序运行后的结果. (程序运行结果如下: Class A: a=1 d=2.0 Class A: a=1 d=2.0 Class A: a=1 d=2.0

Class B: a=3.0 d=Java program. b=4 ) 26.现有类说明如下,请回答问题: class A {

int x=10;

int getA(){return x;} }

class B extends A {

int x=100;

int getB(){return x;} }

问题:1)类B是否能继承类A的属性x (能)

2)若b是类B的对象,则b.getB()的返回值是什么 (100) 3)若b是类B的对象,则b.getA()的返回值是什么 (10)

13

4)类A和类B都定义了x属性,这种现象称为什么 (属性的隐藏)

27.有如下源程序,请回答问题: class A

{ String s=\class B extends A

{ String s=\

public class TypeConvert {

public static void main(String args[]) {

B b1,b2=new B(); A a1,a2; a1=(A)b2; a2=b2;

System.out.println(a1.s); System.out.println(a2.s); b1=(B)a1;

System.out.println(b1.s); System.out.println(b2.s); }}

问题: 该程序的四行输出各是什么 (class A class A class B class B)

28.现有类说明如下,请回答问题: public class A {

int x=888;

String str=\

public String toString() { return str+x; } }

public class B extends A {

String x=\

public String toString()

{ return str+x+\

问题:1)类A和类B是什么关系 (类B是类A的子类)

2)类A和类B都定义了方法toString(),这种现象称为什么 (方法的覆盖)

14

3)若a是类A的对象,则a.toString( )的返回值是什么 (I like: 888) 4)若b是类B的对象,则b.toString( )的返回值是什么

I like:java and 888

29下面的是关于类及其修饰符的一些描述,不正确的是:( b )。

A、abstract类只能用来派生子类,不能用来创建abstract类的对象。 B、final类不但可以用来派生子类,也可以用来创建final类的对象。 C、abstract不能与final同时修饰一个类。

D、abstract方法必须在abstract类中声明,但abstract类定义中可以没 有abstract方法。

30.设有下面两个赋值语句:

a = Integer.parseInt(“1024”);

b = Integer.valueOf(“1024”).intValue(); 下述说法正确的是( d )。

A、a是整数类型变量,b是整数类对象。 B、a是整数类对象,b是整数类型变量。

C、a和b都是整数类对象并且它们的值相等。 D、a和b都是整数类型变量并且它们的值相等。 31. class X {

Y b = new Y(); X() {

System.out.print(\ } }

class Y { Y() {

System.out.print(\ } }

public class Z extends X { Y y = new Y(); Z() {

System.out.print(\ }

public static void main(String[] args) { new Z(); }} A.Z B.YZ C.XYZ D.YXYZ 答案D

32. 6.关于实例方法和类方法,以下描述正确的是: (D)

A. 实例方法只能访问实例变量

15

B. 类方法既可以访问类变量,也可以访问实例变量 C. 类方法只能通过类名来调用

D. 实例方法只能通过对象来调用 33. 下列关于继承的说法哪个是正确的? B

A.子类只继承父类public方法和属性; B.子类继承父类的非私有属性和方法;

C.子类只继承父类的方法,而不继承父类的属性;’ D.子类将继承父类的所有的属性和方法。

34. 你想要增加一个功能给wait()方法。你将如何来做? d a. 重载wait()方法。 b. 覆盖wait()方法。

c. 不能增加功能,因为他是Object类的成员,而Object类是所有类的基类。 d. 不能增加任何功能,因为wait()已经被最终定义。

练习十一(多态:抽象类和抽象方法,最终类和最终方法,接口)

1. 下列选项中,用于定义接口的关键字是( A).

A)interface B) implements C) abstract D) class 2. 下列选项中,用于实现接口的关键字是( B).

A)interface B) implements C) abstract D) class 3. 抽象方法只有方法头,没有________. (方法体) 4. Java 语言的接口是特殊的类,其中包含______常量和_______方法.(static(静态) ; abstract(抽象))

5. 接口中所有属性均为_________,_________和________的.(public,static,final) 6. 用那个关键字定义抽象类和抽象方法 (abstract)

7. 抽象类只能做为父类吗 (也可以包含抽象方法,做为抽象子类) 8. 可以定义抽象类的对象吗 (不能) 9. 抽象方法能被继承吗 (能)

10. 抽象方法只能存在于抽象类中吗 (是;也可以存在在接口中) 11. 声明为final的方法能在子类中重新定义吗 (不能) 12 用那个关键字定义最终类和最终方法 (final) 13. 最终类能做为父类吗 (不能) 14. 最终方法能被继承吗 (不能)

15. 最终方法只能存在于最终类中吗 (不一定)

16. 声明为 static和private 的方法是final的吗 (不是) 17. 接口中所有方法均为抽象方法吗 (是) 18. 接口中所有方法均为public的吗 (是)

19. abstract 和 final能同时修饰一个类吗 (不能) 20.下面是定义一个接口ITF的程序,完成程序填空. (interface abstract ) public _______ ITF {

public static final double PI=Math.PI;

public ______ doubl e area(double a, double b); }

16

21.下面是定义一个接口A的程序,完成程序填空.(final \public interface A {

public static _______ double PI=3.14159;

public abstract double area(double a, double b)_____ }

22.在使用interface声明一个接口时,只可以使用( d )修饰符修饰该接口。 A、private B、protected C、private protected D、public 23.关于接口的定义和实现,以下描述正确的是: (A) A. 接口定义中的方法都只有定义没有实现

B. 接口定义中的变量都必须写明final和static

C. 如果一个接口由多个类来实现,则这些类在实现该接口中的方法时应采用统一的代码

D. 如果一个类实现一个接口,则必须实现该接口中的所有方法,但方法未必声明为public.

24. 下列关于抽象类的说法四个是正确的?C

A.某个抽象类的父类是抽象类,则这个子类必须重载父类的所有抽象方法; B.接口和抽象类是同一回事; C.绝对不能用抽象类去创建对象; D.抽象类中不可以有非抽象方法。

练习十二(异常处理)

1、异常是一种运行时错误,在java中,异常是以对象的方法表示的,任何异常对象都是__________类或其子类的对象。(Throwable)

2、java把它能够识别的异常分为两大类:检查的和非检查的异常。非检查的异常是Error类和______________类的实例,它代表程序不必处理严重系统错误;检查异常是

____________类的子类,它代表程序应该而且能够处理的错误。(RuntimeException、 Exception)

3、Java的异常处理模型以3种操作为基础:声明异常、抛出异常和捕捉异常。当程序语句导致错误时,包含语句的方法将创建异常对象并把它传递到系统,这个过程称为_______________,查找异常处理程序的过程称为捕捉异常。(抛出异常) 4、java中的异常处理通过5个关键字来实现:它们是try、catch、throw、throws和finally。进行异常监测的代码要放在try子句内:捕获异常需要使用______________子句;在方法中宣布可能产生的异常应使用________________关键字。(catch、throws) 5、下列说法正确的是(D )

A. try-catch-finally代码块中的finally子句只有在try子句和catch子句都执行执行之后才执行

B. 如果try代码块中出现了异常并被catch子句捕捉到,那么finally子句就不再执行了 C. 只有try代码块中没有出现异常时,finally子句才会执行

D. 不管try代码块中是否出现异常,或者catch子句是否捕捉到异常,finally子句都将被执行

6、关于异常的应用,下列说法恰当的是( B )

17

A. 在java中,使用异常很方便、很有效,因而,只要能使用异常的地方应尽量使用异常 B. 在程序中过多的使用异常,会使程序的执行效率降低

C. 异常产生后,我们只需捕获它,至于是否对异常给与处理,无所谓 D. Java中的异常全部都被预先定义好了,程序员无需自行定义异常类 7、关于异常的捕获和处理,下列说法不正确的是( D )

A. 与一个try相关的catch语句可以有多个,但一个catch子句只能捕获一种类型的异常 B. 由内部try代码块产生的异常如果没有被与该try代码块相关的catch捕获,或者被JVM捕获和处理

C. 捕获资料异常的catch子句必须出现在捕获超类异常的catch子句的前面 D. 在一个try子句中产生的异常,必须由该try子句相关的catch子句捕获和处理,否则,就会造成程序非正常终止。

8、 当方法遇到异常又不知如何处理时,下列 C 做法是正确的。

A、捕获异常 B、抛出异常 C、声明异常 D、嵌套异常 9、下列常见的系统定义的异常中,哪个是数组越界异常?a A)ArrayIndexOutOfBoundsException B)IOException

C)NullPointerException D)ArithmeticException 10.public class ThrowsDemo { static void throwMethod() {

System.out.println(\ throw new IllegalAccessException(\ }

public static void main(String args[]) { try {

throwMethod();

} catch (IllegalAccessException e) { System.out.println(\ } } }

A.编译错 B.运行错 C.编译成功,但是没有输出结果 D.以上都不是 答案:A

11. 如果一个程序段中有多个catch,则程序会按如下哪种情况执行?C A找到适合的例外类型后继续执行后面的catch. B找到每个符合条件的catch都执行一次。

C找到适合的例外类型后就不再执行后面的catch。 D对每个catch都执行一次。

12. 下列代码行在执行时会出现一个运行时错误ArrayOutOfBoundsException,同时程序的执行终止。

cstObjects[ctr].displayDetails();

要避免上面提到的运行时错误你应该采取下列哪一个行动? d a. 在finally块中写入代码。

b. 使用throw 声明来发出异常警报ArrayOutOfBoundsException并且在catch块中捕获

18

一个异常。

c. 在try 块中写入代码并且在finally块中捕获异常ArrayOutofBoundsException。 d. 在try 块中写入代码并且在catch块中捕获异常ArrayOutofBoundsException。

练习十三(杂)

一、选择题。

2. main方法是Java Application程序执行的入口点,关于main方法的方法头以下哪项是合法的( )?

A、public static void main( )

B、public static void main( String args[] ) C、public static int main(String [] arg ) D、public void main(String arg[] ) 答:B

3.为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为( )。

A、static void method( ) B、public void method( ) C、final void method( ) D、abstract void method( ) 答:A

4.Java Application源程序的主类是指包含有( )方法的类。 A、main方法 B、toString方法

C、init方法 D、actionPerfromed方法 答:A

5.Java的字符类型采用的是Unicode编码方案,每个Unicode码占用( )个比特位。

A、8 B、16 C、32 D、64 答:B

6.若a的值为3时,下列程序段被执行后,c的值是多少?( ) c = 1;

if ( a>0 ) if ( a>3 ) c = 2; else c = 3; else c = 4; A、1 B、2 C、3 D、4 答:C

7.在编写Java Application程序时,若需要使用到标准输入输出语句,必须在程序的开头写上( )语句。

A、import java.awt.* ; B、import java.applet.Applet ; C、import java.io.* ; D、import java.awt.Graphics ; 答:C

8.对于构造函数,下列叙述不正确的是( )。

A、构造函数是类的一种特殊函数,它的方法名必须与类名相同。 B、构造函数的返回类型只能是void型。

C、构造函数的主要作用是完成对类的对象的初始化工作。 D、一般在创建新对象时,系统会自动调用构造函数。 答:B

9.设有下面两个类的定义:

class Person { class Student extends Person

19

{

long id; // 身份证号 int score; // 入学总分 String name; // 姓名 int getScore(){ } return score; } } 问:类Person和类Student的关系是( )。

A、包含关系 B、继承关系

C、关联关系 D、无关系,上述类定义有语法错误 答:B、

10. 设有下面的两个类定义: class AA {

void Show(){ System.out.println(“我喜欢Java!”); }

class BB extends AA {

void Show(){ System.out.println(“我喜欢C++!”); }

则顺序执行如下语句后输出结果为:( ) AA a; BB b; a.Show(); b.Show();

A、我喜欢Java! B、我喜欢C++! 我喜欢C++! 我喜欢Java! C、我喜欢Java! D、我喜欢C++! 我喜欢Java! 我喜欢C++! 答:A

11. 设有下面的一个类定义: class AA {

static void Show( ){ System.out.println(“我喜欢Java!”); } }

class BB { void Show( ){ System.out.println(“我喜欢C++!”); } }

若已经使用AA类创建对象a和BB类创建对象b,则下面哪一个方法调用是正确的:( )

A、a.Show( ) B、AA.Show( ); b.Show( ) BB.Show( ); C、AA.Show( ); D、a.Show( ); b.Show( ); BB.Show( ); 答:C

12.在Java中,一个类可同时定义许多同名的方法,这些方法的形式参数的个数、类型或顺序各不相同,传回的值也可以不相同。这种面向对象程序特性称为( )。

A、隐藏 B、覆盖 C、重载 D、Java不支持此特性 答:C

13.在Java中,存放字符串常量的对象属于( )类对象。 A、Character B、String C、StringBuffer D、Vector

20

答:B 14. 在编写异常处理的Java程序中,每个catch语句块都应该与( )语句块对应,使得用该语句块来启动Java的异常处理机制。

A、if – else B、switch C、try D、throw 答:C

15.对于子类的构造函数说明,下列叙述中不正确的是( )。 A、子类无条件地继承父类的无参构造函数。

B、子类可以在自己的构造函数中使用super关键字来调用父类的含参数构造函数,但这个调用语句必须是子类构造函数的第一个可执行语句。

C、在创建子类的对象时,将先执行继承自父类的无参构造函数,然后再执行自己的构造函数。

D、子类不但可以继承父类的无参构造函数,也可以继承父类的有参构造函数。 答:D 二、填空题。

1. 设x = 2 ,则表达式 ( x + + ) * 3 的值是 。 答:6

2. 若x = 5,y = 10,则x > y和x <= y的逻辑值分别为 和 。答:false、true

3. 据程序的构成和运行环境的不同,Java源程序分为两大类: 程序和 程序。答:Java Application 、 Java Applet ( 或 Application、Applet )

4. 如果一个Java源程序文件中定义有4个类,则使用Sun公司的JDK编译器javac编译该源程序文件将产生 个文件名与类名相同而扩展名为 的字节码文件。 答: 4、class

5. 开发与运行Java程序需要经过的三个主要步骤为 、 和 。 答:编辑源程序、编译生成字节码、解释运行字节码

6. 一个Java Application源程序文件名为MyJavaApplication.java,如果使用Sun公司的Java开发工具JDK编译该源程序文件并使用其虚拟机运算这个程序的字节码文件,应该顺序执行如下两个命令:

、 。 答:javac MyJavaApplication.java 、java MyJavaApplication

7. 在Java的基本数据类型中,char型采用Unicode编码方案,每个Unicode码字符占用 字节内存空间,这样,无论是中文字符还是英文字符,每个都是占用 字节内存空间。 答:2、2

8. Java源程序是由类定义组成的,每个程序中可以定义若干个类,但是只有一个类是主类。在Java Application中,这个主类是指包含 方法的类;在Java Applet里,这个主类是一个系统类 的子类。答:main Applet 9. 创建一个名为 MyPackage 的包的语句是 , 该语句应该放在程序的位置为: 。 答:package MyPackage ; 应该在程序第一句。

10. 形式参数指的是方法被 时的参数行,实际参数是方法被 时所传递进去的变量或值。 答:定义、调用

21

11. 多态是指 ,在Java中有两种多态,一种是使用方法的 实现多态,另一种是使用方法的 实现多态。

答:一个程序中有多个同名的不同方法 覆盖 重载

12. 在Java程序中,通过类的定义只能实现 重继承,但通过接口的定义可以实现 重继承关系。答:单、多

13. 设有数组定义:int MyIntArray[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 };

则执行以下几个语句后的输出结果是 。 int s = 0 ;

for ( int i = 0 ; i < MyIntArray.length ; i + + ) s += MyIntArray[i] ; System.out.println( s ); 答:550

练习十四(io)

1 在读字符文件Employee.dat时,使用该文件作为参数的类是___D___。

A、BufferedReader B、DataInputStream C、DataOutputStream D、FileInputStream

解析:本题考查的是文件和文件I/O。

BufferedReader用来从字符输入流中读取并缓冲字符,以提高读取字符(或字符数组)的效率。其构造方法为:

public BufferedReader(Reader in)

public BufferedReader(Reader in, int sz)

DataInputStream用来以独立于机器的方式从潜在的输入流中读取Java基本数据类型。构造方法为:

DataInputStream(InputStream in)

FileInputStream用来从文件中读取字节。构造方法为: FileInputStream(String name) FileInputStream(File file)

2 要串行化某些类的对象,这些类就必须实现 A 。

A、Serializable接口 B、java.io.Externalizable接口 C、java.io.DataInput接口 D、DataOutput接口

3、下列InputStream类中哪个方法可以用于关闭流?b A)skip() B)close() C)mark() D)reset()

4. 编译和运行下面的应用程序,并在命令行界面输入 12345 ,则回车后屏幕输出的结果是 ( ). C

public class A {

public static void main(String args[]) throws IOException{ BufferedReader buf=new BufferedReader( new InputStreamReader(System.in));

22

String str=buf.readLine(); int x=Integer.parseInt(str); System.out.println(x/100); } } A) 45 B) 5 C ) 123 D) 12345

6、 属于Java输入输出流的、且处理的是char类型的类是( )。a A、Reader类 B、InputStream类 C、OutputStream类 D、File类

练习十五(线程)

1 一个Java Application运行后,在系统中是作为一个 B 。 A、线程 B、进程 C、进程或线程 D、不可预知 2、下列方法中可以用来创建一个新线程的是___C___。 A、实现java.lang.Runnable接口并重写start()方法 B、实现java.lang.Runnable接口并重写run()方法 C、继承java.lang.Thread类并重写run()方法 D、继承java.lang.Thread类并重写start()方法

3、 下列关于线程优先级的说法中,正确的是__C____。

A、线程的优先级是不能改变的 B、线程的优先级是在创建线程时设置的 C、在创建线程后的任何时候都可以设置 D、B和C

4、 下列程序的功能是创建了一个显示5个\的线程并启动运行,请将程序补充完整。

public class ThreadTest extends Thread{

public static void main(String args[]){ ThreadTest t=new __ThreadTest()____; t.start();}

public void run(){int i=0;

while(true){System.out.println(\ if (i++==4) break; } }

5、 下列叙述中,正确的是___C___。

A、Reader是一个读取字符文件的接口 B、Reader是一个读取数据文件的抽象类

C、Reader是一个读取字符文件的抽象类 D、Reader是一个读取字节文件的一般类

6、下列哪个关键字通常用来对对象进行加锁,该标记使得对对象的访问是排他的?c A)transient B)serialize C)synchronized D)static

7、下列有关线程的叙述哪个是正确的?b

A)一旦一个线程被创建,它就立即开始运行

B)调用start()方法可以使一个线程成为可运行的,但是它不一定立即开始运行 C)主线程不具有默认优先级

D)Java中线程的优先级从低到高以整数0~9表示

23

8、(12)在线程中普通优先级的线程,其优先级默认值为 (5) 。

解析: NORM_PRIORITY代表普通优先级默认值为5。MIN_PRIORITY代表最小优先级,通常为1。MAX_PRIORITY代表最高优先级,通常为10。

9. 程序中实现多线程的方法有两种:继承 _____ 类和实现 Runnable 接口。Thread

10. 在一个线程中Sleep(1000)方法,将使得该线程在多少时间后获得对CPU的控制(假设睡眠过程中不会有其他事件唤醒该线程)?(C) (A)正好1000毫秒 (B)1000毫秒不到 (C)=>1000毫秒 (D)不一定

10.下列关于Java线程的说法那些是正确的() a

A 每一个Java线程可以看成由代码、一个真实的CPU以及数据三部份组成。

B 创建线程的两种方法中,从Thread类中继承的创建方式可以防止出现多父类问题。 C Thread类属于java.util程序包。 D 以上说法无一正确

11.class MyThread extends Thread{

public void run(){

System.out.println(\ }

public void start(){

System.out.println(\ }}

class MyRunnable implements Runnable{ public void run(){

System.out.println(\ }

public void start(){

System.out.println(\ } }

public class MyTest {

public static void main(String args[]){ MyThread myThread = new MyThread(); MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable); myThread.start(); thread.start(); }}

A.打印MyThread: start() 后面跟 MyRunnable: run() B.打印MyThread: run() 后面跟MyRunnable: start() C.打印MyThread: start() 后面跟MyRunnable: start() D.打印MyThread: run() 后面跟MyRunnable: run() 答案A

12. public class MyThread implements Runnable {

24

String myString = \ public void run() {

this.myString = \ }

public static void main(String[] args) { MyThread t = new MyThread(); new Thread(t).start(); for (int i=0; i < 10; i++)

System.out.print(t.myString); }}

A. 打印yes yes yes yes yes yes B. 打印no no no no no no no no C. 打印yes no yes no ye no ye no D. 不确定 答案D

13.检查下列关于线程的陈述的正确性。d

陈述A:在多线程应用程序中,在任何一个特定的时间点上都有一个以上的线程是活动的。 陈述B:如果一个线程终止了,那么处理过程也随之终止。

a. A是正确的,B是错误的。 b. A是错误的,B是正确的。 c. A和B都是正确的。 d. A和B都是错误的

练习十六(网络)

1、Socket的工作流程是c

①打开连接到Socket的输入/输出 ②按某个协议对Socket进行读/写操作 ③创建Socket ④关闭Socket A)①③②④ B)②①③④ C)③①②④ D)①②③④

2、利用Java网络包java.io中套接口类Socket和服务者套接口类(ServerSocket)实现有连接通讯.(只写出两端的通信连接结构语句)

3、利用Java网络包java.io中套接口类Socket和服务者套接口类(ServerSocket)实现无连接的通讯。(只写出两端的通信连接结构语句)

4、使用socket编写一个服务器端程序,服务器端程序在端口8888监听,如果它接到客户端发来的\请求时会回应一个\,对客户端的其他请求不响应。

5、有下面一段Server段程序,目的是能够同时服务多个客户,客户的请求是一句话(一个 String)。如果这个请求的内容是字符串\的话,服务器仅将\字符串返回给用户。否则将用户的话追加到当前目录的文本文件Memo.txt中(路径为\),并向用户返回\。注意Server并发的处理多用户,Memo.txt被共享,要求不能出现数据不一致。Server的程序如下文件Server.java: public class Server{

public static void main(String args[]){

25

MemoController memoController = new MemoController(); try{

ServerSocket ss = new ServerSocket(1999); while (true){

Socket s = ss.accept(); try{

UserThread t = new UserThread(s, memoController); t.start();

}catch(Exception e){ e.printStackTrace(); } }

}catch(Exception e){ e.printStackTrace(); }finally{

memoController.close(); }}}

类UserThread程序如下: 文件UserThread.java:

public class UserThread extends Thread{ Socket s;

MemoController memo;

public UserThread (Socket s, MemoController memo){ this.s = s;

this.memo = memo; }

public void run(){ try{ BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream())); String req = br.readLine(); if (req.equals(\ pw.println(\ }else{

memo.append(req); pw.println(\ }

pw.flush(); pw.close(); br.close(); s.close();

}catch(Exception e){

26

e.printStackTrace(); }}}

请根据题目的要求和现有的Server.java, UserThread.java的程序完成类MemoController.java的程序

练习十七(gui)

1、Panel默认的布局管理器是 A、FlowLayout 。

2 要从文件\文件中读出第10个字节到变量C中,下列__A____方法适合。 A、FileInputStream in=new FileInputStream(\in.skip(9); int c=in.read(); B、FileInputStream in=new FileInputStream(\in.skip(10); int c=in.read();

C、FileInputStream in=new FileInputStream(\int c=in.read(); D、RandomAccessFile in=new RandomAccessFile(\in.skip(9); int c=in.readByte();

3、下列关于Frame类的说法不正确的是c

A)Frame是Window类的直接子类 B)Frame对象显示的效果是一个窗口

C)Frame被默认初始化为可见 D)Frame的默认布局管理器为BorderLayout 4、在Java中,开发图形用户界面的程序需要使用一个系统提供的类库,这个类库就是( )包。

A、java.io B、java.awt

C、java.applet D、java.awt.event 答:B

5. 创建一个标识有“软件学院”字样的标签对象gxdd的语句为( );创建一个标识有“关闭”字样的按钮对象gb的语句为 ( )。 答:Label gxdd = new Label(“软件学院”); Button gb = new Button(“关闭”); 6.如果有一个类MyFrame是Frame的子类,能够被不同包中的类所使用,同时又能够为线程提供运行代码(run()方法),请写出该类的声明头:public class MyFrame extends Frame implements Runnable

7.容器被重新设置大小后,哪种布局管理器的容器中的组件大小不随容器大小的变化而改变? b

A CardLayout B FlowLayout C BorderLayout D GridLayout 8. public class Outer {

public void someOuterMethod() { // Line 3 }

public class Inner { }

public static void main(String[] argv) { Outer o = new Outer(); // Line 8

27

} }

A. new Inner(); // At line 3 B. new Inner(); // At line 8 C. new o.Inner(); // At line 8

D. new Outer.Inner(); // At line 8//new Outer().new Inner() 答案:A

解释:在外部类的之内定义内部类:

OuterClass.InnerClass innerObject=outerObject.new InnerClass(Constructor Parameters);

因此在第三行可以写成:new Inner();或者this.new Inner(); 也可以在第八行写成:Inner i=o.new Inner();或者Outer.Inner i=o.new Inner();

9. 你在为一个游戏应用程序创建主面板,其中包括三个导航按钮对面板上的“Game Help”、 “Play Game” 和 “Current Score vs Time Remaining”进行操纵。你希望主面板显示在

Java程序的左侧。而将要显示在程序右侧的三个面板中的一个取决于当前在主面板上被激 活的按钮。为此程序你将使用下列哪一个layout? b

a. BorderLayout b. GridLayout c. BoxLayout d. FlowLayout 10. 下列哪一种方法用来获取产生一个事件的组件?b

a. actionPerformed() b. getSource() c. super() d. getContentPane()

11. 当Frame的大小被改变时, Frame中的按钮的位置也可能被改变,请问此时使用的是什么布局管理器 B

A. BorderLayout B. FlowLayout C. CardLayout D. GridLayout 12. 容器是用来组织其他界面成分和元素的单元,它不能嵌套其他容器。( 错 ) 13. 容器被重新设置大小后,哪种布局管理器的容器中的组件大小不随容器大小的变化而改变?( B )

A.CardLayout B.FlowLayout C.BorderLayout D.GridLayout

练习十八(杂)

1、 Java语言的各种数据类型之间提供两种转换:强制转换和 自动转换 。 2、 假设有String a=\b=’A’;int c=65,下面选项中__B____是正确的。 A、if(a==b) {System.out.print(\B、if(c==b) {System.out.print(\C、if(a==c) {System.out.print(\D、if(c=b) {System.out.print(\3、 在运行时,由Java解释器自动导入,而不用import语句引入的包是__java.lang____。 4、在Java源文件中什么类最多只能有一个,其他类的个数不限?a A)public B)private C)protected D)没有这种类 5、下列关于变量的叙述哪个是错的?b

A)实例变量是类的成员变量 B)实例变量用关键字static声明 C)在方法中定义的局部变量在该方法被执行时创建 D)局部变量在使用前必须被初始化 6、执行完语句int [ ]x = new int[25];后,则下列哪项说法是正确的?a

28

A)x[24]值为0 B)x[24]值未定义 C)x[25]值为0 D)x[0]值为空 7、在Java语言的下列哪个包是编译器自动导入的?a

A)java.lang B)java.awt C)java.io D)java.applet

8、在Java程序中,通过类的定义只能实现单重继承,但通过 () 的定义可以实现多重继承关系。

解析: Java不支持多重继承,但类可以实现多个接口 9、顺序执行下列两个语句的输出结果是 (9) 。

String s = \全国计算机等级考试\(s.length()); 解析: 提取字符串的长度可以用length()方法,其返回字符串的字符个数。 10、关键字 ______ 用于导入包中的类到程序中,供程序中使用。Import 11. 下面关于垃圾收集的说法正确的是:(D)

(A)一旦一个对象成为垃圾,就立刻被收集掉。(不会随时有动作的) (B)对象空间被收集掉之后,会执行该对象的finalize方法(之前)

(C)finalize方法和C++的析构函数是完全一回事情(析构可以主动,Finalize是被动的)

(D)一个对象成为垃圾是因为不再有引用指着它,但是线程并非如此。

12. 假设类A有构造方法A(int a),则在类A的其他构造方法中调用该构造方法的语句格式应为 (C)

A. A(x) B. this. A(x) C. this(x) D. super(x)

13、如果一个Java源程序文件中定义有4个类,则使用Sun公司的JDK编译器javac编译该源程序文件将产生 个文件名与类名相同而扩展名为 的字节码文件。 答:4、class

14.设有下面两个类的定义:

class Person { class Student extends Person { long id; // 身份证号 int score; // 入学总分 String name; // 姓名 int getScore(){ } return score; } } 问:类Person和类Student的关系是( )。

A、包含关系 B、继承关系

C、关联关系 D、无关系,上述类定义有语法错误 答:B、

29

2009-2010第二学期《java程序设计》复习题 可能题型:程序填空、读程写结果、编程

1、 public class If3{

public static void main(String[] args){ int average=75; String judgement; if(average>=90){

judgement=”A’;} else if(average>=80){ judgement=”B’;} else if(average>=70){ judgement=”C’; }

else if(average>=60){ judgement=”D’; } else{

judgement=”E’; }

System.out.println(“平均=”+average); System.out.println(“评价=”+judgement); }} 2、

public class Switch3{

public static void main(String[] args){ int a=1; switch(a){

case 1: System.out.println(“111”); case 2: System.out.println(“222”); break;

case 3: System.out.println(“333”); default: System.out.println(“未知”); } } } 3、

public class For8_1{

public static void main(String[] args){ for(int a=1;true;a++){

30

if(a==6) break;

System.out.print((a+” “); } }} 4、

public class For8_2{

public static void main(String[] args){ for(int a=1;a<=10;a++){ //x1 if(a<=5) continue;

System.out.print((a+” “); } //x4 }} 5、

public class For11{

public static void main(String[] args){ for(int a=1;a<=3;a++){ for(int b=1;b<=2;b++){ if(a<=2)continue;

System.out.println(a+” ”+); }

System.out.println(“你好”+a) } }} 6、

public class Fo15{

public static void main(String[] args){ for(int a=1;a<=5;a++){ for(int b=1;b<=a;b++)

System.out.print(’*’); System.out.println(); } } } 7、

public class A2{

public static void main(String[] args) { int n=29; int i;

for(i=2;i<=n-1;i++) if(n%i==0) break; if(i>n-1)

System.out.println(n+\是素数\ else

System.out.println(n+\是合数\ }}

8、 public class A5{

public static void main(String[] args){ int k=1;

for(int i=1;i<5;i++){ for(int j=1;j<=i;j++){ System.out.print(k); k++; }

System.out.println(); } }}

9、 public class Array6{

public static void main(String[] args){ int[][] a; a=new int[3] [];

31

int[] sum=new int[3];

a[0]=new int[]{100,80,80,75,90}; a[1]=new int[]{50,60,60,65,55};

a[2]=new int[]{100,100,100,100,100}; for(int i=0;i

for(int i=0;i

System.out.println(”合计=”+sum[i]); System.out.println(); }}

10、class Man{ int height; int age; }

public class Class1{

public static void main(String[] args){ Man man1;

man1 =new Man(); man1.height=180; man1.age=20;

System.out.println(man1.height); System.out.println(man1.age); }}

11、 Class Man{ int heitht; int age; }

public class Class2{

public static void main(String[] args){ Man man1;

man1 =new Man(); Man man2; man2=man1;

man1.height=180; man1.age=20;

System.out.println(man2.height); System.out.println(man2.age); }}

12、 public class Class3{ double f(int x){ return 2.5+x; }

public static void main(String[] args){ Class3 aaa=new Class3(); //x3 double y=aaa.f(3); System.out.println(y);

System.out.println(aaa.f(2)); }}

13、 public class Class4{ Void printInt(int x){

System.out.println(“整数=”+x); }

public static void main(String[] args){ Class4 aaa=new Class4();

System.out.println(“你好~~”); }}

32

14、public class Class5{ void printInt(int x){ if(x<=0) return;

System.out.println(“整数=”+x); }

public static void main(String[] args){ Class5 aaa=new Class5(); aaa.printInt(10); aaa.printInt(-10); }}

15、public class Class6{ Void hello(){

System.out.println(“您好!”); }

public static void main(String[] args){ Class6 aaa=new Class6(); aaa.hello(); aaa.hello(); }}

16、 class Class10{ int a;

void setA(int a){ this.a=a; }

int getA(){ return this.a; }

public static void main(String[] args){ class10 ob=new Class10(); ob.setA(1000);

System.out.println(ob.a);

System.out.println(ob.getA()); }}

17、public class Class11{ void hi(){

System.out.println(“Hi~~~~”;) hello(); //x1 }

void hell(){

System.out.println(“Hello~~”); }

public static roid main(String[] args){ Class11 ob=new Class11(); Ob.hi(); }}

18、 public class Class12{ static int a; int b;

public static void main (String[] args){ Class12.a=10; //x1 Class12 ob1=new Class12(); Class12 ob2=new Class12(); System.out.println(ob1.a); ob1.a=20;

System.out.println(ob2.a); ob2.a=30;

System.out.println(Class12.a); }}

33

19、public class Class15{ static int a;

static void hello(){

System.out.println(“你好~~”); }

static void setA(int b){ a=b; hello(); }

Void printA(){

System.out.println(a); hello(); }

public static void main(String[] args){ Class15.setA(100);

Class15 ob=new Class15(); Ob.printA(); }}

20、class Class20_A{ private int mum;

void setNum(int a){ //x1,接口 num=a; } //仅在类内部使用 int getNum(){ //x2,接口 return num; } }

public class Class20{

public static void main(Steing[] args){ Class20_A ob=new Class20_A();

ob.setNum(10); //改变num值,但num对外不可见 System.out.println(ob.getNum()); }}

21、class Class21_A{ public int mum;

public void hi(){

system.out.println(“你好~~”); } }

public class Class21{

public static void main(Steing[] args){ Class21_A ob=new Class21_A(); ob.num=10;

ob.hi();

System.out.println(ob.num); }}

22、public class Class22{ void hi(){

System.out.println(“你好~~”);

}

void hi(String name){

System.out.println(name+“先生,你好~~”);

}

void hi(String name1,String name2){

System.out.println(name1+“先生,”+name2+”先生 你好~~”);

}

public class Class22{

public static void main(Steing[] args){ Class22 ob=new Class22();

34

ob.hi();

ob.hi(“张三”);

ob.hi(“李四”,”王五”); }}

23、public class Class26{ int a;

public Class26(){ a=10; }

public static void main(String[] args){ Class26 ob=new Class26(); System.out.println(ob.a); }}

24、public class Class27{ int a;

public Class27(){ //x1 a=10; }

public Class27(int a){ //x2 this.a=a; }

public static void main(String[] args){ Class27 ob1=new Class27(); //x3 Class27 ob2=new Class27(20); //x4 Class27 ob3=new Class27(30); //x5 System.out.println(ob1.a); System.out.println(ob2.a); System.out.println(ob3.a); }}

25、public class Class30{ public Class30(){

System.out.println(“你好~~”); }

public Class30(String s){ this();

System.out.println(“s”); }

public Class30(int a){ this(“高兴~~”);

System.out.println(“a”); }

public static void main(String[] args){ Class30 ob=new Class30(20); }}

26、public class Object1{ int a,b;

Object1(int a, int b){ this.a=a; this.b=b;

System.out.println(“对象创建”); }

Public static void main(String[] args){ Object1 ob1,ob2; //x1 ob1=new Object1(10,20); //x2 ob2=new Object1(30,40); //x3 ob1=ob2; //x4 } //x5 }

35

27、 public class Object6{

static void swap(int a,int b){ //x1 int temp=a; //x2 a=b; //x3 b=temp; //x4 }

public static void main(String[] args){ int x=10,y=20;

swap(x,y); //x5 System.out.println(x+” “+y); } } 28、public class Object8{ private int a;

public Object8(int a){ this.a=a; }

public void twice(Object8 o) //x1 o.a*=2; //x2 }

public int getA(){ return a; }

public atatic void main(String[] args){ Object8 ob=new Object8(100); //x3

ob.twice(ob); //x4 System.out.println(ob.getA()); //x5 }}

29、class Inher1_1{ int a; void hi(){

System.out.println(“Hi~~”);

} }

public class Inher1 extends Inher1_1{ public static void main(String[] args){ Inher1 ob=new Inher1(); ob.a=10; ob.hi(); }}

30、class Inher2_1{ int a;

Inher2_1(){ a=10;

System.out.println(“调用父类构造函数”);

} }

public class Inher2 extends Inher2_1{ int b; Inher2(){ //super(); b=20;

System.out.println(“调用子类构造函数”); public static void main(String[] args){ Inher2 ob=new Inher2(); //x3

System.out.println(ob.a+” “+ob.b); //x4 }}

31、class Inher3_1{ int a;

36

Inher3_1(){ //x1 a=1; }

Inher3_1(int a){ //x2 this.a=a; } }

public class Inher3 extends Inher3_1{ int b;

Inher3(int a, int b){ //x3 super(a); //x4 this.b=b; //x5 }

public static void main(String[] args){ Inher3 ob=new Inher3(10,20); //x6 System.out.println(ob.a+” “+ob.b); //x7 }}

32、class Inher6_1{ void hello(){

System.out.println(“Hi~~”); } }

public class Inher6 extends Inher6_1{ void hello(){ //覆盖

super.hello(); //调用继承而来的hello()方法 System.out.println(“高兴”); //功能的扩展 }

public static void main(String[] args){ Inher6 ob=new Inher6(); ob.hello(); }}

33、 class Super{ int a=10; void hi(){

System.out.println(\你好~~\ } }

class Sub extends Super{ int b=20; void hi(){

System.out.println(\ }

void bye(){

System.out.println(\ }}

public class Inher9{

public static void main(String[] args){ Super ob=new Sub(); ob.a=20; ob.hi();

//ob.b=30;错误 //ob.bye();错误 }}

34、public class Excep2{

public static void main(String[] args){ System.out.println(\开始程序\ int a=1,b=0; try{

37

int c=a/b; //x1 System.out.println(c); //x2 }catch(ArithmeticException ae){ //x3

System.out.println(\不能被0除!\ }

System.out.println(\退出程序\ }}

35、 public class Excep7{

static void go() throws NegativeArraySizeException{ int[] a=new int[-1]; //x1 }

static void hi() throws NegativeArraySizeException{ go(); //x2 }

public static void main(String[] args){ try{ hi();

}catch(NegativeArraySizeException e){

System.out.println(\捕获来自远方的异常.\ } } }

36、import java.io.IOException; public class Excep10{

public static void main(String[] args){ int a=0;

System.out.print(\请敲入字符>> \ try{

a=System.in.read(); }catch(IOException e){

System.out.println(\错误~~~~\ }

System.out.println(\敲入的字符为: \ }}

37、阅读程序,回答问题 1、

1: public class Output1 {

2: public static void main(String arge[]) { 3: int i=0;

4: for ( char ch = 97; ch<113; ch++,i++) { 5: if( i % 8 == 0 )

6: System.out.println(\7: System.out.print(\8: } 9: } 10: }

(1) 程序第 5 、 6 行的 if 语句的功能是什么? (2) 程序输出的结果有几行?

【 答案 】 (1) 每打印 8 个字符,则换行。 (2) 输出的结果有 2 行。 38、写出下面程序的运行结果。 1、import java.io.*; public class abc

{ public static void main(String args[ ]) { AB s = new AB(\ System.out.println(s.toString( )); } }

class AB { String s1; String s2;

38

AB( String str1 , String str2 ) { s1 = str1; s2 = str2; } public String toString( ) { return s1+s2;} }

答:Hello! I love JAVA. 2、import java.io.* ; public class abc {

public static void main(String args[ ]) { int i , s = 0 ;

int a[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 }; for ( i = 0 ; i < a.length ; i ++ ) if ( a[i]%3 = = 0 ) s += a[i] ; System.out.println(\ } }

答:s = 180

3、 import java.io.* ; public class abc {

public static void main(String args[ ])

{ SubSubClass x = new SubSubClass(10 , 20 , 30); x.show(); } }

class SuperClass { int a,b;

SuperClass(int aa , int bb) { a=aa; b=bb; } void show( )

{ System.out.println(\ }

class SubClass extends SuperClass { int c;

SubClass(int aa,int bb,int cc) { super(aa,bb); c=cc; } }

class SubSubClass extends SubClass { int a;

SubSubClass(int aa,int bb,int cc) { super(aa,bb,cc); a=aa+bb+cc; }

void show()

{ System.out.println(\ }

答:a=60 b=20 c=30

4、 import java.io.*; public class abc {

public static void main(String args[]) {

String s1 = \

39

String s2 = new String(\ System.out.println(s1.concat(s2)); } }

答:Hello! World! 5、 import java.io.* ; public class ABC {

public static void main(String args[ ]) { int i ;

int a[ ] = { 11,22,33,44,55,66,77,88,99 }; for ( i = 0 ; i <= a.length / 2 ; i ++ )

System.out.print( a[i]+a[a.length-i-1]+\ System.out.println( ); } }

答:110 110 110 110 110 6、 import java.io.*; class Parent {

void printMe() {

System.out.println(\ }}

class Child extends Parent {

void printMe() {

System.out.println(\ }

void printAll() {

super.printMe(); this.printMe(); printMe(); }}

public class Class1 {

public static void main(String args[ ]) {

Child myC = new Child( ); myC.printAll( ); }}

答:parent child child

39、编写一个字符界面的Java Application 程序,接受用户从键盘输入的一个正整数,然后统计并输出从1到这个正整数的累加和。

答:参考程序如下: import java.io.*; public class SUM {

public static void main (String[] args) {

int i , n=-1 , sum=0 ; do{ try {

BufferedReader br =

new BufferedReader( new InputStreamReader(System.in)); n = Integer.parseInt(br.readLine( ));

40

} catch ( IOException e ) { } ; } while ( n<1);

for ( i =1 ; i<=n; i++ ) sum += i; System.out.println(sum); } }

40、 编写一个字符界面的Java Application 程序,接受用户输入的10个整数,并输出这10个整数的最大值和最小值。

答:参考程序如下: import java.io.* ; public class abc {

public static void main(String args[ ])

{ int i , n = 10 , max = 0 , min = 0 , temp = 0; try {

BufferedReader br = new BufferedReader(

new InputStreamReader(System.in)); max = min = Integer.parseInt(br.readLine( )); } catch ( IOException e ) { } ; for ( i = 2 ; i <= n ; i ++ ) { try {

BufferedReader br = new BufferedReader(

new InputStreamReader(System.in)); temp = Integer.parseInt(br.readLine( )); if (temp > max ) max=temp; if (temp < min) min=temp;

} catch ( IOException e ) { } ; }

System.out.println(\ }}

41、使用socket编写一个服务器端程序,服务器端程序在端口8888监听,如果它接到客户端发来的\请求时会回应一个\,对客户端的其他请求不响应。

import java.io.*; import java.net.*;

public class HelloServer {

public static void main(String args[]) throws IOException {

ServerSocket server=null;

server = new ServerSocket(8888); Socket ClientSocket = null; ClientSocket = server.accept(); String line;

BufferedReader is = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));

PrintWriter os = new PrintWriter(ClientSocket.getOutputStream()); while (true) {

line = is.readLine();

if (line.equals(\ os.println(\ os.flush(); } }} }

42、编写一个应用程序,创建三个线程分别显示各自的时间。 import java.util.*; import java.text.*;

class ThreeTimeThread extends Thread {

public ThreeTimeThread(String str)

41

{

super(str); }

public void run() {

while (true) {

SimpleDateFormat formatter = new SimpleDateFormat (\hh:mm:ss z\

Date currentTime = new Date(); try {

sleep(1000);

}catch (Exception e) {}

String dateString = formatter.format(currentTime); System.out.println(getName()+\ } }

public static void main(String args[]) throws Exception {

new ThreeTimeThread(\ new ThreeTimeThread(\ new ThreeTimeThread(\ }}

43、在空白处填上合适的语句。

public class ServerTimedOutException extends Exception { private int port;

public ServerTimedOutException(String message, int port) { super(message); this.port = port; }

public int getPort() { return port; } }

class Client{

//在下行空白出填上声明抛弃ServerTimedOutException例外的语句

public void connectMe(String serverName) 错误!未找到引用源。 {

int success;

int portToConnect = 80;

success = open(serverName, portToConnect); if (success == -1) {

/*在下行空白处填上抛出ServerTimedOutException例外的语句*/ 错误!未找到引用源。 } } }

程序填空题答案:throws ServerTimedOutException;

throw new ServerTimedOutException(serverName,portName);

44、阅读以下程序,写出输出结果。 class First{ public First(){

aMethod(); } public void aMethod(){

System.out.println(“in First class”);} }

public class Second extends First{ public Second(){//super(); aMethod(); } public void aMethod(){

System.out.println(“in Second class”);} public static void main(String[ ] args){

42

new Second( ); } }

45、写出以下程序的运行结果。 class StringTest1 {

public static void main(String[] args) {

String s1=\ String s2= \ if(s1==s2){

System.out.println(\ }else{

System.out.println(\}}

46、写出下列程序完成的功能 public class Sum

{ public static void main( String args[ ]) { double sum = 0.0 ;

for ( int i = 1 ; i <= 100 ; i + + ) sum += 1.0/(double) i ;

System.out.println( \ }}

47、写出下列程序完成的功能 import java.io.*; public class Class1 {

public static void main(String args[ ]){ int i , Max , Min ;

int a[ ] = { 12,67,8,98,23,56,124,55,99,100 }; Max = Min = a[0];

for ( i = 1 ; i < a.length; i ++ ) { if ( a[i]Max ) Max = a[i]; }

System.out.println( Max+\ System.out.println( ); }}

48、①将100以内凡是7的倍数的数,全部输出。 ②在①题的基础之上,输出要求:5个一行的输出。 ③在②题的基础之上,最后输出共有多少这样的数。 49、输出如下图形 * *** ***** *** *

50.有三个字符串,编写程序找出其中最大者。

51.编写一个程序,设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出。

52.设定5个字符串,要求只打印那些以字母“b”开头的串,编写程序完成。 53.设定一个有大小写字母的字符串和一个查找字符,使用类 String方法IndexOf()来判断在该字符串中该字符出现的次数。

54. class MyParent { int x, y;

MyParent(int x, int y){ this.x = x; this.y = y; }

43

public int addMe(int x, int y){ return this.x + x + y + this.y; }

public int addMe(MyParent myPar){ return addMe(myPar.x, myPar.y); }}

class MyChild extends MyParent{ int z;

MyChild (int x, int y, int z){ super(x,y); this.z = z; }

public int addMe(int x, int y, int z){

return this.x + x + this.y + y + this.z + z; }

public int addMe(MyChild myChi){

return addMe(myChi.x, myChi.y, myChi.z); }

public int addMe(int x, int y){ return this.x + x + this.y + y; }}

public class MySomeOne{

public static void main(String args[]){

MyChild myChi = new MyChild(10, 20, 30); MyParent myPar = new MyParent(10, 20); int x = myChi.addMe(10, 20, 30); int y = myChi.addMe(myChi); int z = myPar.addMe(myPar); System.out.println(x + y + z); }} A. 300 B. 240 C. 120 D. 180

正确答案:A

55. public class Test {

public static void changeStr(String str){ str=\ }

public static void main(String[] args) { String str=\ changeStr(str);

System.out.println(str); }}答案:1234

56. 该程序的输入结果: public class Hidden {

public static void main(String[] args) { Hidden h = new Hidden(); try {

h.method(); } catch (Exception e) {

System.out.println(e.getMessage()); } }

public void method() throws Exception { try {

throw new Exception(\ } catch (Exception e) {

44

throw new Exception(\ }finally {

throw new Exception(\ } } }

答案:Third Exception

57.下列代码是否可以编译成功?由此得出的结论是什么? class Animal {

public void eat() throws IOException {

System.out.println(\ throw new IOException(); } }

class Horse extends Animal {

public void eat() throws Exception {

System.out.println(\ throw new Exception(); } } 答案:不能

结论:重写方法 子类不能抛出比父类更为广泛的异常,及异常范围不能扩大

58.下列代码输出结果为?由此得出的结论是什么? class Animal {

public static void eat(){

System.out.println (\ } }

class Horse extends Animal{ public static void eat(){

System.out.println (\ } }

public class Hidden {

public static void main(String[] args) { Animal h = new Horse(); h.eat(); } }

答案:Animal is eating.

结论:静态方法和所属的类进行绑定,实例方法和所属的对象进行绑定

59. 下列代码的输出结果是什么?原因是什么?

StringBuffer sb1 = new StringBuffer(\ StringBuffer sb2 = new StringBuffer(\ System.out.println(sb1.equals(sb2));

String s1 = new String(\ String s2 = new String(\

System.out.println(s1.equals(s2));

答案:false true

StringBuffer类中没有重新定义equals这个方法,而String类中重新定义了equals这个方法。

60.列程序将如何输出?说明理由

public static void main(String[] args) {

45

new Hidden(); }

public Hidden() {

Hidden alias1 = this; Hidden alias2 = this; synchronized (alias1) { try {

alias2.wait();

System.out.println(\ } catch (Exception e) {

System.out.println(\ }finally {

System.out.println(\ } }

System.out.println(\ }

答案:不打印任何代码 理由:在Java中,每一个对象都有锁。任何时候,该锁都至多由一个线程控制。由于alias1与alias2指向同一对象Hidden,在执行第11行前,线程拥有对象 Hidden的锁。在执行完第11行以后,该线程释放了对象Hidden的锁,进入等待池。但此后没有线程调用对象Hidden的notify()和notifyAll()方法,所以该 进程一直处于等待状态,没有输出。

61. 多线程

public class Atest7_2 implements Runnable { public static void main(String args[]) {

Thread t1 = (1) ; (2) ;

System.out.println(Thread.currentThread().getName()); }

public void run() {

System.out.print(Thread.currentThread().getName()+\} }

答案:(1)new Thread(new Atest7_2(),\注:“,\”可省略不写。

(2)t1.start()

62. 设计一个交通工具接口InterfaceVehicle,其中含有表示前进、倒车、向左转、向右转的抽象方法,由Car类实现InterfaceVehicle接口,Car类中新增字段有:车号、载重(吨)、颜色、运动状态,新增方法有:构造方法用以接收并初始化汽车的各字段信息;toString方法用以返回汽车的相关信息。创建一个驱动类TestCar,通过main方法实例化一个Car对象,然后输出该对象的信息。InterfaceVehicle接口、Car类的UML包图如图2所示,字段及其说明、实例数据见表2.

图 InterfaceVehicle接口和Car类的UML包图

表 InterfaceVehicle接口中方法和Car类中的字段及实例数据 InterfaceVehicle 交通工具 方法 方法说方法/实例数据 明 forward 前进 back 倒车 turnLeft 向左转 turnRight 向右转 Car 汽车 CID 车号 湘K97958 load 载重6

46

color state (吨) 颜色 运动状态 红色 1,2,3,4:分别表示前进、倒车、向左转、向右转 参考答案:

public class TestCar {

public static void main(String args[]) {

Car car = new Car(\湘K97958\红色\ System.out.println(car); car.back(); car.forward(); car.turnLeft(); car.turnRight(); } }

interface InterfaceVehicle {

public abstract void forward(); public abstract void back(); public abstract void turnLeft(); public abstract void turnRight(); }

class Car implements InterfaceVehicle { private String CID;//车号

private float load;//载重(吨) private String color;//颜色

private int state;// 运动状态:1,2,3,4:分别表示前进、倒车、向左转、向右转

public Car(){};

public Car(String CID,float load,String color,int state){ this.CID = CID; this.load = load; this.color = color; this.state = state; };

public String toString(){

return CID+\吨\ }

public void forward() { this.state = 1; System.out.println(showState());};

public void back() {this.state = 2; System.out.println(showState());}; public void turnLeft() {this.state = 3; System.out.println(showState());};

public void turnRight() {this.state = 4;System.out.println(showState());};

private String showState() { String s=null; switch (state) {

case 1: s = \前进\ case 2: s = \倒车\ case 3: s = \向左转\ case 4: s = \向右转\ }

return s; } }

63. 阅读下列程序,并给出下列程序运行的输出结果(8分)

47

class T{

void f(int x){ System.out.println(\

void f(double x){ System.out.println(\void f(Object x){ System.out.println(\}

class S extends T{

void f(int x){ System.out.println(\}

class TestOverLoadAndOverrid{

public static void main(String[] args){ T t = new S(); t.f(20); t.f(20L); t.f(3.5f); t.f(3.5);

t.f(\}

答案:int in S: 20 double in T: 20.0 double in T: 3.5 double in T: 3.5 Object in T: abcdef 48