2009 /2010一. 单项选择题(共10题,每题3分)1.在Java中,一个类可同时定义许多同名的方法,这些方法的形式参数个数、类型或顺序各不相同,传回的值也可以不相同。
这种面向对象程序的特性称为( C )。
A、隐藏B、覆盖C、重载D、继承2.以下关于构造函数的描述错误的是( A )。
A、构造函数的返回类型只能是void型。
B、构造函数是类的一种特殊函数,它的方法名必须与类名相同。
C、构造函数的主要作用是完成对类的对象的初始化工作。
D、一般在创建新对象时,系统会自动调用构造函数。
3.设有下面两个类的定义:public class Person {String name; //姓名long id; //身份证号class Student extends Person {int score; // 入学总分int getScore(){return score;}}}}则类Person和类Student的关系是( B )。
A、包含关系B、继承关系C、关联关系D、上述类定义有语法错误4. 下列哪一种main()方法的声明是合法的? ( B )A. public static void main() { }B. public static void main(String[] args){ }C. void main(String[] args) { }D. public void static main(String []args){ }5.若类A的成员的访问控制符为默认(即未定义),关于该成员访问控制权限的正确描述是()。
A、只能被A的成员方法访问。
B、只能被与A在同一个包里的类的成员方法访问。
C、只能被A的子类的成员方法访问。
D、可以被所有类访问。
6.有以下方法的定义,请选择该方法的返回类型是什么?( D )。
ReturnType method(byte x, double y){return (short)x/y*2;}A、byteB、shortC、intD、double7.为了以字节方式从文件读出内容,可以使用哪个类?()A、FileReaderB、FileInputStreamC、FileOutputSteamD、FileWriter8. 设有类型定义short i=32;long j=64;下面赋值语句中哪一个是不正确的?()A.j=iB.i=jC.i=(short)jD.j=(long)i9. 在某个类A中存在一个方法:void GetSort(int x),以下哪一项能作为这个方法的重载的声明?()A.void GetSort(float x)B.int GetSort(int y)C.double GetSort(int x,int y)D.void Get(int x,int y)10. 为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用AB.method()就可以调用该方法,该方法的形式为下面哪一种?()A.public void method()B.static void method()C.final void method()D.abstract void method()二. 程序阅读题(共8题,每题5分)11.写出以下程序段的输出结果。
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="+s);}}回答:S=18012.写出以下程序段的输出结果。
public class Test{public static void main(String[] args) {char grade = 'B';switch (grade) {case 'B':System.out.print("Excellent");case 'C':System.out.print("OK");break;default:System.out.print("Let's talk");}}}回答:ExcellentOK13.根据重写(overriding)的概念,写出以下程序段的输出结果。
class Cruncher{void crunch( int i ){System.out.print(“int”);}void crunch(String s){System.out.print(“String”);}public static void main(String args[ ]){Cruncher crun=new Cruncher ( );char ch=’p’;int i=10;crun.crunch(ch);System.out.print(“,”);crun.crunch(i);}}回答:int,int14.根据父类子类间构造函数的调用顺序,写出以下程序的运行结果。
class C1 {C1() {System.out.print("1"); }}class C2 extends C1 {C2() {System.out.print("2"); }}public class C3 extends C2{C3() {System.out.println("3"); }public static void main(String[] args) {C3 c = new C3( );}}回答:12315.写出以下程序段的输出结果。
class exam31{public static void main(String[] args) {String s1=new String("hello");String s2=new String("hello");if(s1==s2){System.out.println("s1==s2");}else{System.out.println("s1!=s2");}}}回答:Exception in thread "main" ng.NoClassDefFoundError: exam31 16.写出以下程序段的输出结果。
class Complex extends Object {private double x,y;Complex(double u,double v){x=u;y=v;}double real(){return x;}double imag(){return y;}void plus(Complex w) {x+=w.real();y+=w.imag();}public String toString() {if (y>0)return x+" + "+y+"i";elsereturn x+" - "+(-y)+"i";}public static void main(String[] args) {Complex c1=new Complex(2,1.5);Complex c2=new Complex(-0.8,-3);c1.plus(c2);System.out.println(c1.toString());}}回答:1.2 - 1.5i17.写出以下程序段的输出结果:import java.io.*;public class Test{public static void main(String[] args){try{throw new IOException();}catch(IOException npex){System.out.println("IOException thrown");}catch(ArrayIndexOutOfBoundsException ex){System.out.println("ArrayIndexOutOfBoundsException thrown");}finally{System.out.println("Done with exceptions ");System.out.println("myMethod is done");}}}回答:IOException thrownDone with exceptionmyMethod is done18.下面HTML代码用于显示,写出程序的运行结果:<HTML><BODY><Applet code = AppletTest.class height=400 width=400><param name=age value="30" ></Applet></BODY></HTML>import java.applet.*;import java.awt.*;public class AppletTest extends Applet {int localnum,paranum;public void init(){localnum++;paranum=Integer.parseInt(getParameter("age"));}public void paint(Graphics g){paranum++;String a="localnum="+localnum;g.drawString(a,5,50);g.drawString("paranum="+paranum,5,80);}}回答:三. 程序设计题(30分)19.(5分)下列程序的功能是:从命令行接受用户输入的10个整数,并输出这10个整数中的最大值和最小值。
请在下面横线处填入正确的代码,使程序可以正确运行。
import java.io.* ;public class exam44{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( );} catch ( IOException e ) { } ;for ( ) {try {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));temp = Integer.parseInt( );if (temp > max ) ;if (temp < min) ;} catch ( IOException e ) { } ;}System.out.println("max="+max+"\n min="+min);}}20. (13分)下面程序中定义了一个名为Comparable的接口,只要某个类的元素是可以“对比”的,就可以自由地使用这个接口。