您的位置:首页 > 编程语言 > Java开发

Java基础试卷及答案

2010-12-07 20:14 507 查看
[align=center][/align]
[align=center]Java程序设计》试卷一[/align]
班级____学号____ 姓名_____ 成绩____
一.选择题(每题1分,共11分)
1.欲构造ArrayList类的一个实例,此类继承了List接口,下列哪个方法是正确的 ?
A、 ArrayList myList=new Object();
B、 List myList=new ArrayList();
C、 ArrayList myList=new List();
D、 List myList=new List();
2.paint()方法使用哪种类型的参数?
A、 Graphics
B、 Graphics2D
C、 String
D、 Color
3.指出正确的表达式
A、 byte=128;
B、 Boolean=null;
C、 long l=0xfffL;
D、 double=0.9239d;
4.指出下列程序运行的结果

public class Example{

  String str=new String("good");

  char[]ch={'a','b','c'};

  public static void main(String args[]){

    Example ex=new Example();

    ex.change(ex.str,ex.ch);

    System.out.print(ex.str+" and ");

    Sytem.out.print(ex.ch);

  }

  public void change(String str,char ch[]){

    str="test ok";

    ch[0]='g';

  }

}
A、 good and abc
B、 good and gbc
C、test ok and abc
D、 test ok and gbc



5.运行下列程序, 会产生什么结果

public class X extends Thread implements Runable{

 public void run(){

  System.out.println("this is run()");

 }

 public static void main(String args[])

 {

  Thread t=new Thread(new X());

  t.start();

 }

}
A、 第一行会产生编译错误
B、 第六行会产生编译错误
C、 第六行会产生运行错误
D、 程序会运行和启动

6.要从文件" file.dat"文件中读出第10个字节到变量C中,下列哪个方法适合?
A、 FileInputStream in=new FileInputStream("file.dat"); in.skip(9); int c=in.read();
B、 FileInputStream in=new FileInputStream("file.dat"); in.skip(10); int c=in.read();
C、 FileInputStream in=new FileInputStream("file.dat"); int c=in.read();
D、 RandomAccessFile in=new RandomAccessFile("file.dat"); in.skip(9); int c=in.readByte();



7.容器被重新设置大小后,哪种布局管理器的容器中的组件大小不随容器大小的变化而改变?
A、 CardLayout
B、 FlowLayout
C、 BorderLayout
D、 GridLayout



8.给出下面代码:

public class Person{

  static int arr[] = new int[10];

  public static void main(String a[])

  {

   System.out.println(arr[1]);

  }

}

那个语句是正确的?
A、 编译时将产生错误;
B、 编译时正确,运行时将产生错误;
C 、输出零;
D、 输出空。



9.哪个关键字可以对对象加互斥锁?
A、 transient
B synchronized
C serialize
D static



10.下列哪些语句关于内存回收的说明是正确的?
A、 程序员必须创建一个线程来释放内存;
B、 内存回收程序负责释放无用内存
C、内存回收程序允许程序员直接释放内存
D、内存回收程序可以在指定的时间释放内存对象



11.下列代码哪几行会出错:

1) public void modify() {

2) int I, j, k;

3) I = 100;

4) while ( I > 0 ) {

5) j = I * 2;

6) System.out.println (" The value of j is " + j );

7) k = k + 1;

8) I--;

9) }

10} }
A、 line 4
B、 line 6
C、 line 7
D、 line 8


二、多项选择(每题3分,共39分)

1.执行下列代码后,哪个结论是正确的 String[] s=new String[10];
A、 s[10] 为 "";
B、 s[9] 为 null;
C、 s[0] 为未定义
D、 s.length 为10
2.下面的表达式哪个是正确的?
A、 String s="你好";int i=3; s+=i;
B、 String s="你好";int i=3; if(i==s){ s+=i};
C、 String s="你好";int i=3; s=i+s;
D、 String s="你好";int i=3; s=i+;
E、. String s=null; int i=(s!=null)&&(s.length>0)?s.length():0;


3.选出合理的标识符
A、 _sys1_lll
B、 2mail
C、 $change
D、 class
4.哪个布局管理器使用的是组件的最佳尺寸( preferred size)
A、 FlowLayout
B、 BorderLayout
C、 GridLayout
D、 CardLayout
E、GridBagLayout
5.下列哪个方法可用于创建一个可运行的类?
A、 public class X implements Runable{ public void run(){ ......} }
B、 public class X implements Thread{ public void run(){ ......} }
C、 public class X implements Thread{ public int run(){ ......} }
D、public class X implements Runable{ protected void run(){ ......} }
E、public class X implements Thread{ public void run(){ ......} }
6.下面哪个方法可以在任何时候被任何线程调用?
A、 notify()
B、 wait()
C、 notifyAll()
D、 sleep()

