2013-2014-1-java考试复习题库 联系客服

发布时间 : 星期四 文章2013-2014-1-java考试复习题库更新完毕开始阅读

5.编写应用程序1!+2!+3!+?+10!。 6.编写程序求100内所有素数。 7.设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下:

? 编写抽象类Animal

Animal抽象类有两个抽象方法cry( )和getAnimalName( ),即要求各种具体动物给出自己的叫声和种类名称。

? 编写模拟器类Simulator

该类有一个playSound(Animal animal)方法,该方法的参数是Animal类型。即参数animal可以调用Animal的子类重写的cry( )方法播放具体动物的声音,调用子类重写的getAnimalName( )方法显示动物种类的名称。

? 编写Animal类的子类:Dog和Cat类 ? 编写主类Test1(用户程序)

在主类的Test1的main方法中至少包含如下代码。 Simulator simulator=new Simulator( ); simulator.playSound(new Dog( )); simulator.playSound(new Cat( ));

8.用类描述计算机中CPU的速度和硬盘的容量。要求Java应用程序有4个类,名字分别是PC、CPU、HardDisk和Test,其中Test是主类。

? PC类、CPU和HardDisk类的相关要求如下。

其中,CPU类要求getSpeed( )返回speed的值,要求setSpeed(int m)方法将参数m的值赋值给speed。HardDisk类要求getAmount( )返回amount的值,要求setAmount(int m)方法将参数m的值赋值给amount。PC类要求setCPU(CPU c)将参数c的值赋值给cpu,要求seHardDisk(HardDisk h)方法将参数h的值赋值给HD,要求show( )方法能显示cpu的速度和硬盘的容量。

? 主类Test的要求

(1)main方法中创建一个CPU对象cpu,cpu将自己的speed设置为2200;

(2)main方法中创建一个HardDisk对象disk,disk将自己的amount设置为200; (3)main方法中创建一个PC对象pc;

(4)pc调用setCPU(CPU c)方法,调用时实参是cpu;

(5)pc调用setHardDisk(HardDisk h)方法,调用时实参是disk; (6)pc调用show( )方法。

四、简答题

1.Java语言有哪些主要特点?

2.面向对象编程与面向过程编程有哪些不同之处? 3.实例成员和类成员有哪些区别?

4.什么是异常?Java语言的异常处理机制是怎样处理异常的? 5.画图说明线程状态及其转换 6.请说明Java里多态的体现

7.举例说明对象的上转型对象的使用 8.举例说明什么是接口回调

17

9.与类相关的匿名类和与接口相关的匿名类在使用上有什么区别? 10.什么叫方法的重载?构造方法可以重载吗?

11.this关键字代表什么?this可以出现在类方法中吗? 12.简述类变量和实例变量的区别。

13.子类重写方法的规则是怎样的?重写方法的目的是什么? 14.怎样使用输入、输出流克隆对象? 15.引起线程中断的常见原因是什么?

16.ServerSocket对象调用accept方法返回一个什么类型的对象?

五、阅读理解题(请在指定位置写出答案,否则无效。)1.请给出E类中标记的【结果1】、【结果2】。

class B { int n;

static int sum=0; void setN(int n) { this.n=n; }

int getSum()

{ for(int i=1;i<=n;i++) sum=sum+i; return sum; } }

public class E

{ public static void main(String args[]) { B b1=new B(),b2=new B(); b1.setN(3); b2.setN(5);

int s1=b1.getSum(); int s2=b2.getSum();

System.out.println(s1); //【结果1】 System.out.println(s2);//【结果2】 } }

2.请给出E类中标记的【结果1】、【结果2】。

class A

{ double f(double x,double y) { return x+y; } }

class B extends A { double f(int x,int y) { return x*y; } }

public class E

1.答: 【结果1】: 【结果2】: 2.答: 【结果1】: 【结果2】: 18

{ public static void main(String args[]) { B b=new B();

System.out.println(b.f(5,8)); //【结果1】 System.out.println(b.f(8.0,12.0));// 【结果2】 } }

3.请给出E类中标记的【结果1】、【结果2】。

class AAA

{ int add(int x,int y) { return x+y; } }

class Student2004 extends AAA { int add(int x,int y) { return x-y; } }

public class E

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

System.out.println(a.add(55,33)); //【结果1】 a=new Student2004();

System.out.println(a.add(55,33)); //【结果2】 } }

3.答: 【结果1】: 【结果2】: 4.请给出E类中标记的【结果1】、【结果2】。

import java.awt.*;

import java.awt.event.*;

public class E implements Runnable

{ StringBuffer buffer=new StringBuffer(); Thread t1,t2,t3; E()

{ t1=new Thread(this); t2=new Thread(this); t3=new Thread(this); }

public synchronized void addString(String c) { if(Thread.currentThread()==t1) { while(buffer.length()==0) try{ wait(); }

catch(Exception e){} buffer.append(c); }

if(Thread.currentThread()==t2) { while(buffer.length()<15) try{ wait(); }

catch(Exception e){} buffer.append(c); }

if(Thread.currentThread()==t3) { buffer.append(c);

4.答: 【结果1】: 【结果2】: 19

}

notifyAll(); }

public void run()

{if(Thread.currentThread()==t1)

{ addString(\今天是一月十五号,\ }

if(Thread.currentThread()==t2) { addString(\天气不错,\ }

if(Thread.currentThread()==t3)

{ addString(\我们考试的科目是Java,\ } }

public static void main(String s[]) { E hello=new E();

System.out.println(hello.t1.isAlive()+\ //【结果1】 hello.t2.start(); hello.t1.start(); hello.t3.start();

while(hello.t1.isAlive()||hello.t2.isAlive()||hello.t3.isAlive()) { }

System.out.println(hello.buffer); //【结果2】 } }

5.请说出E类中System.out.println的输出结果。

import java.io.*; public class E {

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

5.答: 【结果1】: 【结果2】: FileOutputStream out=new FileOutputStream(\ FileInputStream in=new FileInputStream(\ byte content[]=\ StringBuffer bufferOne=new StringBuffer(),

bufferTwo=new StringBuffer();

int m=-1;

byte tom[]=new byte[3]; out.write(content); out.close();

while((m=in.read(tom,0,3))!=-1) {

String s1=new String (tom,0,m);

bufferOne.append(s1);

String s2=new String (tom,0,3); bufferTwo.append(s2); }

20