|
一、 選擇
1.欲構造ArrayList類的一個實例,此類繼承了List接口,下列哪個方法是正確的 ? B
A ArrayList myList=new Object();
B List myList=new ArrayList();
C ArrayList myList=new List();
D List myList=new List();
2.paint()方法使用哪種類型的參數(shù)? A
A Graphics
B Graphics2D
C String
D Color
3.指出正確的表達式 C
A byte=128;
B Boolean=null;
C long l=0xfffL;
D double=0.9239d;
4.指出下列程序運行的結果 B
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.運行下列程序, 會產(chǎn)生什么結果 A
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 第一行會產(chǎn)生編譯錯誤
B 第六行會產(chǎn)生編譯錯誤
C 第六行會產(chǎn)生運行錯誤
D 程序會運行和啟動
6.要從文件" file.dat"文件中讀出第10個字節(jié)到變量C中,下列哪個方法適合? A
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.容器被重新設置大小后,哪種布局管理器的容器中的組件大小不隨容器大小的變化而改變? B
A CardLayout
B FlowLayout
C BorderLayout
D GridLayout
8.給出下面代碼: C
public class Person{ static int arr[] = new int[10]; public static void main(String a[]) { System.out.println(arr[1]); } } 那個語句是正確的?
A 編譯時將產(chǎn)生錯誤;
B 編譯時正確,運行時將產(chǎn)生錯誤;
C 輸出零;
D 輸出空。
9.哪個關鍵字可以對對象加互斥鎖? B
A transient
B synchronized
C serialize
D static
10.下列哪些語句關于內存回收的說明是正確的? B
A 程序員必須創(chuàng)建一個線程來釋放內存;
B 內存回收程序負責釋放無用內存
C 內存回收程序允許程序員直接釋放內存
D 內存回收程序可以在指定的時間釋放內存對象
11.下列代碼哪幾行會出錯: C
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
二、多項選擇
1.執(zhí)行下列代碼后,哪個結論是正確的 String[] s=new String[10]; BD
A s[10] 為 ""; B s[9] 為 null;
C s[0] 為 未定義
D s.length 為10
2.下面的表達式哪個是正確的? AE
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.選出合理的標識符 AC
A _sys1_lll
B 2mail
C $change
D class
4.哪個布局管理器使用的是組件的最佳尺寸( preferred size)AE
A FlowLayout
B BorderLayout
C GridLayout
D CardLayout
E.GridBagLayout
5.下列哪個方法可用于創(chuàng)建一個可運行的類? AE
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.下面哪個方法可以在任何時候被任何線程調用? DEF
A notify()
B wait()
C notifyAll()
D sleep()
E.yield()
F.synchronized(this)
7.構造BufferedInputStream的合適參數(shù)是哪個? AC
A BufferedInputStream
B BufferedOutputStream
C FileInputStream
D FileOuterStream
E. File
8.下列說法正確的是 BC
A java.lang.Clonable是類
B java.lang.Runnable是接口
C Double對象在java.lang包中
D Double a=1.0是正確的java語句
9.指出正確的表達式 AB
A double a=1.0;
B Double a=new Double(1.0);
C byte a = 340;
D Byte a = 120;
10.定義一個類名為"MyClass.java"的類,并且該類可被一個工程中的所有類訪問,那么該類的正確聲明應為: CD
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){}為合理的重載方法。 CD
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.如果下列的方法能夠正常運行,在控制臺上將顯示什么? ACD
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.下列哪些情況可以終止當前線程的運行? ABD
A 拋出一個例外時。
B 當該線程調用sleep()方法時。
C 當創(chuàng)建一個新線程時。
D 當一個優(yōu)先級高的線程進入就緒狀態(tài)時。
三、 填空題
1.執(zhí)行下列代碼后的結果是什么? int x,a=2,b=3,c=4; x=++a+b+++c++; x=10,a=3,b=4,c=5
2. java.util
包包含了Collection的接口和類的API 3.main方法的聲明格式包括 : (public )(static )(void)(main)(String args[])
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()); } } 1
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); } } 26
四、 編程題
1.編寫一個輸出"Hello World!"的程序,用兩種方式實現(xiàn)(Application、Applet)。
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.打印輸出10行楊暉三角形
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.有下面一段Server段程序,目的是能夠同時服務多個客戶,客戶的請求是一句話(一個 String)。如果這個請求的內容是字符串"plain"的話,服務器僅將"hello"字符串返回給用戶。否則將用戶的話追加到當前目錄的文本文件Memo.txt中(路徑為"Memo.txt"),并向用戶返回"OK"。注意Server并發(fā)的處理多用戶,Memo.txt被共享,要求不能出現(xiàn)數(shù)據(jù)不一致。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(); } } } 請根據(jù)題目的要求和現(xiàn)有的Server.java, UserThread.java的程序完成類MemoController.java的程序。
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.用輸入/輸出寫一個程序,讓用戶輸入一些姓名和電話號碼。每一個姓名和號碼將加在文件里。用戶通過點"Done"按鈕來告訴系統(tǒng)整個列表已輸入完畢。 如果用戶輸入完整個列表,程序將創(chuàng)建一個輸出文件并顯示或打印出來。 格式如:555-1212,Tom 123-456-7890,Peggy L. 234-5678,Marc 234-5678,Ron 876-4321,Beth&Brian 33.1.42.45.70,Jean-Marc
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; } } (難度系數(shù):3) [正確答案] 一、 選擇題答案 B A C B A A B C B B 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; } } |
|
|
來自: 書香宜人 > 《數(shù)據(jù)結構@JAVA》