15道非常经典的Java面试题 附详细答案
程序员文章站
2024-03-12 08:33:32
试题如下:
参考答案:
import java.io.filenotfoundexception;
import java.io.ioex...
试题如下:
参考答案:
import java.io.filenotfoundexception; import java.io.ioexception; import java.util.arraylist; import java.util.concurrent.executorservice; import java.util.concurrent.executors; /** * created by ysc on 7/26/16. */ public class interview { private static void one(){ string str1 = "hello"; string str2 = "he"+new string("llo"); system.err.println(str1==str2); system.out.println("1. false"); } private static void two(){ int i = integer.max_value; system.err.println((i+1)<i); system.out.println("2. 存在一个i, 使得(i+1)<i"); } private static void three(){ system.err.println("gc is not a java thread, it is a native thread"); thread.getallstacktraces().keyset().foreach(thread -> system.out.println(thread.getname()+"->"+thread.isdaemon()+" "+thread.getpriority())); system.out.println("3. gc线程是daemon线程"); } private static volatile int count = 0; private static void four(){ executorservice executorservice = executors.newcachedthreadpool(); for(int j=0; j<10; j++){ executorservice.submit(()->{ for(int i=0; i<1000000; i++){ count++; } }); } system.out.println("count should be: "+10000000+", actual be: "+count); system.out.println("4. volatile不能保证线程安全"); } private static void five(){ arraylist<integer> list = new arraylist<>(20); list.add(1); system.out.println("debug code, not execute grow method"); system.out.println("5. list grow 0 times"); } private static void six() { system.out.println("bufferedreader's constructor only accepts a reader instance"); system.out.println("6. new bufferedreader(new fileinputstream(\"a.dat\")); is wrong"); } private static void seven() { try{ if(true){ throw new ioexception(); } }catch (filenotfoundexception e){ system.out.print("filenotfoundexception!"); }catch (ioexception e){ system.out.print("ioexception!"); }catch (exception e){ system.out.print("exception!"); } system.out.println("\n7. ioexception!"); } private static void eight() { system.out.println("string s;system.out.println(s); error: variable s might not have been initialized\nrecompile with -xlint:unchecked for details."); system.out.println("8. 由于string s没有初始化, 代码不能编译通过"); } private static void nine() { system.out.println("5"+2); system.out.println("9. 52"); } private static void ten() { int i = 2; int result = 0; switch(i){ case 1: result = result + i; case 2: result = result + i * 2; case 3: result = result + i * 3; } system.out.println("result="+result); system.out.println("10. 10"); } private static class null{ public static void hello(){ system.out.println("hello"); } public static void main(string[] args) { ((null)null).hello(); null _null = (null)null; _null.hello(); } } private static class stringexample1{ string str = new string("good"); char[] ch = {'a', 'b', 'c'}; public void change(string str, char[] ch){ str = "test ok"; ch[0] = 'g'; } public static void main(string[] args) { stringexample1 ex = new stringexample1(); ex.change(ex.str, ex.ch); system.out.print(ex.str+" and "); system.out.print(ex.ch); system.out.println(); } } private static class stringexample2{ public static void change(string str){ str = "welcome"; } public static void main(string[] args) { string str = "1234"; change(str); system.out.println(str); } } private static class forloop{ static boolean foo(char c){ system.out.print(c); return true; } public static void main(string[] args) { int i=0; for(foo('a');foo('b')&&(i<2);foo('c')){ i++; foo('d'); } system.out.println(); } } private static class helloa{ public helloa(){ system.out.println("helloa"); } { system.out.println("i'm a class"); } static { system.out.println("static a"); } } private static class hellob extends helloa{ public hellob(){ system.out.println("hellob"); } { system.out.println("i'm b class"); } static { system.out.println("static b"); } public static void main(string[] args) { system.out.println("main start"); new hellob(); new hellob(); system.out.println("main end"); } } public static void main(string[] args) { one(); two(); three(); four(); five(); six(); seven(); eight(); nine(); ten(); null.main(null); stringexample1.main(null); stringexample2.main(null); forloop.main(null); hellob.main(null); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。