筆試題 Java 易錯題精選
一、寫出下列程序的運行結果( )String 不變性Java 值傳遞java
public class Test { public static void main(String[] args) { String a = "hello"; change(a); System.out.println(a); } public static void change(String name) { name = "world"; } }
運行結果
緩存
hello
二、寫出下列程序的運行結果( )String 不變性Java 值傳遞多線程
public class Test { public void change(String str, char ch[]) { str = "test ok"; ch[0] = 'g'; } public static void main(String args[]) { String str = new String("good"); char[] ch = {'a', 'b', 'c'}; Test ex = new Test(); ex.change(str, ch); System.out.print(str + " and "); System.out.print(ch); } }
運行結果
app
good and gbc
三、寫出下列程序的運行結果( )Integer 類緩存== 和 equalsui
public class Test { public static void main(String[] args) { Integer a = 1; Integer b = 2; Integer c = 3; Integer d = 3; Integer e = 321; Integer f = 321; Long g = 3L; Long h = 2L; System.out.println(c == d); System.out.println(e == f); System.out.println(c == (a + b)); System.out.println(c.equals(a + b)); System.out.println(g == (a + b)); System.out.println(g.equals(a + b)); System.out.println(g.equals(a + h)); } }
運行結果
this
true false true true true false true
四、寫出下列程序的運行結果( )String 不變性Java 值傳遞spa
public class Test { public static void main(String[] args) { Test test = new Test(); int a = 1; test.addInt(a); System.out.println(a); String str = "hello"; test.addString(str); System.out.println(str); StringBuilder sb = new StringBuilder("hello"); test.addBuilder(sb); System.out.println(sb.toString()); Long m = 1L; Long n = 1L; System.out.println(m == n); m = 200L; n = 200L; System.out.println(m == n); String x = new String("hello"); String y = "hello"; System.out.println(x.equals(y)); System.out.println(x == y); } public void addInt(int a) { a = a + 1; } public void addString(String str) { str = str + "world"; } public void addBuilder(StringBuilder sb) { sb.append("world"); } }
運行結果
.net
1 hello helloworld true false true false
五、寫出下列程序的運行結果( )intern() 方法== 和 equals線程
public class Test { public static void main(String[] args) { String s1 = "Monday"; String s2 = new String("Monday"); s2 = s2.intern(); if (s1 == s2) { System.out.println("s1 == s2"); } else { System.out.println("s1 != s2"); } if (s1.equals(s2)) { System.out.println("s1 equals s2"); } else { System.out.println("s1 not equals s2"); } } }
運行結果
指針
s1 == s2 s1 equals s2
解析
講解:java.lang.String 的 intern() 方法"abc".intern() 方法的返回值仍是字符串"abc",表面上看起來好像這個方法沒什麼用處。但實際上,它作了個小動做:檢查字符串池裏是否存在"abc"這麼一個字符串,若是存在,就返回池裏的字符串;若是不存在,該方法會把"abc"添加到字符串池中,而後再返回它的引用。
六、寫出下列程序的運行結果( )多線程 run() 和 start() 方法
public class Test { public static void main(String[] args) { Thread t = new Thread() { public void run() { pong(); } }; t.run(); System.out.print("ping"); } private static void pong() { System.out.print("pong"); } }
運行結果
pongping
七、寫出下列程序的運行結果( )switch
public class Test { public static void main(String[] args) { System.out.println(getValue(2)); } public static int getValue(int i) { int Test = 0; switch (i) { default: System.out.println("default"); case 1: Test = Test + i; case 2: Test = Test + i * 2; case 3: Test = Test + i * 3; } return Test; } }
運行結果
10
八、寫出下列程序的運行結果( )變量做用域
public class MeaningOfThis { public final int value = 4; public void doIt() { int value = 6; Runnable r = new Runnable() { public final int value = 5; public void run() { int value = 10; System.out.println(this.value); } }; r.run(); } public static void main(String... args) { MeaningOfThis m = new MeaningOfThis(); m.doIt(); } }
運行結果
5
解析
結果爲 5,由於 this 指的是包含它的 Runnable,而不是外面的類 MeaningOfThis。
九、對文件名爲 Test.java 的 Java 代碼描述正確的是( )String 不變性Java 值傳遞構造方法
class Person { String name = "No name"; public Person(String nm) { name = nm; } } class Employee extends Person { String empID = "0000"; public Employee(String id) { empID = id; } } public class Test { public static void main(String args[]) { Employee e = new Employee("123"); System.out.println(e.empID); } }
A. 輸出: 0000
B. 輸出: 123
C. 編譯報錯
D. 輸出: No name
答案
C
解析
class Person { String name = "No name"; public Person(String nm) { name = nm; } } class Employee extends Person { String empID = "0000"; public Employee(String id) { super("Railway Employee"); empID = id; } } public class Test { public static void main(String[] args) { Employee employee = new Employee("123"); System.out.println(employee.empID); System.out.println(employee.name); } }
運行結果
123 Railway Employee
方案二
class Person { String name = "No name"; public Person(String nm) { name = nm; } public Person() { } } class Employee extends Person { String empID = "0000"; public Employee(String id) { empID = id; } } public class Test { public static void main(String args[]) { Employee e = new Employee("123"); System.out.println(e.empID); } }
運行結果
123
十、寫出下列程序的運行結果( )空指針異常
public class NULL { private static void hahn() { System.out.println("hahn"); } public static void main(String[] args) { NULL.hahn(); } }
運行結果
hahn
十一、寫出下列程序的運行結果( )溢出Java 類型轉換
public class Test { public static void main(String[] args) { int a = Integer.MAX_VALUE; long b = a + 1; System.out.println(a); System.out.println(b); } }
運行結果
2147483647 -2147483648
解決方案
public class Test { public static void main(String[] args) { int a = Integer.MAX_VALUE; long b = (long) a + 1; System.out.println(a); System.out.println(b); } }
運行結果
2147483647 2147483648
十二、寫出下列程序的運行結果( )super 關鍵字final 關鍵字
import java.util.Date; public class Test extends Date { public static void main(String[] args) { new Test().test(); } private void test() { System.out.println(super.getClass().getName()); System.out.println(getClass().getName()); System.out.println(this.getClass().getName()); System.out.println(super.getClass().getSuperclass().getName()); System.out.println(getClass().getSuperclass().getName()); System.out.println(this.getClass().getSuperclass().getName()); } }
運行結果
Test Test Test java.util.Date java.util.Date java.util.Date
解析
爲何 super 沒有起做用呢?簡單來講,super 並不能表明一個超類的引用。
由於 super 並無表明超類的一個引用的能力,只是表明調用父類的方法而已。因此,在子類的方法中,不能這樣用 System.out.println(super);也不能使用 super.super.mathod();
參考資料