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

來源:互聯網
上載者:User

本文收錄各種猥瑣的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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.