首頁  >  文章  >  Java  >  分享一篇Java入門題

分享一篇Java入門題

PHP中文网
PHP中文网原創
2017-06-21 09:37:461483瀏覽

描述

前幾天在知乎裡看到一份這樣的題,當時只是隨便做了一下,對了一下答案。昨天又有了進階的題,裡面有些還是需要記錄一下,於是從這個入門的題目開始。
題目與答案來自阿里雲大學- 知乎專欄

題目

  1. #現在假設有以下程式

    class Happy {public static void main(String args[])     {int i = 1 ;    int j = i++ ;if((i==(++j))&&((i++)==j))     {
                i += j ;
            }
            System.out.println("i = "+i);
        }
    }

    運行完上面程式碼之後輸出i的值是多少?

    A. 4

    B. 5

    C. 3

    D. 6

  2. ##下面的數據聲明及賦值哪一個是沒有錯誤的?

    A.

    float f = 1.3;

    B.

    char c = "a"

    C.

    byte b = 257

    D.

    int i = 10

  3. 編譯Java原始程式檔案產生的字節碼檔案的擴展名為?

    A. java

    B. class

    C. html

    D. exe

  4. ##現在假設有以下程式:
  5. public class Demo {public static void main(String args[]) {boolean flag = 10%2 == 1 && 10 / 3 == 0 && 1 / 0 == 0 ;
            System.out.println(flag ? "aliyunedu" : "yootk") ;
        }
    }

    以上程式的最終執行結果是什麼?

    A. aliyunedu

    B. yootk

    #C. true

    D. 程式出錯

  6. 現在假設有以下程式:
  7. public class Demo {public static void main(String args[]) {int x = 10 ;double y = 20.2 ;long z = 10L;
            String str = "" + x + y * z ;
            System.out.println(str) ;
        }
    }

    以上程式的最終執行結果是什麼?

    A. 10202.0

    B. 0212.0

    C. 302.0

    D. 1020.210

  8. 現在假設有以下程式:
  9. public class Demo {public static void main(String args[]) {
            String str = "" ;for (int x = 0 ; x < 5 ; x ++) {
                str += x ;
            }
            System.out.println(str) ;
        }
    }

    以上程式最終的執行結果是什麼?

    A. 01234

    B. 10

    #C. 14

    D. 25

  10. 現在假設有以下程式:
  11. public class Demo {public static void main(String args[]) {
            System.out.println(inc(10) + inc(8) + inc(-10)) ;
        }public static int inc(int temp) {if (temp > 0) {return temp * 2 ;
            }return -1 ;
        }
    }

    以上程式的最終執行結果是什麼?

    A. 35

    B. 8

    C. 28

    D. 12

  12. 現在假設有以下程式:
  13. public class Demo {public static void main(String args[]) {char c = &#39;A&#39; ;int num = 10 ;switch(c) {case &#39;B&#39; :
                    num ++ ;case &#39;A&#39; :
                    num ++ ;case &#39;Y&#39; :
                    num ++ ;break ;default :
                    num -- ;
            }
            System.out.println(num) ;
        }
    }

    以上程式的最終執行結果是什麼?

    A. 11

    B. 13

    C. 12

    D. 10

    ##現在假設有以下程式:
  14. public class Demo {public static void main(String args[]) {int sum = 0 ;for (int x = 1 ; x < 10 ; x ++) {
                sum += x ;if (x % 3 == 0) {continue ;
                }
            }
            System.out.println(sum) ;
        }
    }

    以上程式的最終執行結果是什麼?
    A. 6

    B. 0

    C. 程式錯誤,死迴圈

    D. 45

    現在假設有以下程式:
  15. public class Demo {public static void main(String args[]) {int sum = 0 ;for (int x = 0 ; x < 10 ; x ++) {
                sum += x ;if (x % 3 == 0) {break ;
                }
            }
            System.out.println(sum) ;
        }
    }

    以上程式的最終執行結果是什麼?
    A. 6

    B. 0

    C. 程式錯誤,死迴圈

    D. 45

    答案
BDBBA AACDB

個人解析

