作各種猥瑣的Java筆試/面試題目__Java

來源:互聯網
上載者:User

 原題 http://blog.csdn.net/smcwwh/article/details/7315041,自己做的一些心得體會

 1.

 public static void main(String[] args) {          int k = 0;          int ret = ++k + k++ + ++k + k;          // ret的值為多少           System.err.println(ret);  

1+1+3+3=8

2.

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

i1 + i2 = 1010(String)

異常(好猥瑣 String-in t)
i1 * i2 = 100
i1 / i2 = 1

3.

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

String (子類優先匹配的原則)

4.

Date date = new Date();          System.err.println(date.getMonth() + " " + date.getDate());  

8 8 

getMonth返回的是0-11

5.

 double val = 11.5;          System.err.println(Math.round(val));          System.err.println(Math.floor(val));          System.err.println(Math.ceil(val));  

12 

11.0

12.0

Math.round返回最接近參數的 long

Math.floor返回最大的(最接近正無窮大)double 值,該值小於等於參數,並等於某個整數。

Math.ceil 返回最小的(最接近負無窮大)double 值,該值大於等於參數,並等於某個整數。

6.編程輸出一個目錄下的所有目錄及檔案名稱,目錄之間用tab

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


7.從鍵盤讀入10個整數,然後從大到小輸出

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


8.

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


test

test

9.

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

Test

Base

在test方法中,直接調用getClass().getName()方法,返回的是Test類名
由於getClass()在Object類中定義成了final,子類不能覆蓋該方法,所以,在
test方法中調用getClass().getName()方法,其實就是在調用從父類繼承的getClass()方法,等效於調用super.getClass().getName()方法,所以,super.getClass().getName()方法返回的也應該是Test。
如果想得到父類的名稱,應該用如下代碼:
getClass().getSuperClass().getName();
————————————————————
以上是題目和分析,我也做了測試,結果確實是Test,但是我看不明白下面的分析,我覺得他說的模稜兩可,沒有說明白實質問題。
為此,我查閱了Object的getClass()方法的API,解釋是:返回此 Object 的運行時類。返回的 Class 對象是由所表示類的 static synchronized 方法鎖定的對象。
看了這個,我很清楚this.getClass()是返回運行時對象的類型,但是super.getClass()為何返回的還是子類的類型,還是不明白。
後來在網上查了super的含義,有一個文章說super不是超類的引用,而是表示在子類中調用父類的方法或屬性而已,並且給出例子
class B extends A{
  public void print(){
  System.out.println(super.getClass());//調用A類中的getclass()方法,A是Object的子類,A中的getClass()是Object中的,運行時期的執行個體是B類,所以輸出的依然是Class B
  System.out.println(this.getClass()); //調用B類中的getclass()方法,此方法從A繼承的,A從Object繼承的,運行時期的執行個體是B類,所以輸出的是Class B

 
}
我認為後面的解釋更加說的通一些,但是我不確定解釋是不是正確,所以,發到版上來,請版上的高手幫忙看一下,給出合理的解釋。因為這道筆試題的所謂答案也已經被轉載了很多次了,如果解釋真是不正確了,那就蒙蔽了很多人了。。。

原文連結http://blog.sina.com.cn/s/blog_60cad3500102dx11.html

10.

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

true

false

String類提供了一些方法,用來進行字串的比較。這個類實現了Object父類的equals()方法,用來比較兩種字串的值是否相等。

但是StringBuffer類並沒有實現Objcet類的Equals方法,所以不能用這個方法來比較兩個StringBuffer類的字串是否相等

詳細情況串連http://www.west263.com/info/html/chengxusheji/Javajishu/20080403/57254.html

11.

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

1

2

 finally 塊必須與 try 或 try/catch 塊配合使用。此外,不可能退出 try 塊而不執行其 finally 塊。如果 finally 塊存在,則它總會執行。(無論從那點看,這個陳述都是正確的。有一種方法可以退出 try 塊而不執行 finally 塊。如果代碼在 try 內部執行一條 System.exit(0); 語句,則應用程式終止而不會執行 finally 執行。另一方面,如果您在 try 塊執行期間撥掉電源,finally 也不會執行。)

finally 結構使代碼總會執行,而不管有無異常發生其次try, catch, finally中fianlly的throw/return的層級最高
12.

 synchronized  與static synchronized  的區別

:A: synchronized static是某個類的範圍,synchronized static cSync{}防止多個線程同時訪問這個    類中的synchronized static 方法。它可以對類的所有對象執行個體起作用。
  
               B: synchronized 是某執行個體的範圍,synchronized isSync(){}防止多個線程同時訪問這個執行個體中的synchronized 方法。



     2.synchronized方法與synchronized代碼快的區別
      synchronized methods(){} 與synchronized(this){}之間沒有什麼區別,只是synchronized methods(){} 便於閱讀理解,而synchronized(this){}可以更精確的控制衝突限制訪問地區,有時候表現更高效率。


     3.synchronized關鍵字是不能繼承的
     這個在http://www.learndiary.com/archives/diaries/2910.htm一文中看到的,我想這一點也是很值得注意的,繼承時子類的覆蓋方法必須顯示定義成synchronized

原文http://www.cnblogs.com/shipengzhi/articles/2223100.html

13.

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

true

false

對於整數,Integer類中設定了緩衝,儲存了(-128)~127, 所以當建立小於128的整數對象時不是每次都分配一個新對象,而是先判斷的*
14.

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

true
false
true

intern()返回字串對象的正常化表示形式。

15. 

 System.err.println(12 - 11.9 == 0.1);  

false

0.5在電腦中可以精確表示,而0.1無法精確用二進位表示.同理0.75等計算起來應該都是對的
深入http://blog.csdn.net/smcwwh/article/details/7080878

16.

 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());  

0

最後輸出的還是sum,這破名起的

BigInteger c= sum.add(three);          System.out.println(c);  

這樣輸出c才是求得和

17.

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

無序



 


 






 

聯繫我們

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