Java基础面试题.docx

上传人:b****2 文档编号:2029060 上传时间:2022-10-26 格式:DOCX 页数:12 大小:17.86KB
下载 相关 举报
Java基础面试题.docx_第1页
第1页 / 共12页
Java基础面试题.docx_第2页
第2页 / 共12页
Java基础面试题.docx_第3页
第3页 / 共12页
Java基础面试题.docx_第4页
第4页 / 共12页
Java基础面试题.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

Java基础面试题.docx

《Java基础面试题.docx》由会员分享,可在线阅读,更多相关《Java基础面试题.docx(12页珍藏版)》请在冰豆网上搜索。

Java基础面试题.docx

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=new

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > PPT模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1