#主要測試
    i++
  1. ++i的區別,只要記住“先++,先自增;後++,後自增”,這題就只剩下考驗細心了。 <pre class="sourceCode java">class Happy {public static void main(String[] args) {int i = 1;int j = i++; // i = 2, j = 1if ((i == (++j)) &amp;&amp; ((i++) == j)) { // 第一个判断:j先自增1变为2后与i比较// 第二个判断:i先与j比较后再自增1,// if内为true,i = 3, j = 2i += j; // i = 5, j = 2} System.out.println(&quot;i = &quot; + i); } }</pre>

    如果選項A最後沒有那個
  2. ;
  3. ,那麼這題就沒有爭議了

    int b = 257;
  • #byte b = 57;
    char c = 'a';
  • #String c = "a";
    #float f = 1.3f;
  • double f = 1.3;
  • float f =(float) 1.3;
  • double f = 1.3f;
    A.
  • float f = 1.3;

    1.3預設是double類型,java中基本資料型別由高階向低階轉換需要強轉。

  • B.
  • char c = "a"

    #java中的字元常數應該用單引號括起來,雙引號括起來的為字串。 (最後少了個分號)

  • C.
  • byte b = 257

    byte的範圍是 -128~127。 (最後少了個分號)

  • D.
  • int i = 10

    (最後少了個分號)

  • public class Demo {public static void main(String args[]) {boolean flag = 10 % 2 == 1 && 10 / 3 == 0 && 1 / 0 == 0 ;// 10对2取余为0,故flag为falseSystem.out.println(flag ? "aliyunedu" : "yootk") ;
        }
    }
  • &&
    (短路與)一旦前面的條件為false,就會跳過後面的條件。

    X = 條件? A : B為三元表達式,與
    <pre class="sourceCode java">if (条件) { X = A; } else { X = B; }</pre>

    #意思相同

    public class Demo {public static void main(String args[]) {int x = 10 ;double y = 20.2 ;long z = 10L;
            String str = "" + x + y * z ;
            System.out.println(str) ;
        }
    }
  • *
    的優先權高於

    +,故優先計算乘法,接著由左往右依序進行+。當有字串參與+運算時,加法變成字串拼接,結果為字串。故最後為字串"10"202.0的拼接。

    見上
  • public class Demo {public static void main(String args[]) {
            System.out.println(inc(10) + inc(8) + inc(-10)) ; // 20 + 16 - 1}public static int inc(int temp) {if (temp > 0) {return temp * 2 ;
            }return -1 ;
        }
    }
  • 如果為正數,則傳回參數的2倍值;如果不是正數,返回-1。結果為
    20 + 16 + (-1)

    public class Demo {public static void main(String args[]) {char c = &#39;A&#39; ;int num = 10 ;switch(c) {case &#39;B&#39; :
                    num ++ ;case &#39;A&#39; :// 匹配成功,开始执行num ++ ; // num = 11case &#39;Y&#39; :
                    num ++ ; // num = 12break ;// 因break跳出switchdefault :
                    num -- ;
            }
            System.out.println(num) ;
        }
    }
  • switch-case語句區塊中break的重要性

    #
    public class Demo {public static void main(String args[]) {int sum = 0 ;for (int x = 1 ; x < 10 ; x ++) {
                sum += x ;if (x % 3 == 0) {continue ;
                }
            }
            System.out.println(sum) ;
        }
    }
  • 感覺這題
    sum += x
    的位置可能寫錯了,應該在if的後面,要嘛就只是單純的和下一道題作對比。現在這段程式碼裡if的用處幾乎沒有,結果和沒有if時是一樣的,都是1+2+…+9=45。

    public class Demo {public static void main(String args[]) {int sum = 0 ;for (int x = 0 ; x < 10 ; x ++) {
                sum += x ;if (x % 3 == 0) {break ;
                }
            }
            System.out.println(sum) ;
        }
    }
  • 和上一題類似,不過i的初始值變成了0,if裡面的continue變成了break。由於0對3取餘為0,所以直接跳出循環,輸出sum的值0。

    #

    以上是分享一篇Java入門題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述:
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn