求Java答案!

[复制链接]
查看11 | 回复2 | 2010-6-27 21:41:53 | 显示全部楼层 |阅读模式
1. 程序阅读与分析题:阅读以下程序,并把运行结果写出来。
(1)
class SuperClass{

int i;
}
class Exam1 extends SuperClass{

Exam1(int i){

this.i=i;

}

void go(){

System.out.println(super.i);

System.out.println(i);

}

public static void main(String args[]){

SubClass obj=new SubClass(10);

obj.go();

}
}
(2)
public class Exam2
{
public static void main(String args[])
{
int m=0,n=0,t=555;
try
{
m=8888;
n=5/0;
t=9999;
}
catch (ArithmeticException e)
{
System.out.println("发生异常");
n=1;
}
System.out.println("m="+m+" n="+n+" t="+t);
}
}
(3)
class A{
void print(){
System.out.println("This is a superclass");
}
}
class B extends A{
void print(){
System.out.println("This is a subclass");
super.print();
}
}
class Exam3{
public static void main(String args[]){
A a=new A();
a.print();
B b=new B();
b.print();
}
}
2. 请阅读以下程序,并在空白处填写适当内容,使其功能正确完整
输入输出流题
import_______________________;
public class FileCopy{

public static void main(String[] args) {

FileInputStreamin;

FileOutputStreamout;
try{

in = new
(args[0]);

out= new
(args[1]);

copyFile(in,out);

}

catch(IndexOutOfBoundException e){

System.out.println("没有指定文件名!");

return;

}

catch(FileNotFoundEception e){

System.out.println("File not found");

return;

}

}

copyFile(FileInputStream in , FileOutputStream out){

int len;

byte buf[]=new byte[1024];

try{

while((len=in.read(buf,0,1024))!=-1)

out.write(buf,0,len);

}

catch(IOExceptione){

System.out.println("IO error");

return;

}

}
}
四.判断题(每题1分,共10分)
1.如果一个Java程序中有多个类,编译后只生成一个字节码文件,其名字同主类名。( )
2.Java程序中出现的输出方法println()和print()是完全一致的。
( )
3.下述两个命令:javac和java是不同的,编译时用前者,运行时用后者。
( )
4.Applet程序中主类必须是Applet类的子类。
( )
5.通常在Applet程序被重新启动时,start()方法被系统自动调用来启动主线程的运行。( )
6.Java语言中,类的继承是可以传递的。
( )
7.Java语言中,方法调用一律都是传址的引用调用。
( )
8.抽象方法一定出现在抽象类中。
( )
9.class不是定义类的惟一关键字。
( )
10.一个类可以实现多个接口,即接口可以实现“多重继承”。
( )

回复

使用道具 举报

千问 | 2010-6-27 21:41:53 | 显示全部楼层
1.(1):程序的主要意思可能就是想让你区分一下父类和子类的属性的引用和构造方法的引用( SubClass obj=new SubClass(10);这句中,应该是SuperClass吧,不然我就不懂事什么了)。输出是:1010其中System.out.println(super.i);和System.out.println(i);分别是输出父类属性的值和子类属性的值。(2)输出结果为:发生异常m=8888 n=1 t=555分析:首先定义了m、n、t三个变量,然后给m赋值,m=8888,然后给n赋值,但是此过程中引发了一个异常(即除数不能为0的异常),所以直接跳到catch里执行catch里的内容,所以t是不变的,
回复

使用道具 举报

千问 | 2010-6-27 21:41:53 | 显示全部楼层
为楼上的补充一下:try{
in = new FileInputStream(args[0]);
out= new FileOutputStream(args[1]);
copyFile(in,out);
}另外应该有关闭in和out的地方,不知道为什么没
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行