Five Questions for Daily Java Written Exam 2020-9-23

Five Questions for Daily Java Written Exam 2020-9-23

  1. This represents the object reference of (), and super represents the () object of the current object?

    Correct answer: C Your answer: C (correct)

当前类 当前类
当前类的父类 当前类
当前类  当前类的父类
以上都不正确
  1. After the following code is executed, the output result is ()
public class Test {
    
     
    public static void main(String[] args) {
    
     
        System.out.println("return value of getValue(): " +
        getValue()); 
    } 
     public static int getValue() {
    
     
         try {
    
     
             return 0; 
         } finally {
    
     
             return 1; 
         } 
     } 
 }

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

return value of getValue(): 1
return value of getValue(): 0
return value of getValue(): 0return value of getValue(): 1
return value of getValue(): 1return value of getValue(): 0

Analysis:

According to the official JVM specification:
If there is a return in the try statement, the value of the variable in the try statement block is returned.
The detailed execution process is as follows:

  1. If there is a return value, save the return value in a local variable;
  2. Execute the jsr instruction to jump to the finally statement for execution;
  3. After executing the finally statement, return the value previously saved in the local variable table.

If both try and finally statements have return, ignore the return of try and use the return of finally.

  1. In a Web program, which object should be stored in the current user context information ()

    Correct answer: C Your answer: D (wrong)

page
request
session
Application

Analysis:

JSP four scopes: page(minimum requestscope), session, , application(maximum scope).

  • applicationThe attributes stored in the object can be accessed by all Servlets and JSP pages in the same WEB application . (The attribute has the largest scope)
  • sessionThe attributes stored in the object can be accessed by all Servlet and JSP pages that belong to the same session (the browser is opened until closed is called a session, and the session does not expire during this period).
  • requestThe attributes stored in the object can be accessed by all Servlets and JSP pages belonging to the same request (the attribute value can be obtained across pages in the case of forwarding), for example, multiple Servlets connected using the PageContext.forward and PageContext.include methods And JSP pages.
  • pageContextThe attributes stored in the object can only be accessed by various components called during the current response process of the current JSP page , for example, the JSP page that is responding to the current request and the various custom label classes it calls.

Current user context information: session

appication: current application

pageContext: current page

request: current request

  1. Which one of the following statements is correct? ()

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

    java中的集合类(如Vector)可以用来存储任何类型的对象,且大小可以自动调整。但需要事先知道所存储对象的类型,才能正常使用。
    在java中,我们可以用违例(Exception)来抛出一些并非错误的消息,但这样比直接从函数返回一个结果要更大的系统开销。
    java接口包含函数声明和变量声明。
    java中,子类不可以访问父类的私有成员和受保护的成员。
    

Analysis:

  • A.vector is a thread-safe ArrayList that occupies continuous space in memory. There is an initial size at the beginning, and when the number of data is greater than this initial size, a larger continuous space will be overwritten and allocated. If Vector is defined to store Object, any type can be stored.

  • The situation mentioned by option B is the situation of our custom exception , please read carefully: we can use Exception to throw some messages that are not error, yes , not error messages. For example, if I customize an exception, if a variable is greater than 10, an exception will be thrown. This corresponds to the situation described by option B. I use an exception to indicate that the variable is greater than 10, instead of using a function body (the function body determines whether If it is greater than 10, then return true or false) Judgment, **Because the function call is pushed into the stack, the stack is the fastest under the register and takes up less space, and the custom exception is stored in the heap, which is definitely an exception The memory overhead is large! **So B is right.

  • The C option says that the interface contains method declarations and variable declarations. Because the methods in the interface are abstract public by default, writing only function declarations in the interface conforms to the grammatical rules . But **variables are decorated with public final static by default, meaning that they are static constants, and constants must be initialized when declared in interfaces or classes! **So the second half of C is wrong, it must be initialized at the time of declaration!

  • D. Subclasses can access protected members of the parent class

  1. Among the following containers, which containers are searched by key with a complexity of O(log(n)) ()

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

std::unordered_set
std::multimap
std::map
std::deque

Analysis:

In the STL library, the bottom layers of map and multimap are implemented by red-black trees. The difference between the two is that multimap allows duplication, but not in map.

The search complexity of the red-black tree is O(log(n))

The bottom layer of unodered_map/_set is implemented by a hash table, and the search complexity is O(1)

Guess you like

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