E、yield()

F、synchronized(this)



7.构造BufferedInputStream的合适参数是哪个?
A BufferedInputStream
B BufferedOutputStream
C FileInputStream
D FileOuterStream

E. File



8.下列说法正确的是
A java.lang.Clonable是类

B java.lang.Runnable是接口

C Double对象在java.lang包中
D Double a=1.0是正确的java语句



9.指出正确的表达式
A double a=1.0;
B Double a=new Double(1.0);
C byte a = 340;
D Byte a = 120;



10.定义一个类名为"MyClass.java"的类,并且该类可被一个工程中的所有类访问,那么该类的正确声明应为:
A private class MyClass extends Object
B class MyClass extends Object
C public class MyClass
D public class MyClass extends Object



11.指出下列哪个方法与方法public void add(int a){}为合理的重载方法。
A public int add(int a)
B public void add(long a)
C public void add(int a,int b)
D public void add(float a)



12.如果下列的方法能够正常运行,在控制台上将显示什么?

public void example(){

  try{

    unsafe();

    System.out.println("Test1");

    }

    catch(SafeException e)

    {System.out.println("Test 2");}

  finally{System.out.println("Test 3");}

  System.out.println("Test 4");

}
A Test 1
B Test 2
C Test 3
D Test 4



13.下列哪些情况可以终止当前线程的运行?
A 抛出一个例外时。
B 当该线程调用sleep()方法时。
C 当创建一个新线程时。
D 当一个优先级高的线程进入就绪状态时。

、 填空题(每题2分,共10分)

1.执行下列代码后的结果是什么? int x,a=2,b=3,c=4; x=++a+b+++c++;

2. 包包含了Collection的接口和类的API

3.main方法的声明格式包括

4.下列程序中构造了一个SET并且调用其方法add(),输出结果是

public class A{

public int hashCode(){return 1;}

public Boolean equals(Object b){return true}

public static void main(String args[]){ Set set=new HashSet();

set.add(new A());

set.add(new A());

set.add(new A());

System.out.println(set.size());

}

}

5.下列程序的运行结果是

class A{

class Dog{

  private String name;

  private int age;

  public int step;

  Dog(String s,int a)

  {

   name=s;

   age=a;

   step=0;

   }

  public void run(Dog fast)

  {

   fast.step++;

  }

}

  public static void main (String args[])

  {

  A a=new A();

  Dog d=a.new Dog("Tom",3);

  d.step=25;

  d.run(d);

  System.out.println(d.step);

  }

}

四、 编程题(每题10分,共40分)

1.编写一个输出"Hello World!"的程序,用两种方式实现(Application、Applet)。

2.打印输出10行杨晖三角形

3.有下面一段Server段程序,目的是能够同时服务多个客户,客户的请求是一句话(一个 String)。如果这个请求的内容是字符串"plain"的话,服务器仅将"hello"字符串返回给用户。否则将用户的话追加到当前目录的文本文件Memo.txt中(路径为"Memo.txt"),并向用户返回"OK"。注意Server并发的处理多用户,Memo.txt被共享,要求不能出现数据不一致。Server的程序如下文件Server.java:

   public class Server{

    public static void main(String args[]){

     MemoController memoController = new MemoController();

     try{

       ServerSocket ss = new ServerSocket(1999);

        while (true){

         Socket s = ss.accept();

         try{

           UserThread t = new UserThread(s, memoController);

           t.start();

           }catch(Exception e){

            e.printStackTrace();

           }

         }

        }catch(Exception e){

          e.printStackTrace();

        }finally{

          memoController.close();

        }

      }

    }
 类UserThread程序如下:

  文件UserThread.java:

   public class UserThread extends Thread{

    Socket s;

    MemoController memo;

    public UserThread (Socket s, MemoController memo){

     this.s = s;

     this.memo = memo;

    }

    public void run(){

     try{

       BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));

       PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));

       String req = br.readLine();

       if (req.equals("plain")){

        pw.println("hello");

       }else{

        memo.append(req);

        pw.println("OK");

       }

       pw.flush();

       pw.close();

       br.close();

       s.close();

     }catch(Exception e){

      e.printStackTrace();

     }

    }

   }
请根据题目的要求和现有的Server.java, UserThread.java的程序完成类MemoController.java的程序。

