JAVA期末试题及答案 下载本文

答案:(1) 10; (2) 20; (3) 10。 2. 按要求填空

abstract class SuperAbstract{ }

interface AsSuper { }

abstract class SubAbstract extends SuperAbstract implements AsSuper {

public void b(){…} }

public class InheritAbstract extends SubAbstract{ }

在以上这段程序中:

抽象类有:SuperAbstract和 (1) (写出类名) 非抽象类有: (2) (写出类名) 接口有: (3) (写出接口名)

AsSuper中的x()方法是(4)方法,所以在InheritAbstract中必须对它进行(5) 答案:

(1) SuperAbstract; (2) InheritAbstract; (3) AsSuper;

public void x(){…} public int c(int i ) {…} public String f(){…}

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

InheritAbstract instance=new InheritAbstract(); instance.x(); instance.a(); instance.b(); instance.c(100);

System.out.println(instance.f()); abstract String f(); void x(); void a(){…} abstract void b(); abstract int c(int i);

(4) 抽象; (5) 覆盖和实现。 3. 按注释完成程序 public class Leaf { }

输出结果为 i = (3) 答案: (1) this; (2) new Leaf(); (3) 3

4. 按注释提示完成文件复制的程序 //FileStream源代码如下: import java.io.*; class FileStream {

public static void main(String args []) { private int i = 0; //此属性值用于检验

Leaf increment(){ //定义方法increment(),返回值是Leaf类的对象 }

void print() { }

public static void main(String args[]){

Leaf x = (new Leaf();); //创建Leaf类的对象x x.increment().increment().increment().print(); System.out.println(\

i++;

return (this) ;//将当前对象的地址作为返回值返回

}//多次调用方法increment(),返回的都是x的地址,i 值表示调用次数

try {

File inFile = new File(\//指定源文件 File outFile = new File(\ FileInputStream fis =(1);

FileOutputStream fos = new FileOutputStream(outFile);

int c;

//指定目标文件

//逐字节从源文件中输入,再输出到fos流

while ((c = fis.read ())!=-1)

fos.write(c);

fis.close();

fos.close();

}

catch (Exception e) {

System.out.println(\

}

} } 答案:

(1) new FileInputStream(inFile); (2) fos.write(c); 5. 阅读程序,给出结果:

// AbstractClassDemo.java源代码如下:

abstract class Shape { //定义抽象类Shape和抽象方法display }

class Circle extends Shape { }

class Rectangle extends Shape {

void display() { //实现抽象类的方法 System.out.println(\

}

class Triangle extends Shape { }

public class AbstractClassDemo{ }

输出结果是 ?

答案:(1) Circle; (2) Rectangle; (3) Triangle。

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

(new Circle()).display(); //定义无名对象来调用对应的display方法 (new Rectangle()).display(); (new Triangle()).display(); void display() { //实现抽象类的方法 }

System.out.println(\}

void display() { }

//实现抽象类的方法

abstract void display();

System.out.println(\

一、选择题(20题,每题2分,共40分)

1.在面向对象的方法中,一个对象请求另一个对象为其服务的方式是通过发送 ( D ) A、调用语句 B、命令 C、口令 D、消息

2.Java语言具有许多优点和特点,下列选项中,哪个反映了Java程序并行机制的特点:( B ) A、安全性 B、多线程 C、跨平台 D、可移值

3.编写和运行Java applet程序与编写和运行Java application程序不同的步骤是:(B ) A、编写源代码

B、编写HTML文件调用该小程序,以.html为扩展名存入相同文件夹 C、编译过程 D、解释执行

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

5.关于下列程序段的输出结果,说法正确的是:( D 基本类型数值数据的默认初始值为0 ) public class MyClass{ static int i;

public static void main(String argv[]){ System.out.println(i); } }

A、有错误,变量i没有初始化。 B、null C、1 D、0

6.下列代码的执行结果是:( B ) public class Test3{

public static void main(String args[]){ System.out.print(100%3); System.out.print(\System.out.println(100%3.0); } }

A、1,1 B、1,1.0 C、1.0,1 D、1.0,1.0

7.下列程序段的输出结果是:( B ) void complicatedExpression(){ int x=20, y=30; boolean b;

b=x>50&&y>60||x>50&&y<-60||x<-50&&y>60||x<-50&&y<-60; System.out.println(b); } A、true B、false C、1 D、0

8.给出下列代码片段:( D ) if(x>0){System.out.println(\else if(x>-3){ System.out.println(\else {System.out.println(\请问x处于什么范围时将打印字符串“second”? A、x>0 B、x>-3 C、x<=-3 D、x<=0 &&x>-3

9.若要把变量声名为暂时性变量,应使用如下哪种修饰符?(C) A、protected B、provate

C、transient (临时变量) D、volatile (共享变量)

10.在Java中,一个类可同时定义许多同名的方法,这些方法的形式参数的个数、类型 或顺序各不相同,传回的值也可以不相同,这种面向对象程序特性称为:( C ) A、隐藏 B、覆盖 C、重载

D、Java不支持此特性

11.如要抛出异常,应用下列哪种子句?( B ) A、catch B、throws C、try D、finally