1、Java基础面试题范本模板1.下面能够得到文件“file。txt”的父路径的是:A.String name= File。getParentName(“file.txt); B.String name= (new File(“file。txt”))。getParent();C.String name = (new File(“file.txt”).getParentName();D.String name= (new File(“file.txt”).getParentFile(); 2.在Java中,要创建一个新目录,要使用的类的实例是:A。FileB.FileOutputStreanC.Pr
2、intWriterD。Dir3。题目代码的功能为:在d:创建一个文件“test。txt”,并向文件写入“HelloWorld”,然后删除文件. public static void main(String args) File file = new File(d:”, file。txt”); try catch (Exception e) e。printStackTrace(); A。 BufferedWriter bw = new BufferedWriter(new FileWriter(file); bw.write(”HelloWorld); bw.close(); if (file.
3、exists()) file。delete(); B。 BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write(”HelloWorld); bw.close(); if (file.exists() file.deleteFile(); C。BufferedWriter bw = new BufferedWriter(file); bw。write(”HelloWorld); bw.close(); if (file.exists()) file.delete(); D. BufferedWriter bw
4、= new BufferedWriter(file); bw.write(”HelloWorld”); bw.close(); if (file.exists()) file。deleteFile(); 4。题目代码功能为:打印扩展名为txt的文件public static void main(String args) String dir=d:/javalesson”; File currDir=new File(dir); FilenameFilter filter=new JavaFilter(); String javaFiles=currDir.list(filter); for(i
5、nt i=0;ijavaFiles。length;i+) System.out。println(javaFilesi); 代码中JavaFilter类的代码为:A。public class JavaFilter implements FilenameFilter public void accept(File dir, String name) return name。endsWith(”。txt”); B.public class JavaFilter implements FilenameFilter public boolean accept(File dir, String name)
6、 return name.endsWith(.txt”); C.public class JavaFilter extends FilenameFilter public boolean accept(File dir, String name) return name.endsWith(”。txt); D. public class JavaFilter extends FilenameFilter public void accept(File dir, String name) return name.endsWith(”。txt”); 5. 下列关于序列化和反序列化描述正确的是A。序列
7、化是将数据转换为 n个byte序列的过程B.反序列化是将n个 byte 转换为一个数据的过程C。将类型int 转换为4 byte是反序列化过程D。将8个字节转换为long类型的数据是序列化过程6。请看下列代码:interface Foo class Alpha implements Foo class Beta extends Alpha public class Delta extends Beta public static void main(String args) Beta x = new Beta(); 插入代码 在插入代码处,填入下列代码,运行时能引起java.lang。Clas
8、sCastException异常的是:A。 Alpha a = x;B. Foo f= (Delta)x;C。 Foo f= (Alpha)x;D. Beta b = (Beta)(Alpha)x;7.请看下列代码:1:public class Foo 2: public static void main(String args) 3: try 4: A a = new A();5: a.method1();6: catch (Exception e) 7: System.out。print(an error occurred”);8: 9: 10:1:class A 2: public vo
9、id method1() 3: B b = new B();4: b。method2();5: System。out。println(”method one);6: 7:1:class B 2: public void method2() 3: C c = new C();4: c。method3();5: System.out.println(”method two”);6: 7:1:class C 2: public void method3() 3: System。out。println(”method three);4: throw new NullPointerException()
10、;5: 6:关于上述代码说法正确的是:A.Foo类的第7行将被执行B。A类的第5行将被执行C。B类的第5行将被执行D。C类的第3行将被执行8.请看下列代码:public class A public String sayHello(String name) throws TestException if (name = null) throw new TestException(); return ”Hello + name; public static void main(String args) throws TestException A a=new A(); System。out.pr
11、intln(a。sayHello(null); class TestException extends Exception 关于上述代码说法正确的是:A。A类编译失败B.代码”System.out。println(a.sayHello(John”));行,会抛出未检查异常TestExceptionC.代码” A a=new A();”行,会抛出未检查异常TestExceptionD。代码”System.out.println(a.sayHello(John”));”行,会抛出已检查异常TestException9.请看下列代码:1。 / some code here2。 try 3. / so
12、me code here4. catch (SomeException se) 5. / some code here6。 finally 7. / some code here8. 下面哪三种情况能使第7行的代码执行:A.第3行抛出异常B.第1行抛出异常C。第5行抛出异常D。第3行代码成功执行10.以下说法错误的是:A.try catch 。 catch,多个catch时,后一个catch捕获类型一定是前一个的父类B。try。finally 可以组合使用C。throw抛出一些异常,程序不进行处理,程序编译也无错误D.throws 一定是写在方法后面11。下列代码运行的结果是:public c
13、lass A public void process() System。out。print(A ); public static void main(String args) try (A) new B())。process(); catch (Exception e) System.out.print(”Exception”); class B extends A public void process() throws RuntimeException super。process(); if (true) throw new RuntimeException(); System.out.p
14、rint(B ); A.ExceptionB。A ExceptionC。A Exception BD。A B Exception12。下列代码实现的功能是: FileOutputStream fos = new FileOutputStream(”system。txt,true); PrintStream ps = new PrintStream(fos); System。setOut(ps);System.out。println(writer); A。向控制台打印“writer,可以实现追加打印B。向控制台打印“writer,但是不可以实现追加打印C.向文件system.txt写“write
15、r”,但是不可以实现追加写D。向文件system。txt写“writer”,可以实现追加写13。在Java中,下面的代码会出现编译错误的是:A.File f = new File(”/,autoexec.bat);B.DataInputStream din = new DataInputStream(new FileInputStream(”autoexec。bat”);C。InputStreamReader in = new InputStreamReader(System.in);D.OutputStreamWriter out = new OutputStreamWriter(Syste
16、m.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 PrintWriter out=new PrintWriter(new FileOutputStream(“d:/abc.txt”)) ; String name=”tare
17、na; out。print(name) ; out。close( ) ;catch(Execption e) System.out。println(“文件没有发现!“) ;A。将在控制台上打印:“文件没有发现!B。正常运行,但没有生成文件abc.txtC。运行后生成abc。txt ,但该文件中无内容D.运行后生成abc.txt,该文件内容为:tarena16. 关于java.io。Serializable接口说法正确的是:A。java.io。Serializable中有一个serialID属性,但是没有方法B.类通过实现java.io.Serializable 接口以启用其序列化功能C。jav
18、a.io.Serializable中有一个run方法,但是没属性D.java。io.Serializable接口没有方法或属性,仅用于标识可序列化的语义。17。关于线程的设计,下列描述正确的是:A。线程对象必须实现Runnable接口B.启动一个线程直接调用线程对象的run()方法C。Java提供对多线程同步提供语言级的支持D. 一个线程可以包含多个进程18。下列代码编译和运行的结果是: public static void main(String args) Runnable r = new Runnable() public void run() System。out.print(”Cat
19、”); ; Thread t = new Thread(r) public void run() System.out.print(Dog”); ; t。start(); A.CatB.DogC.运行无输出D。编译失败19。下面能使线程处于阻塞状态的是:A.sleep();B。notify();C。synchronized(Object obj)D.wait();20。下列代码编译和运行的结果是:public class TestOne public static void main(String args) throws Exception Thread.sleep(3000); Syste
20、m.out。println(”sleep”); A.编译错误B。抛出运行时异常C。输出:sleepD.代码正常运行,但是无输出21.现有一个线程dt,把dt线程设置为守护线程的方式正确的是:A。Thread.setDaemon(true);B.dt。daemon(true);C.Thread.daemon(true);D.dt。setDaemon(true);22.请看下列代码:class ThreadDemo implements Runnable int age=0; public synchronized void run() for(int i=0;i100;i+) System。ou
21、t.println(”age=”+(age+)); 下列构造线程对象的方式中,两个线程共用一个age数据的是:A。ThreadDemo r1=new ThreadDemo();ThreadDemo r2=new ThreadDemo();B。Thread t1=new Thread();Thread t2=new Thread();C。ThreadDemo r1=new ThreadDemo();Thread t1=new Thread(r1);Thread t2=new Thread(r1);D.ThreadDemo r1=new ThreadDemo();ThreadDemo r2=new
22、 ThreadDemo();Thread t1=new Thread(r1);Thread t2=new Thread(r2);23.可以对对象加互斥锁的关键字是:A.transient B。synchronized C.serialize D.static24.下列属于线程安全的类的是A.AarryListB.VectorC.HashMapD。Hashtable25。下面关于ServerSocket描述正确的是:A.ServerSocket类实现服务器套接字B。ServerSocket类的accept()方法侦听并接受到客户端套接字C。一个服务器端只能有一个客户端进行连接D。一个服务器端可以有多个客户端进行连接
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1