15道非常經(jīng)典的Java面試題 附詳細(xì)答案
試題如下:
參考答案:
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. 存在一個(gè)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沒(méi)有初始化, 代碼不能編譯通過(guò)"); } 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); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 15個(gè)高級(jí)Java多線程面試題及回答
- 探討Java中最常見(jiàn)的十道面試題(超經(jīng)典)
- JAVA實(shí)現(xiàn)鏈表面試題
- 最有價(jià)值的50道java面試題 適用于準(zhǔn)入職Java程序員
- Java實(shí)現(xiàn)棧和隊(duì)列面試題
- 關(guān)于Java集合框架面試題(含答案)下
- java 多態(tài)性詳解及常見(jiàn)面試題
- 10個(gè)經(jīng)典的Java main方法面試題
- 適用于Java程序員的10道XML面試題 新鮮出爐
- 網(wǎng)易Java程序員兩輪面試 請(qǐng)問(wèn)你能答對(duì)幾個(gè)?
相關(guān)文章
Java策略模式的簡(jiǎn)單應(yīng)用實(shí)現(xiàn)方法
這篇文章主要介紹了Java策略模式的簡(jiǎn)單應(yīng)用實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02SpringBoot 實(shí)戰(zhàn) 之 優(yōu)雅終止服務(wù)的方法
本篇文章主要介紹了SpringBoot 實(shí)戰(zhàn) 之 優(yōu)雅終止服務(wù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Java 十進(jìn)制轉(zhuǎn)二、八、十六進(jìn)制的字符串
本文主要介紹了十進(jìn)制轉(zhuǎn)二進(jìn)制;十進(jìn)制轉(zhuǎn)八進(jìn)制;十進(jìn)制轉(zhuǎn)十六進(jìn)制的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02Java和scala實(shí)現(xiàn) Spark RDD轉(zhuǎn)換成DataFrame的兩種方法小結(jié)
今天小編就為大家分享一篇Java和scala實(shí)現(xiàn) Spark RDD轉(zhuǎn)換成DataFrame的兩種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06深入理解 Java、Kotlin、Go 的線程和協(xié)程
這篇文章主要介紹了深入理解 Java、Kotlin、Go 的線程和協(xié)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12使用Runtime 調(diào)用Process.waitfor導(dǎo)致的阻塞問(wèn)題
這篇文章主要介紹了使用Runtime 調(diào)用Process.waitfor導(dǎo)致的阻塞問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12MongoDB中ObjectId的誤區(qū)及引起的一系列問(wèn)題
這篇文章主要介紹了MongoDB中ObjectId的誤區(qū)及引起的一系列問(wèn)題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12Java實(shí)現(xiàn)短信驗(yàn)證碼和國(guó)際短信群發(fā)功能的示例
本篇文章主要介紹了Java實(shí)現(xiàn)短信驗(yàn)證碼和國(guó)際短信群發(fā)功能的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02Nacos集群模式下服務(wù)無(wú)法注冊(cè)問(wèn)題
這篇文章主要介紹了Nacos集群模式下服務(wù)無(wú)法注冊(cè)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01rabbitmq結(jié)合spring實(shí)現(xiàn)消息隊(duì)列優(yōu)先級(jí)的方法
本篇文章主要介紹了rabbitmq結(jié)合spring實(shí)現(xiàn)消息隊(duì)列優(yōu)先級(jí)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02