本文收錄各種猥瑣的Java筆試/面試題,一些比較容易忘記的,不定期更新。也希望大家在底下留言,貼出自己碰到或看到的各種猥瑣筆試、面試題目。 J2EE基礎部分
1、運算子優先順序問題,下面代碼的結果是多少。(筆試) [java] view plain copy package test; public class Test { public static void main(String[] args) { int k = 0; int ret = ++k + k++ + ++k + k; // ret的值為多少 System.err.println(ret); } } 2、運算子問題,下面代碼分別輸出什麼。(筆試) [java] view plain copy package test; public class Test { public static void main(String[] args) { int i1 = 10, i2 = 10; System.err.println("i1 + i2 = " + i1 + i2); System.err.println("i1 - i2 = " + i1 - i2); System.err.println("i1 * i2 = " + i1 * i2); System.err.println("i1 / i2 = " + i1 / i2); } } 3、下面代碼的結果是什麼。還是拋出異常。(筆試) [java] view plain copy package test; public class Test { public void myMethod(String str) { System.err.println("string"); } public void myMethod(Object obj) { System.err.println("object"); } public static void main(String[] args) { Test t = new Test(); t.myMethod(null); } } 4、假設今天是9月8日,下面代碼輸出什麼。(筆試) [java] view plain copy package test; import java.util.Date; public class Test { public static void main(String[] args) { Date date = new Date(); System.err.println(date.getMonth() + " " + date.getDate()); } } 5、下面代碼的輸出結果是什麼。 [java] view plain copy package test; public class Test { public static void main(String[] args) { double val = 11.5; System.err.println(Math.round(val)); System.err.println(Math.floor(val)); System.err.println(Math.ceil(val)); } } 6、編程輸出一個目錄下的所有目錄及檔案名稱,目錄之間用tab。(筆試) [java] view plain copy package test; import java.io.File; public class Test { public static void main(String[] args) { new Test().read("D:/test", ""); } public void read(String path, String tab) { File file = new File(path); File[] childFiles = file.listFiles(); for (int i = 0; childFiles != null && i < childFiles.length; i++) { System.err.println(tab + childFiles[i].getName()); if (childFiles[i].isDirectory()) { read(childFiles[i].getPath(), tab + "\t"); } } } } 不要覺得很簡單,最起碼你要記得返回當前檔案夾下的所有檔案的方法是listFiles(),isDirectory別拼錯了。
7、從鍵盤讀入10個整數,然後從大到小輸出。(筆試) [java] view plain copy package test; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意這裡的數組,不是int的 Integer[] arr = new Integer[10]; for (int i = 0; i < 10; i++) { arr[i] = in.nextInt(); } Arrays.sort(arr, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { if (o1 > o2) return -1; if (o1 < o2) return 1; return 0; } }); System.err.println(Arrays.toString(arr)); } } 自己手寫排序演算法的可以無視此題,如果是Arrays.sort()的,請注意Comparator與Comparable介面的區別,別搞混了。
8、下面代碼的結果是什麼。 [java] view plain copy package test; public class Test extends Base { public static void main(String[] args) { Base b = new Test(); b.method(); Test t = new Test(); t.method(); } @Override public void method() { System.err.println("test"); } } class Base { public void method() throws InterruptedException { System.err.println("base"); } } 9、以下代碼的結果是什麼。 [java] view plain copy package test; public class Test extends Base { public static void main(String[] args) { new Test().method(); } public void method() { System.err.println(super.getClass().getName()); System.err.println(this.getClass().getSuperclass().getName()); } } class Base { } 10、true or false。
[java] view plain copy package test; public class Test { public static void main(String[] args) { String str1 = new String("abc"); String str2 = new String("abc"); System.err.println(str1.equals(str2)); StringBuffer sb1 = new StringBuffer("abc"); StringBuffer sb2 = new StringBuffer("abc"); System.err.println(sb1.equals(sb2)); } } 11、輸出的結果是什麼。 [java] view plain copy package test; public class Test { public static void main(String[] args) { System.err.println(new Test().method1()); System.err.println(new Test().method2()); } public int method1() { int x = 1; try { return x; } finally { ++x; } } public int method2() { int x = 1; try { return x; } finally { return ++x; } } } 這樣呢。輸出什麼 [java] view plain copy package test; public class Test { public static void main(String[] args) { System.err.println(method()); &nbs