收錄各種猥瑣的Java筆試/面試題目

來源:互聯網
上載者:User

本文收錄各種猥瑣的Java筆試/面試題,一些比較容易忘記的,不定期更新。也希望大家在底下留言,貼出自己碰到或看到的各種猥瑣筆試、面試題目。

J2EE基礎部分

1、運算子優先順序問題,下面代碼的結果是多少?(筆試)

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、運算子問題,下面代碼分別輸出什嗎?(筆試)

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、下面代碼的結果是什嗎?還是拋出異常?(筆試)

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日,下面代碼輸出什嗎?(筆試)

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、下面代碼的輸出結果是什嗎?

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。(筆試)

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個整數,然後從大到小輸出。(筆試)

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>() {@Overridepublic 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、下面代碼的結果是什嗎?

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();}@Overridepublic void method() {System.err.println("test");}}class Base {public void method() throws InterruptedException {System.err.println("base");}}

9、以下代碼的結果是什嗎?

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?

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、輸出的結果是什嗎?

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;}}}

這樣呢?輸出什麼

package test;public class Test {public static void main(String[] args) {System.err.println(method());}public static boolean method() {      try {         return true;     } finally {       return false;    } }}

12、方法m1和m2有區別嗎?什麼區別

package test;public class Test {public static void main(String[] args) {}public synchronized void m1() {}public static synchronized void m2() {}}

13、true or false?理由

package test;public class Test {public static void main(String[] args) {Integer i1 = 127;Integer i2 = 127;System.err.println(i1 == i2);i1 = 128;i2 = 128;System.err.println(i1 == i2);}}

14、true or false?理由

package test;public class Test {public static void main(String[] args) {String str1 = "a";String str2 = "a";String str3 = new String("a");System.err.println(str1 == str2);System.err.println(str1 == str3);str3 = str3.intern();System.err.println(str1 == str3);}}

15、true or false?理由

package test;public class Test {public static void main(String[] args) {System.err.println(12 - 11.9 == 0.1);}}

16、以下代碼輸出是什嗎?

package test;import java.math.BigInteger;public class Test {public static void main(String[] args) {BigInteger one = new BigInteger("1");BigInteger two = new BigInteger("2");BigInteger three = new BigInteger("3");BigInteger sum = new BigInteger("0");sum.add(one);sum.add(two);sum.add(three);System.out.println(sum.toString());}}

17、輸出的結果是什嗎?12345?根據單詞排序?還是?

package test;import java.util.HashSet;import java.util.Iterator;import java.util.Set;public class Test {public static void main(String[] args) {Set<String> set = new HashSet<String>();set.add("one");set.add("two");set.add("three");set.add("four");set.add("five");for (Iterator<String> it = set.iterator(); it.hasNext();) {System.err.println(it.next());}}}

18、如何迭代Map容器,別所大概是......,手寫個試試?

19、以下代碼輸出的結果(筆試選擇題)

public class Test {public static void main(String[] args) {System.err.println(args.length);}}/*A. nullB. 0C. TestD. Exception in thread "main" java.lang.NullPointerException*/

20、下面為一個單例的實現代碼,請指出代碼中有幾個錯誤或不合理之處,並改正。

public class Test {public Test instance = null;public static Test getInstance() {if (instance == null) {instance = new Test();return instance;}}}

先寫這麼多,會一直更新。

請記住本文地址:http://blog.csdn.net/smcwwh/article/details/7315041

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.