}
其中,方法fun1() 将抛出IOException, 以下可以填入第1行的是( D )。 A.public IOException methodName() B.public void methodName()
C.public void methodName() throw IOException D.public void methodName() throws IOException 20.下列语句序列执行后,x的值是( B )。 public class ex3{
public static void main(String[ ]args){ int a=2;
int b=3; int x=4; x=++a*x;
System.out.println(x);
A、l3 B、12 C、11
二、改错题
判断下面的程序段是否正确。若有错,指出错在哪里并改正;若正确,打“√”。 1.class Test1
{ }
int x;
abstract int getX( );
答:错误,语句为abstract int getX( );该语句为抽象方法,只有在抽象类中才能定义,可以将Test1定义为抽象类:class abstract Test1
2.class A
{ private long pw; }
class Test2 extends A { long getPW() { return pw; } }
答:错误,语句为return pw;,原因是pw在类A中为私有成员,在类B中不可访问,改正方式为:将A类中的private long pw;改成public long pw;
3.public static void main(String args[ ])
{ char ch=?c?; try{ ch=System.in.read(); //省略其余代码
第9页
}
catch(Exception e) { return; } catch(IOException e) {
System.out.println(e.toString()); } }
答:错误,第一处错误为语句ch=System.in.read();,原因是System.in.read();表示从键盘接收一个数值,改成ch=(char)System.in.read();第二处错误为catch(IOException e),原因是Exception已经包含了IOExcption,可将IOExcption放在前,Excption放在后。
4.public class Test4
{ }
int i;
void Test4(int i) {
this.i=i; } void printi( int i ) { System.out.println(i); }
答:正确
5./*ActionListener为接口,void actionPerformed(ActionEvent a)为接口方法*/
public class Test5 implements ActionListener
{ }
void actionPerformed(ActionEvent a) {
//省略
}
答:错误,出现在:void actionPerformed(ActionEvent a),应加上权限修饰符public,改正为:public void actionPerformed(ActionEvent a)
三、程序阅读题
1.写出以下程序的运行结果。
class A{ A(){ System.out.print(\ } }
class B extends A{ B(){ System.out.print(\ } }
public class Test_1 extends B{ Test_1(){ System.out.print(\ \ } public static void main(String[ ] args){ new Test_1( ); } }
答:A B Test_1
第10页
2.写出以下程序的功能。
import java.io.*; class Test_2{
public static void main(String[ ] args) throws IOException { int b; FileInputStream fileIn=new FileInputStream(\ while((b=fileIn.read())!=-1){ System.out.print((char)b); } } }
答:将文本文件a.txt中的字符逐个读出并输出到显示器上。
3.阅读以下程序,若输入: 1<回车> 2<回车> 3<回车> 4<回车> 5<回车>
写出以下程序的运行结果。
import java.io.* ; public class Test_3
{ public static void main(String args[ ]) { int a[ ] = new int[5];
for (int i = 0 ; i < a.length ; i++ )
try { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); a[i] = Integer.parseInt(br.readLine( )); } catch ( IOException e ) { } ; int s=0;
for (int i = 0 ; i < a.length ; i++ ) {s+=a[i];}
System.out.print(s); } } 答: 15
4.写出以下程序的运行结果。
public class Test_4 { static int x; int c; public Test_4( ){ x++; c++; } public static void main(String[ ] args){ Test_4 s1=new Test_4(); System.out.println(s1.x+\ s1=new Test_4(); System.out.println(s1.x+\ }
static { x=2; } }
答: 3,1 4,1
5.写出以下程序的运行结果。
public class Test_5 { public static void main(String[ ] args)
第11页
{ System.out.println( fun(30, 20, 10) ); }
static int fun(int x, int y, int z) { return fun( x, fun(y,z) ); } static int fun(int x,int y) { if(x>y) return x; else return y; } }
采用递归函数求最大值 答: 30
6.写出以下程序的运行结果。
public class Test_6 { public static void main(String args[]) { char a[][]=new char[3][3]; for(int i=0;i * ** *** 7.写出以下程序的运行结果。 class SuperTest{ public SuperTest(){ System.out.println(\ showInfo(); } public void showInfo(){ System.out.println(\} } public class Test_7 extends SuperTest{ public Test_7(){ System.out.print(\ } public void showInfo(){ System.out.println(\} public static void main(String[ ] args){ new Test_7( ); } } 答: SuperTest in Test_7 Test_7 8.写出以下程序的运行结果。 第12页