4.用输入/输出写一个程序,让用户输入一些姓名和电话号码。每一个姓名和号码将加在文件里。用户通过点"Done"按钮来告诉系统整个列表已输入完毕。如果用户输入完整个列表,程序将创建一个输出文件并显示或打印出来。 格式如:555-1212,Tom 123-456-7890,Peggy L. 234-5678,Marc 234-5678,Ron 87**321,Beth&Brian 33.1.42.45.70,Jean-Marc

一、 选择题答案

选择第1题

B

选择第2题

A

选择第3题

C

选择第4题

B

选择第5题

A

选择第6题

A

选择第7题

B

选择第8题

C

选择第9题

B

选择第10题

B

选择第11题

C  

二、多项选择题答案

多项选择第1题

BD

多项选择第2题

AE

多项选择第3题

AC

多项选择第4题

AE

多项选择第5题

AE

多项选择第6题

DEF

多项选择第7题

AC

多项选择第8题

BC

多项选择第9题

AB

多项选择第10题

CD

多项选择第11题

CD

多项选择第12题

ACD

多项选择第13题

ABD  

三、 填空题答案

填空第1题

x=10,a=3,b=4,c=5

填空第2题

java.util

填空第3题

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

填空第4题

1

填空第5题

26  

四、 编程题答案

1

 public class HelloWorld

  { 

  public static void main(String args[])

  {

   System.out.println("Hello,World!");

  }

 }

 import java.awt.Graphics;

 import java.applet.Applet;

 public class HelloWorld extends Applet{

  String s;

 public void init(){

  s="Hello World!";

 }

 public void paint(Graphics g){

  g.drawString(s,25,25);

 }

}

2

 class yanghui

 {

 public static void main (String args[])

 {

  int i,j;

  int yhlevel=10;

  int yanghui[][];

  System.out.println("杨晖三角形:");

  yanghui=new int[yhlevel][];

  for(i=0;i<yanghui.length;i++)

   yanghui[i]=new int[i+1];

   yanghui[0][0]=1;

  for (i=1; i<yanghui.length;i++)

  {

  yanghui[i][0]=1;

  for(j=1;j<yanghui[i].length-1;j++)

   yanghui[i][j]=yanghui[i-1][j-1]+yanghui[i-1][j];

   yanghui[i][yanghui[i].length-1]=1;

  }

for (i=0; i<yanghui.length;i++)

  {

   for(j=0;j<yanghui[i].length;j++)

   System.out.print(yanghui[i][j]+" ");

   System.out.println();

   }

  }

 }

输出结果是:

杨晖三角形:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1

1 9 36 84 126 126 84 36 9 1

3

 import java.io.*;

 public class MemoController{

  FileOutputStream fos;

  OutputStreamWriter osw;

  BufferedWriter bw;

 public MemoController(){

  try{

    fos=new FileOutputStream("memo.txt",true);

    osw=new OutputStreamWriter(fos);

    bw=new BufferedWriter(osw);

    }catch(FileNotFoundException e){};

  }

 public synchronized void append(String s){

  try{

    bw.write(s,0,s.length());

    bw.flush();

    bw.close();

    osw.close();

    fos.close();

    }catch(IOException e){}

  }

 public static void main(String args[]){

  MemoController mmc=new MemoController();

  mmc.append("I am xubin ");

  }

 }

4

 import java.io.*;

 class Phones

 {

  static FileOutputStream fos;

  public static final int lineLength = 81;

  public static void main(String args[]) throws IOException

  {

   byte[] phone = new byte[lineLength];

   byte[] name = new byte[lineLength];

   int I;

   try

   {

    fos = new FileOutputStream("phone.numbers");

  }

   catch(FileNotFoundException e)

   { }

   while (true)

   {

    System.err.println("Enter a name (enter 'done' to quit)");

    readLine(name);

    if ("done".equalsIgnoreCase(new String(name,0,0,4)))

    {

     break;

    }

    System.err.println("Enter the phone number");

    readLine(phone);

    for (int i=0;phone[i]!= 0;i++)

   {

    fos.write(phone[i]);

   }

    fos.write(',');

    for (int i=0;name[i]!= 0;i++)

   {

    fos.write(name[i]);

   }

    fos.write('/n');

   }

    fos.close();

   }

   private static void readLine(byte line[]) throws IOException

  {

   int i=0,b=0;

   while ((i<lineLength-1)&&((b=System.in.read())!='/n'))

   {

    line[i++] = (byte)b;

   }

    line[i]=(byte) 0;

   }

  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: