Five Questions of Daily Java Written Exam-2020-9-18

Five Questions of Daily Java Written Exam-2020-9-18

  1. Can local variables have the same name as member variables?

Correct answer: A Your answer: A (correct)

可以,局部变量可以与成员变量重名,这时可用“this”来指向成员变量
可以,这时可用“local”关键字来指向局部变量
不能,局部变量不能与成员变量重名
不能,在一个类中不能有重名变量,不管是成员变量还是函数中的局部变量
  1. Regarding abstract classes and final classes, what are the following statements wrong?

    Correct answer: D Your answer: C (wrong)

抽象类能被继承,最终类只能被实例化。
抽象类和最终类都可以被声明使用
抽象类中可以没有抽象方法,最终类中可以没有最终方法
抽象类和最终类被继承时,方法可以被子类覆盖

Analysis:

1. An abstract class can have abstract methods or no abstract methods.

2. The abstract class can of course be inherited, because it is used for inheritance,

3. Inherit the abstract class. If there is an abstract method, the subclass must implement its abstract method.

4. Non-abstract methods in abstract classes can be overridden.

The final class is the opposite of the abstract class

5. The class with final is called final class, and the method with final is called final method.

6. There may or may not be a final method in the final class

7. The final class cannot have subclasses, and the final method cannot be overridden

  1. Which of the following descriptions about servlet service is wrong?

​ Correct answer: B Your answer: B (correct)

不管是post还是get方法提交过来的连接,都会在service中处理
doGet/doPost 则是在 javax.servlet.GenericServlet 中实现的
service()是在javax.servlet.Servlet接口中定义的
service判断请求类型,决定是调用doGet还是doPost方法

Analysis:

B

doget/dopost is related to the Http protocol and is implemented in javax.servlet.http.HttpServlet

img

  1. Explain the output result.

    package test; 
    import java.util.Date;  
    public class SuperTest extends Date{
          
            
        private static final long serialVersionUID = 1L;  
        private void test(){
          
            
           System.out.println(super.getClass().getName());  
        }  
         
        public static void main(String[]args){
          
            
           new SuperTest().test();  
        }  
    }  
    

    ​ Correct answer: C Your answer: A (wrong)

    SuperTest
    SuperTest.class
    test.SuperTest
    test.SuperTest.class
    

Analysis:

1. First, super.getClass() is the getClass() method of the parent class, whose parent class is Date, and its getClass() method is inherited from the Object class and is not overridden.

So just call the getClass() method of object. And look at the method explanation of getclass as shown below ** img**

So you can know that it returns the current runtime class.

2. When calling the getName() method and getName() is: package name + class name

  1. Which of the following methods are for loop optimization

    ​ Correct answer: ABD Your answer: D (wrong)

    强度削弱
    删除归纳变量
    删除多余运算
    代码外提
    

Analysis:

Common code optimization techniques include: copy propagation, delete dead code, weaken strength, delete inductive variables

