【17-06-16】Java入門測試題,測測你基礎知識掌握程度(附答案及個人解析)


描述

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

題目

  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
  1. 下面的數據聲明及賦值哪一個是沒有錯誤的?

    A. float f = 1.3;

    B. char c = "a"

    C. byte b = 257

    D. int i = 10

  2. 編譯Java源程序文件產生的字節碼文件的擴展名為?

    A. java

    B. class

    C. html

    D. exe

  3. 現在假設有如下程序:

    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. 程序出錯

  4. 現在假設有如下程序:

    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

  5. 現在假設有如下程序:

    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

  6. 現在假設有如下程序:

    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

  7. 現在假設有如下程序:

    public class Demo {
        public static void main(String args[]) {
            char c = 'A' ;
            int num = 10 ;
            switch(c) {
                case 'B' :
                    num ++ ;
                case 'A' :
                    num ++ ;
                case 'Y' :
                    num ++ ;
                    break ;
                default :
                    num -- ;
            }
            System.out.println(num) ;
        }
    }
    

    以上程序的最終執行結果是什么?

    A. 11

    B. 13

    C. 12

    D. 10

  8. 現在假設有如下程序:

    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

  9. 現在假設有如下程序:

    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

個人解析

  1. 主要考驗i++++i的區別,只要記住“先++,先自增;后++,后自增”,這道題就只剩下考驗細心了。

    class Happy {
        public static void main(String[] args) {
            int i = 1;
            int j = i++; // i = 2, j = 1
    
            if ((i == (++j)) && ((i++) == j)) { 
            // 第一個判斷:j先自增1變為2后與i比較
            // 第二個判斷:i先與j比較后再自增1,
            // if內為true,i = 3, j = 2
                i += j; // i = 5, j = 2
            }
    
            System.out.println("i = " + i);
        }
    }
    
  2. 如果選項A最后沒有那個;,那么這道題就沒有爭議了

    • A. float f = 1.3;
      1.3默認是double類型,java中基本數據類型由高級向低級轉換需要強轉。

      • float f = 1.3f;
      • double f = 1.3;
      • float f =(float) 1.3;
      • double f = 1.3f;
    • B. char c = "a"
      java中的字符常量應該用單引號括起來,雙引號括起來的為字符串。(末尾少了個分號)

      • char c = 'a';
      • String c = "a";
    • C. byte b = 257
      byte的范圍是 -128~127。(末尾少了個分號)

      • int b = 257;
      • byte b = 57;
    • D. int i = 10
      (末尾少了個分號)

  3. public class Demo {
        public static void main(String args[]) {
            boolean flag = 10 % 2 == 1 && 10 / 3 == 0 && 1 / 0 == 0 ;
            // 10對2取余為0,故flag為false
            System.out.println(flag ? "aliyunedu" : "yootk") ;
        }
    }
    

    &&(短路與)一旦前面的條件為false,就會跳過后面的條件。
    X = 條件 ? A : B為三元表達式,與

    if (條件) {
        X = A;
    } else {
        X = B;
    }
    

    意思相同

  4. 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的拼接。

  5. 見上

  6. 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)

  7. public class Demo {
        public static void main(String args[]) {
            char c = 'A' ;
            int num = 10 ;
            switch(c) {
                case 'B' :
                    num ++ ;
                case 'A' :
                    // 匹配成功,開始執行
                    num ++ ; // num = 11
                case 'Y' :
                    num ++ ; // num = 12
                    break ;
                    // 因break跳出switch
                default :
                    num -- ;
            }
            System.out.println(num) ;
        }
    }
    

    switch-case語句塊中break的重要性

  8. 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。

  9. 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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM