[Wrong question sorting] Java basic part

  1. Multiple choice questions (5 points)

    Which of the following methods can form an overload relationship with the method int max(int ​​a, int b, double c) is ( )

    A、double max(int a, int b, double c)

    B、void max(int a, double c, int b)

    C、int max(double a, int b)

    D、int max(int x, int y, double z)

    正确答案BC,涉及知识点:方法的重载

    analyze

    • Method overloading : In the same class, two methods with the same method name but different parameter lists or return value types can form an overloading relationship.
    • Cause of error 1, examination of the question is not serious - the variable names of the parameter list are different, but the data type is the same, and they are also regarded as the same parameter list
    • Error reason 2, conceptual understanding - the return value type of the overloaded relationship can also be different
  2. byte a=23 byte b=12 the result of a+b is byte type

    正确答案:错误。知识点:基本数据类型

    analyze

    • In the operation process of Java, data of byte type cannot be directly added and subtracted , and java will convert byte type data to int type by default, so c is of int type and cannot be directly assigned to byte.
    • If you need to get the result of byte type, you need to cast it again.
  3. Multiple choice questions (3 points)

    Which statement in the following program is correct

    A、byte a=0,b=3; byte c =a+b;

    B、short s =23; s=s+12;

    C、short s=23; s+=12;

    D、float f = 23+23.23;

    正确答案 C,涉及到知识点:基本数据类型

    Why is A wrong?

    • Remember: in JAVA, the numerical calculation with precision less than int will be automatically converted to int for calculation

    The error of B is the same as above, and you need to pay attention

    • Note: s=s+12In fact, short+int, the low and high data type operation, the result will also be converted to the high data type.

    The error of D is the same: the default type of decimal is double, the operation here is int+double, and the result should be of double type

  4. 18. Multiple-choice questions (3 points) and the following code can complete the same option is ( )

    int i=1;
    int sum=0;
    while(i<=100)
    {
          
          
       if(i%2==0)
       {
          
          
          sum=sum+i;
       }
       i++;
    }
    

    A、for (int x=1;x<=100;x++){ sum=sum+x;}

    B、for (int x =0;x<=100;x+=2){ sum=sum+x;}

    C、for (int x =1;x<=100;x+=2){ sum=sum+x;}

    D. All of the above

    正确答案,B,知识点:循环结构

    Analysis: Understand the meaning of the code in the dry -求1~100之间,所有偶数的和

  5. 22. Multiple choice question (3 points) What is the result of the following program execution? ( )

    int x=2,y=3;
    switch(x)
    {
          
          
       default:  
          y++;    
       case 3:
          y++;
       case 4:
          y++;
    }
    Sysetem.out.println("y="+y);
    

    A、3

    B、4

    C、5

    D、6

    答案:D 知识点:switch分支结构的执行顺序

    Analysis: This investigation is about the case penetration phenomenon in switch-case - when a case executes a certain branch, it will be executed all the way down until the break or the end of the code block.

    The order of execution in this question is organized as follows:

    1. Determine the value of x to be 2, and determine the branch to execute—not 3 or 4, that is, take the default route
    2. default executes y++ once
    3. case penetrates to 3 branches: execute y++ once
    4. case penetrates to 4 branches: execute y++ again
    5. End the code block and execute the output statement y=6

Guess you like

Origin blog.csdn.net/Xcong_Zhu/article/details/126361658