试题如下:





参考答案:
1import java.io.FileNotFoundException; 2import java.io.IOException; 3import java.util.ArrayList; 4import java.util.concurrent.ExecutorService; 5import java.util.concurrent.Executors; 6 7/** 8 * Created by ysc on 7/26/16. 9 */ 10public class Interview { 11 private static void one(){ 12 String str1 = "hello"; 13 String str2 = "he"+new String("llo"); 14 System.err.println(str1==str2); 15 System.out.println("1. false"); 16 } 17 private static void two(){ 18 int i = Integer.MAX_VALUE; 19 System.err.println((i+1)<i); 20 System.out.println("2. 存在一个i, 使得(i+1)<i"); 21 } 22 private static void three(){ 23 System.err.println("gc is not a Java Thread, it is a native thread"); 24 Thread.getAllStackTraces().keySet().forEach(thread -> System.out.println(thread.getName()+"->"+thread.isDaemon()+" "+thread.getPriority())); 25 System.out.println("3. gc线程是daemon线程"); 26 } 27 private static volatile int count = 0; 28 private static void four(){ 29 ExecutorService executorService = Executors.newCachedThreadPool(); 30 for(int j=0; j<10; j++){ 31 executorService.submit(()->{ 32 for(int i=0; i<1000000; i++){ 33 count++; 34 } 35 }); 36 } 37 System.out.println("count should be: "+10000000+", actual be: "+count); 38 System.out.println("4. volatile不能保证线程安全"); 39 } 40 private static void five(){ 41 ArrayList<Integer> list = new ArrayList<>(20); 42 list.add(1); 43 System.out.println("debug code, not execute grow method"); 44 System.out.println("5. list grow 0 times"); 45 } 46 private static void six() { 47 System.out.println("BufferedReader's constructor only accepts a Reader instance"); 48 System.out.println("6. new BufferedReader(new FileInputStream(\"a.dat\")); is wrong"); 49 } 50 private static void seven() { 51 try{ 52 if(true){ 53 throw new IOException(); 54 } 55 }catch (FileNotFoundException e){ 56 System.out.print("FileNotFoundException!"); 57 }catch (IOException e){ 58 System.out.print("IOException!"); 59 }catch (Exception e){ 60 System.out.print("Exception!"); 61 } 62 System.out.println("\n7. IOException!"); 63 } 64 private static void eight() { 65 System.out.println("String s;System.out.println(s); error: variable s might not have been initialized\nRecompile with -Xlint:unchecked for details."); 66 System.out.println("8. 由于String s没有初始化, 代码不能编译通过"); 67 } 68 private static void nine() { 69 System.out.println("5"+2); 70 System.out.println("9. 52"); 71 } 72 private static void ten() { 73 int i = 2; 74 int result = 0; 75 switch(i){ 76 case 1: 77 result = result + i; 78 case 2: 79 result = result + i * 2; 80 case 3: 81 result = result + i * 3; 82 } 83 System.out.println("result="+result); 84 System.out.println("10. 10"); 85 } 86 private static class Null{ 87 public static void hello(){ 88 System.out.println("hello"); 89 } 90 public static void main(String[] args) { 91 ((Null)null).hello(); 92 Null _null = (Null)null; 93 _null.hello(); 94 } 95 } 96 private static class StringExample1{ 97 String str = new String("good"); 98 char[] ch = {'a', 'b', 'c'}; 99 public void change(String str, char[] ch){ 100 str = "test ok"; 101 ch[0] = 'g'; 102 } 103 104 public static void main(String[] args) { 105 StringExample1 ex = new StringExample1(); 106 ex.change(ex.str, ex.ch); 107 System.out.print(ex.str+" and "); 108 System.out.print(ex.ch); 109 System.out.println(); 110 } 111 } 112 private static class StringExample2{ 113 public static void change(String str){ 114 str = "welcome"; 115 } 116 117 public static void main(String[] args) { 118 String str = "1234"; 119 change(str); 120 System.out.println(str); 121 } 122 } 123 private static class ForLoop{ 124 static boolean foo(char c){ 125 System.out.print(c); 126 return true; 127 } 128 129 public static void main(String[] args) { 130 int i=0; 131 for(foo('A');foo('B')&&(i<2);foo('C')){ 132 i++; 133 foo('D'); 134 } 135 System.out.println(); 136 } 137 } 138 private static class HelloA{ 139 public HelloA(){ 140 System.out.println("HelloA"); 141 } 142 143 { System.out.println("I'm A class"); } 144 145 static { 146 System.out.println("static A"); 147 } 148 } 149 private static class HelloB extends HelloA{ 150 public HelloB(){ 151 System.out.println("HelloB"); 152 } 153 154 { System.out.println("I'm B class"); } 155 156 static { 157 System.out.println("static B"); 158 } 159 160 public static void main(String[] args) { 161 System.out.println("main start"); 162 new HelloB(); 163 new HelloB(); 164 System.out.println("main end"); 165 } 166 } 167 public static void main(String[] args) { 168 one(); 169 two(); 170 three(); 171 four(); 172 five(); 173 six(); 174 seven(); 175 eight(); 176 nine(); 177 ten(); 178 Null.main(null); 179 StringExample1.main(null); 180 StringExample2.main(null); 181 ForLoop.main(null); 182 HelloB.main(null); 183 } 184}