(The following copy a few pages of PPT and other people's blogs, QAQ, the key is that the principles of compilation have not been studied carefully, our family is not very good at it...)

If any student has studied this part of the content, summed it up, and has a better answer, please contact me to delete this non-original answer

Copy propagation:

illustrate

  • Duplicate statement: assignment of the form f = g
    • A lot of replication will be introduced in the optimization process
    • The practice of copy propagation transformation is to use g to represent f as much as possible after copying the sentence f = g
    • Copy propagation transformation itself is not an optimization, but it brings opportunities for other optimizations
      • Constant merge (calculation that can be done at compile time)
      • Dead code removal

Dead code removal

  • Dead code refers to the statement that the result of the calculation is never quoted
  • Some optimization transformations may cause dead code

Code mention

  • Code outsourcing is a kind of loop optimization
  • Other important techniques for cycle optimization
    • Inductive variable deletion
    • Weakened

Example:

while``(i <= limit - ``2``) ...``// 代码外提后变成``t = limit - ``2``;``while``(i <= t) ...

Inductive variable deletion

j = j - ``1``t4 = ``4` `* j``t5 = a[t4]``if` `t5 > value ``goto` `B3
  • The values ​​of j and t4 change uniformly, and such variables are called inductive variables
  • When there are multiple induction variables in the loop, maybe only one needs to be left
  • This operation is completed by the inductive variable deletion process
  • For this example, the strength can be weakened first, which creates an opportunity to delete the inductive variable

Weakened

  • The essence of strength weakening is to convert high-strength operations into low-strength operations, such as changing multiplication into addition.

Five Questions of Daily Java Written Exam-2020-9-18

  1. Can local variables have the same name as member variables?

Correct answer: A Your answer: A (correct)

可以,局部变量可以与成员变量重名,这时可用“this”来指向成员变量
可以,这时可用“local”关键字来指向局部变量
不能,局部变量不能与成员变量重名
不能,在一个类中不能有重名变量,不管是成员变量还是函数中的局部变量
  1. Regarding abstract classes and final classes, what are the following statements wrong?

    Correct answer: D Your answer: C (wrong)

抽象类能被继承,最终类只能被实例化。
抽象类和最终类都可以被声明使用
抽象类中可以没有抽象方法,最终类中可以没有最终方法
抽象类和最终类被继承时,方法可以被子类覆盖

Analysis:

1. An abstract class can have abstract methods or no abstract methods.

2. The abstract class can of course be inherited, because it is used for inheritance,

3. Inherit the abstract class. If there is an abstract method, the subclass must implement its abstract method.

4. Non-abstract methods in abstract classes can be overridden.

The final class is the opposite of the abstract class

5. The class with final is called final class, and the method with final is called final method.

6. There may or may not be a final method in the final class

7. The final class cannot have subclasses, and the final method cannot be overridden

  1. Which of the following descriptions about servlet service is wrong?

​ Correct answer: B Your answer: B (correct)

不管是post还是get方法提交过来的连接,都会在service中处理
doGet/doPost 则是在 javax.servlet.GenericServlet 中实现的
service()是在javax.servlet.Servlet接口中定义的
service判断请求类型,决定是调用doGet还是doPost方法

Analysis:

B

doget/dopost is related to the Http protocol and is implemented in javax.servlet.http.HttpServlet

img

  1. Explain the output result.

    package test; 
    import java.util.Date;  
    public class SuperTest extends Date{
          
            
        private static final long serialVersionUID = 1L;  
        private void test(){
          
            
           System.out.println(super.getClass().getName());  
        }  
         
        public static void main(String[]args){
          
            
           new SuperTest().test();  
        }  
    }  
    

    ​ Correct answer: C Your answer: A (wrong)

    SuperTest
    SuperTest.class
    test.SuperTest
    test.SuperTest.class
    

Analysis:

1. First, super.getClass() is the getClass() method of the parent class, whose parent class is Date, and its getClass() method is inherited from the Object class and is not overridden.

So just call the getClass() method of object. And look at the method explanation of getclass as shown below ** img**

So you can know that it returns the current runtime class.

2. When calling the getName() method and getName() is: package name + class name

  1. Which of the following methods are for loop optimization

    ​ Correct answer: ABD Your answer: D (wrong)

    强度削弱
    删除归纳变量
    删除多余运算
    代码外提
    

Analysis:

Common code optimization techniques include: copy propagation, delete dead code, weaken strength, delete inductive variables

(The following copy a few pages of PPT and other people's blogs, QAQ, the key is that the principles of compilation have not been studied carefully, our family is not very good at it...)

If any student has studied this part of the content, summed it up, and has a better answer, please contact me to delete this non-original answer

Copy propagation:

illustrate

  • Duplicate statement: assignment of the form f = g
    • A lot of replication will be introduced in the optimization process
    • The practice of copy propagation transformation is to use g to represent f as much as possible after copying the sentence f = g
    • Copy propagation transformation itself is not an optimization, but it brings opportunities for other optimizations
      • Constant merge (calculation that can be done at compile time)
      • Dead code removal

Dead code removal

  • Dead code refers to the statement that the result of the calculation is never quoted
  • Some optimization transformations may cause dead code

Code mention

  • Code outsourcing is a kind of loop optimization
  • Other important techniques for cycle optimization
    • Inductive variable deletion
    • Weakened

Example:

while``(i <= limit - ``2``) ...``// 代码外提后变成``t = limit - ``2``;``while``(i <= t) ...

Inductive variable deletion

j = j - ``1``t4 = ``4` `* j``t5 = a[t4]``if` `t5 > value ``goto` `B3
  • The values ​​of j and t4 change uniformly, and such variables are called inductive variables
  • When there are multiple induction variables in the loop, maybe only one needs to be left
  • This operation is completed by the inductive variable deletion process
  • For this example, the strength can be weakened first, which creates an opportunity to delete the inductive variable

Weakened

  • The essence of strength weakening is to convert high-strength operations into low-strength operations, such as changing multiplication into addition.

Guess you like

Origin blog.csdn.net/qq_37924905/article/details/108666899