【17-06-16】Java入门测试题,测测你基础知识掌握程度(附答案及我的解析)

描述

前几天在知乎里看到一份这样的题,当时只是随便作了一下,对了一下答案。昨天又有了一份进阶的题,里面有些仍是须要记录一下,因而就从这个入门的题开始。
题目和答案来自阿里云大学 - 知乎专栏html

题目

  1. 如今假设有以下程序
    java 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的值是多少?java

    A. 4app

    B. 5阿里云

    C. 3code

    D. 6htm

  2. 下面的数据声明及赋值哪个是没有错误的?字符串

    A. float f = 1.3;get

    B. char c = "a"it

    C. byte b = 257编译

    D. int i = 10

  3. 编译Java源程序文件产生的字节码文件的扩展名为?

    A. java

    B. class

    C. html

    D. exe

  4. 如今假设有以下程序:
    java 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. 程序出错

  5. 如今假设有以下程序:
    java 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

  6. 如今假设有以下程序:
    java 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

  7. 如今假设有以下程序:
    java 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

  8. 如今假设有以下程序:
    java 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

  9. 如今假设有以下程序:
    java 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

  10. 如今假设有以下程序:

    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的区别,只要记住“先++,先自增;后++,后自增”,这道题就只剩下考验细心了。
    ```java
    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为三元表达式,与
    java 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。

相关文章
相关标签/搜索