Java基础面试题.docx
- 文档编号:28392621
- 上传时间:2023-07-13
- 格式:DOCX
- 页数:15
- 大小:18.80KB
Java基础面试题.docx
《Java基础面试题.docx》由会员分享,可在线阅读,更多相关《Java基础面试题.docx(15页珍藏版)》请在冰豆网上搜索。
Java基础面试题
1.下面能够得到文件“file.txt"的父路径的是:
A。
Stringname=File。
getParentName(“file.txt”);
B。
Stringname=(newFile(“file.txt”)).getParent();
C.Stringname=(newFile(“file.txt”)).getParentName();
D.Stringname=(newFile(“file.txt”))。
getParentFile();
2。
在Java中,要创建一个新目录,要使用的类的实例是:
A.File
B.FileOutputStrean
C.PrintWriter
D。
Dir
3.题目代码的功能为:
在d:
创建一个文件“test.txt”,并向文件写入
“HelloWorld”,然后删除文件。
publicstaticvoidmain(String[]args){
Filefile=newFile("d:
\\","file.txt”);
try{
<填入代码>
}catch(Exceptione){
e。
printStackTrace();
}
}
A.BufferedWriterbw=newBufferedWriter(newFileWriter(file));
bw.write("HelloWorld");
bw.close();
if(file。
exists()){
file.delete();
}
B.BufferedWriterbw=newBufferedWriter(newFileWriter(file));
bw。
write(”HelloWorld");
bw.close();
if(file.exists()){
file。
deleteFile();
}
C。
BufferedWriterbw=newBufferedWriter(file);
bw.write(”HelloWorld”);
bw.close();
if(file。
exists()){
file。
delete();
}
D.BufferedWriterbw=newBufferedWriter(file);
bw。
write("HelloWorld”);
bw。
close();
if(file.exists()){
file。
deleteFile();
}
4.题目代码功能为:
打印扩展名为txt的文件
publicstaticvoidmain(String[]args){
Stringdir=”d:
/javalesson”;
FilecurrDir=newFile(dir);
FilenameFilterfilter=newJavaFilter();
String[]javaFiles=currDir。
list(filter);
for(inti=0;i System。 out。 println(javaFiles[i]); } } 代码中JavaFilter类的代码为: A。 publicclassJavaFilterimplementsFilenameFilter{ publicvoidaccept(Filedir,Stringname){ returnname.endsWith(”.txt”); } } B.publicclassJavaFilterimplementsFilenameFilter{ publicbooleanaccept(Filedir,Stringname){ returnname。 endsWith(”。 txt”); } } C.publicclassJavaFilterextendsFilenameFilter{ publicbooleanaccept(Filedir,Stringname){ returnname。 endsWith(”。 txt”); } } D。 publicclassJavaFilterextendsFilenameFilter{ publicvoidaccept(Filedir,Stringname){ returnname。 endsWith("。 txt”); } } 5。 下列关于序列化和反序列化描述正确的是 A。 序列化是将数据转换为n个byte序列的过程 B。 反序列化是将n个byte转换为一个数据的过程 C。 将类型int转换为4byte是反序列化过程 D。 将8个字节转换为long类型的数据是序列化过程 6。 请看下列代码: interfaceFoo{ } classAlphaimplementsFoo{ } classBetaextendsAlpha{ } publicclassDeltaextendsBeta{ publicstaticvoidmain(String[]args){ Betax=newBeta(); <插入代码〉 } } 在<插入代码>处,填入下列代码,运行时能引起java。 lang.ClassCastException异 常的是: A.Alphaa=x; B.Foof=(Delta)x; C.Foof=(Alpha)x; D。 Betab=(Beta)(Alpha)x; 7。 请看下列代码: 1: publicclassFoo{ 2: publicstaticvoidmain(String[]args){ 3: try{ 4: Aa=newA(); 5: a。 method1(); 6: }catch(Exceptione){ 7: System。 out.print("anerroroccurred"); 8: } 9: } 10: } 1: classA{ 2: publicvoidmethod1(){ 3: Bb=newB(); 4: b.method2(); 5: System。 out。 println("methodone"); 6: } 7: } 1: classB{ 2: publicvoidmethod2(){ 3: Cc=newC(); 4: c。 method3(); 5: System。 out.println(”methodtwo"); 6: } 7: } 1: classC{ 2: publicvoidmethod3(){ 3: System.out.println("methodthree"); 4: thrownewNullPointerException(); 5: } 6: } 关于上述代码说法正确的是: A.Foo类的第7行将被执行 B.A类的第5行将被执行 C.B类的第5行将被执行 D。 C类的第3行将被执行 8。 请看下列代码: publicclassA{ publicStringsayHello(Stringname)throwsTestException{ if(name==null){ thrownewTestException(); } return”Hello”+name; } publicstaticvoidmain(String[]args)throwsTestException{ Aa=newA(); System。 out.println(a。 sayHello(null)); } } classTestExceptionextendsException{ } 关于上述代码说法正确的是: A。 A类编译失败 B.代码"System.out。 println(a。 sayHello(”John”));"行,会抛出未检查异常 TestException C.代码”Aa=newA();"行,会抛出未检查异常TestException D.代码"System。 out。 println(a.sayHello("John”));"行,会抛出已检查异常 TestException 9.请看下列代码: 1.//somecodehere 2。 try{ 3.//somecodehere 4.}catch(SomeExceptionse){ 5。 //somecodehere 6.}finally{ 7。 //somecodehere 8。 } 下面哪三种情况能使第7行的代码执行: A.第3行抛出异常 B.第1行抛出异常 C。 第5行抛出异常 D。 第3行代码成功执行 10。 以下说法错误的是: A。 try…catch..catch,多个catch时,后一个catch捕获类型一定是前一个的父类 B。 try….finally可以组合使用 C.throw抛出一些异常,程序不进行处理,程序编译也无错误 D。 throws一定是写在方法后面 11.下列代码运行的结果是: publicclassA{ publicvoidprocess(){ System。 out。 print("A”); } publicstaticvoidmain(String[]args){ try{ ((A)newB()).process(); }catch(Exceptione){ System。 out。 print(”Exception"); } } } classBextendsA{ publicvoidprocess()throwsRuntimeException{ super.process(); if(true) thrownewRuntimeException(); System。 out.print("B"); } } A.Exception B.AException C。 AExceptionB D.ABException 12。 下列代码实现的功能是: FileOutputStreamfos=newFileOutputStream(”system.txt",true); PrintStreamps=newPrintStream(fos); System.setOut(ps); System.out。 println("writer”); A.向控制台打印“writer”,可以实现追加打印 B.向控制台打印“writer",但是不可以实现追加打印 C。 向文件system.txt写“writer”,但是不可以实现追加写 D.向文件system。 txt写“writer”,可以实现追加写 13.在Java中,下面的代码会出现编译错误的是: A.Filef=newFile("/”,"autoexec.bat”); B.DataInputStreamdin=newDataInputStream(newFileInputStream(”autoexec。 bat”)); C.InputStreamReaderin=newInputStreamReader(System。 in); D.OutputStreamWriterout=newOutputStreamWriter(System。 in); 14.java.io.BufferedWriter和java。 io.FileWriter相比,java。 io。 FileWriter 没有但是java.io。 BufferedWriter有的方法是: A。 关闭流的方法 B.刷新流的缓冲的方法 C。 写入流的方法 D。 写入一个行分隔符 15。 给定一个Java程序的main方法的代码片段如下: 假如d盘下不存在abc.txt文 件,现运行该程序,下面说法正确的是() try{ PrintWriterout=newPrintWriter(newFileOutputStream(“d: /abc.txt”)); Stringname=”tarena”; out。 print(name); out。 close(); } catch(Execptione){ System.out.println(“文件没有发现! “); } A。 将在控制台上打印: “文件没有发现! ” B。 正常运行,但没有生成文件abc。 txt C.运行后生成abc.txt,但该文件中无内容 D.运行后生成abc.txt,该文件内容为: tarena 16.关于java.io。 Serializable接口说法正确的是: A.java.io。 Serializable中有一个serialID属性,但是没有方法 B.类通过实现java。 io。 Serializable接口以启用其序列化功能 C。 java。 io.Serializable中有一个run方法,但是没属性 D.java.io。 Serializable接口没有方法或属性,仅用于标识可序列化的语义。 17。 关于线程的设计,下列描述正确的是: A.线程对象必须实现Runnable接口 B.启动一个线程直接调用线程对象的run()方法 C。 Java提供对多线程同步提供语言级的支持 D。 一个线程可以包含多个进程 18。 下列代码编译和运行的结果是: publicstaticvoidmain(String[]args){ Runnabler=newRunnable(){ publicvoidrun(){ System.out。 print(”Cat”); } }; Threadt=newThread(r){ publicvoidrun(){ System。 out。 print(”Dog”); } }; t。 start(); } A.Cat B.Dog C。 运行无输出 D。 编译失败 19.下面能使线程处于阻塞状态的是: A.sleep(); B。 notify(); C。 synchronized(Objectobj){} D。 wait(); 20。 下列代码编译和运行的结果是: publicclassTestOne{ publicstaticvoidmain(String[]args)throwsException{ Thread。 sleep(3000); System.out.println("sleep"); } } A。 编译错误 B。 抛出运行时异常 C。 输出: sleep D。 代码正常运行,但是无输出 21.现有一个线程dt,把dt线程设置为守护线程的方式正确的是: A。 Thread.setDaemon(true); B。 dt.daemon(true); C.Thread。 daemon(true); D.dt。 setDaemon(true); 22.请看下列代码: classThreadDemoimplementsRunnable{ intage=0; publicsynchronizedvoidrun(){ for(inti=0;i<100;i++){ System。 out。 println("age="+(age++)); } } } 下列构造线程对象的方式中,两个线程共用一个age数据的是: A。 ThreadDemor1=newThreadDemo(); ThreadDemor2=newThreadDemo(); B.Threadt1=newThread(); Threadt2=newThread(); C。 ThreadDemor1=newThreadDemo(); Threadt1=newThread(r1); Threadt2=newThread(r1); D.ThreadDemor1=newThreadDemo(); ThreadDemor2=newThreadDemo(); Threadt1=newThread(r1); Threadt2=newThread(r2); 23.可以对对象加互斥锁的关键字是: A。 transient B.synchronized C。 serialize D。 static 24。 下列属于线程安全的类的是 A。 AarryList B。 Vector C。 HashMap D.Hashtable 25。 下面关于ServerSocket描述正确的是: A.ServerSocket类实现服务器套接字 B。 ServerSocket类的accept()方法侦听并接受到客户端套接字 C.一个服务器端只能有一个客户端进行连接 D。 一个服务器端可以有多个客户端进行连接
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 基础 试题