JAVA考试试题一.docx
- 文档编号:18053158
- 上传时间:2023-08-07
- 格式:DOCX
- 页数:13
- 大小:20.74KB
JAVA考试试题一.docx
《JAVA考试试题一.docx》由会员分享,可在线阅读,更多相关《JAVA考试试题一.docx(13页珍藏版)》请在冰点文库上搜索。
JAVA考试试题一
Java试题
(一)
一、选择
1.欲构造ArrayList类的一个实例,此类继承了List接口,下列哪个方法是正确的?
(B)
AArrayListmyList=newObject();
BListmyList=newArrayList();
CArrayListmyList=newList();
DListmyList=newList();
2.paint()方法使用哪种类型的参数?
(A)
AGraphics
BGraphics2D
CString
DColor
3.指出正确的表达式(C)
Abyte=128;
BBoolean=null;
Clongl=0xfffL;
Ddouble=0.9239d;
4.指出下列程序运行的结果(B)
publicclassExample{
Stringstr=newString("good");
char[]ch={'a','b','c'};
publicstaticvoidmain(Stringargs[]){
Exampleex=newExample();
ex.change(ex.str,ex.ch);
System.out.print(ex.str+"and");
Sytem.out.print(ex.ch);
}
publicvoidchange(Stringstr,charch[]){
str="testok";
ch[0]='g';
}
}
Agoodandabc
Bgoodandgbc
Ctestokandabc
Dtestokandgbc
5.运行下列程序,会产生什么结果(A)
publicclassXextendsThreadimplementsRunable{
publicvoidrun(){
System.out.println("thisisrun()");
}
publicstaticvoidmain(Stringargs[])
{
Threadt=newThread(newX());
t.start();
}
}
A第一行会产生编译错误
B第六行会产生编译错误
C第六行会产生运行错误
D程序会运行和启动
6.要从文件"file.dat"文件中读出第10个字节到变量C中,下列哪个方法适合?
(A)
AFileInputStreamin=newFileInputStream("file.dat");in.skip(9);intc=in.read();
BFileInputStreamin=newFileInputStream("file.dat");in.skip(10);intc=in.read();
CFileInputStreamin=newFileInputStream("file.dat");intc=in.read();
DRandomAccessFilein=newRandomAccessFile("file.dat");in.skip(9);intc=in.readByte();
7.容器被重新设置大小后,哪种布局管理器的容器中的组件大小不随容器大小的变化而改变?
(B)
\ACardLayout
BFlowLayout
CBorderLayout
DGridLayout
8.给出下面代码:
(C)
publicclassPerson{
staticintarr[]=newint[10];
publicstaticvoidmain(Stringa[])
{
System.out.println(arr[1]);
}
}
那个语句是正确的?
A编译时将产生错误;
B编译时正确,运行时将产生错误;
C输出零;
D输出空。
9.哪个关键字可以对对象加互斥锁?
(B)
Atransient
Bsynchronized
Cserialize
Dstatic
10.下列哪些语句关于内存回收的说明是正确的?
(B)
A程序员必须创建一个线程来释放内存;
B内存回收程序负责释放无用内存
C内存回收程序允许程序员直接释放内存
D内存回收程序可以在指定的时间释放内存对象
11.下列代码哪几行会出错:
(C)
1)publicvoidmodify(){
2)intI,j,k;
3)I=100;
4)while(I>0){
5)j=I*2;
6)System.out.println("Thevalueofjis"+j);
7)k=k+1;
8)I--;
9)}
10)}
Aline4
Bline6
Cline7
Dline8
二、多项选择
1.执行下列代码后,哪个结论是正确的String[]s=newString[10];(BD)
As[10]为"";
Bs[9]为null;
Cs[0]为未定义
Ds.length为10
2.下面的表达式哪个是正确的?
(AE)
AStrings="你好";inti=3;s+=i;
BStrings="你好";inti=3;if(i==s){s+=i};
CStrings="你好";inti=3;s=i+s;
DStrings="你好";inti=3;s=i+;
E.Strings=null;inti=(s!
=null)&&(s.length>0)?
s.length():
0;
3.选出合理的标识符(AC)
A_sys1_lll
B2mail
C$change
Dclass
4.哪个布局管理器使用的是组件的最佳尺寸(preferredsize)(AE)
AFlowLayout
BBorderLayout
CGridLayout
DCardLayout
E.GridBagLayout
5.下列哪个方法可用于创建一个可运行的类?
(AB)
ApublicclassXimplementsRunable{publicvoidrun(){......}}
BpublicclassXimplementsThread{publicvoidrun(){......}}
CpublicclassXimplementsThread{publicintrun(){......}}
DpublicclassXimplementsRunable{protectedvoidrun(){......}}
6.下面哪个方法可以在任何时候被任何线程调用?
(DEF)
Anotify()
Bwait()
CnotifyAll()
Dsleep()
E.yield()
F.synchronized(this)
7.构造BufferedInputStream的合适参数是哪个?
(AC)
ABufferedInputStream
BBufferedOutputStream
CFileInputStream
DFileOuterStream
E.File
8.下列说法正确的是(BC)
Ajava.lang.Clonable是类
Bjava.lang.Runnable是接口
CDouble对象在java.lang包中
DDoublea=1.0是正确的java语句
9.指出正确的表达式(AB)
Adoublea=1.0;
BDoublea=newDouble(1.0);
Cbytea=340;
DBytea=120;
10.定义一个类名为"MyClass.java"的类,并且该类可被一个工程中的所有类访问,那么该类的正确声明应为:
(CD)
AprivateclassMyClassextendsObject
BclassMyClassextendsObject
CpublicclassMyClass
DpublicclassMyClassextendsObject
11.指出下列哪个方法与方法publicvoidadd(inta){}为合理的重载方法。
(CD)
Apublicintadd(inta)
Bpublicvoidadd(longa)
Cpublicvoidadd(inta,intb)
Dpublicvoidadd(floata)
12.如果下列的方法能够正常运行,在控制台上将显示什么?
(ACD)
publicvoidexample(){
try{
unsafe();
System.out.println("Test1");
}
catch(SafeExceptione)
{System.out.println("Test2");}
finally{System.out.println("Test3");}
System.out.println("Test4");
}
ATest1
BTest2
CTest3
DTest4
13.下列哪些情况可以终止当前线程的运行?
(ABD)
A抛出一个例外时。
B当该线程调用sleep()方法时。
C当创建一个新线程时。
D当一个优先级高的线程进入就绪状态时。
三、填空题
1.执行下列代码后的结果是什么?
intx,a=2,b=3,c=4;x=++a+b+++c++;(X=10,A=2,B=4,C=5注:
x=(++a)+(b++)+(c++))
2.______包包含了Collection的接口和类的API(java.util)
3.main方法的声明格式包括_________((public)(static)(void)(main)(Stringargs[]))
4.下列程序中构造了一个SET并且调用其方法add(),输出结果是____
(1)
publicclassA{
publicinthashCode(){return1;}
publicBooleanequals(Objectb){returntrue}
publicstaticvoidmain(Stringargs[]){Setset=newHashSet();
set.add(newA());
set.add(newA());
set.add(newA());
System.out.println(set.size());
}
}
5.下列程序的运行结果是___(26)
classA{
classDog{
privateStringname;
privateintage;
publicintstep;
Dog(Strings,inta)
{
name=s;
age=a;
step=0;
}
publicvoidrun(Dogfast)
{
fast.step++;
}
}
publicstaticvoidmain(Stringargs[])
{
Aa=newA();
Dogd=a.newDog("Tom",3);
d.step=25;
d.run(d);
System.out.println(d.step);
}
}
四、编程题
1.编写一个输出"HelloWorld!
"的程序,用两种方式实现(Application、Applet)。
2.打印输出10行杨晖三角形
3.有下面一段Server段程序,目的是能够同时服务多个客户,客户的请求是一句话(一个String)。
如果这个请求的内容是字符串"plain"的话,服务器仅将"hello"字符串返回给用户。
否则将用户的话追加到当前目录的文本文件Memo.txt中(路径为"Memo.txt"),并向用户返回"OK"。
注意Server并发的处理多用户,Memo.txt被共享,要求不能出现数据不一致。
Server的程序如下文件Server.java:
publicclassServer{
publicstaticvoidmain(Stringargs[]){
MemoControllermemoController=newMemoController();
try{
ServerSocketss=newServerSocket(1999);
while(true){
Sockets=ss.accept();
try{
UserThreadt=newUserThread(s,memoController);
t.start();
}catch(Exceptione){
e.printStackTrace();
}
}
}catch(Exceptione){
e.printStackTrace();
}finally{
memoController.close();
}
}
}
类UserThread程序如下:
文件UserThread.java:
publicclassUserThreadextendsThread{
Sockets;
MemoControllermemo;
publicUserThread(Sockets,MemoControllermemo){
this.s=s;
this.memo=memo;
}
publicvoidrun(){
try{
BufferedReaderbr=newBufferedReader(newInputStreamReader(s.getInputStream()));
PrintWriterpw=newPrintWriter(newOutputStreamWriter(s.getOutputStream()));
Stringreq=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(Exceptione){
e.printStackTrace();
}
}
}
请根据题目的要求和现有的Server.java,UserThread.java的程序完成类MemoController.java的程序。
4.用输入/输出写一个程序,让用户输入一些姓名和电话号码。
每一个姓名和号码将加在文件里。
用户通过点"Done"按钮来告诉系统整个列表已输入完毕。
如果用户输入完整个列表,程序将创建一个输出文件并显示或打印出来。
格式如:
555-1212,Tom123-456-7890,PeggyL.234-5678,Marc234-5678,Ron876-4321,Beth&Brian33.1.42.45.70,Jean-Marc
编程题答案
1
publicclassHelloWorld
{
publicstaticvoidmain(Stringargs[])
{
System.out.println("Hello,World!
");
}
}
importjava.awt.Graphics;
importjava.applet.Applet;
publicclassHelloWorldextendsApplet{
Strings;
publicvoidinit(){
s="HelloWorld!
";
}
publicvoidpaint(Graphicsg){
g.drawString(s,25,25);
}
}
2
classyanghui
{
publicstaticvoidmain(Stringargs[])
{
inti,j;
intyhlevel=10;
intyanghui[][];
System.out.println("杨晖三角形:
");
yanghui=newint[yhlevel][];
for(i=0;i yanghui[i]=newint[i+1]; yanghui[0][0]=1; for(i=1;i { yanghui[i][0]=1; for(j=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 { for(j=0;j System.out.print(yanghui[i][j]+""); System.out.println(); } } } 输出结果是: 杨晖三角形: 1 11 121 1331 14641 15101051 1615201561 172135352171 18285670562881 193684126126843691 3 importjava.io.*; publicclassMemoController{ FileOutputStreamfos; OutputStreamWriterosw; BufferedWriterbw; publicMemoController(){ try{ fos=newFileOutputStream("memo.txt",true); osw=newOutputStreamWriter(fos); bw=newBufferedWriter(osw); }catch(FileNotFoundExceptione){}; } publicsynchronizedvoidappend(Strings){ try{ bw.write(s,0,s.length()); bw.flush(); bw.close(); osw.close(); fos.close(); }catch(IOExceptione){} } publicstaticvoidmain(Stringargs[]){ MemoControllermmc=newMemoController(); mmc.append("Iamxubin"); } } 4 importjava.io.*; classPhones { staticFileOutputStreamfos; publicstaticfinalintlineLength=81; publicstaticvoidmain(Stringargs[])throwsIOException { byte[]phone=newbyte[lineLength]; byte[]name=newbyte[lineLength]; intI; try { fos=newFileOutputStream("phone.numbers"); } catch(FileNotFoundExceptione) {} while(true) { System.err.println("Enteraname(enter'done'toquit)"); readLine(name); if("done".equalsIgnoreCase(newString(name,0,0,4))) { break; } System.err.println("Enterthephonenumber"); readLine(phone); for(inti=0;phone[i]! =0;i++) { fos.write(phone[i]); } fos.write(','); for(inti=0;name[i]
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 考试 试题