Summary of Java Wrong Questions on Niuke.com (7)

table of Contents

One, dependency injection

Second, the stacking and stacking order of the method

Three, abstract class and interface

Four, JavaWeb session binding monitoring

Five, string


 

One, dependency injection

Analysis:

Dependency Injection (DI) is an important object-oriented programming law to reduce the coupling problem of computer programs.

Dependency injection is widely used. The configuration and dependency specifications of the application can be separated from the actual application code.

One of the features is to configure the relationship between application components through text configuration files, without re-modifying and compiling specific codes.

Therefore, dependency injection reduces the coupling between components, rather than making them dependent on each other.

Reference address: https://blog.csdn.net/sinat_21843047/article/details/80297951

 

Second, the stacking and stacking order of the method

Analysis:

This question uses the method of recursive calling. The method will first be pushed into the stack in the order of recursive calling.

When popping from the stack, the return statement is executed. If there is no return statement, the stack is directly popped and destroyed.

Another pitfall is that the if statement on the seventh line is followed by a semicolon; so the print statement on the eighth line will be executed regardless of whether the conditions are met or not.

if (number % 2 != 0) System.out.print(split((number + 1) / 2));
    System.out.print(split(number / 2));

So when number=2, although the condition number% 2 != 0 is not met, System.out.print(split(number / 2)); will be executed, so 1 is printed

 

Three, abstract class and interface

Analysis:

  • A: Abstract classes can declare and define constructors , but cannot create instance objects. They can only be used to initialize internal variables and provide subclasses for implementation.
    • ​​​​​​​Even if no constructor is defined, the system will add a no-argument construction by default. When the subclass inherits, the subclass's constructor will implicitly call the super() method to call the abstract class's construction method
  • B: The interface is not allowed to be instantiated, so there is no constructor at all

  • C: Abstract classes do not allow multiple inheritance
  • D: After jdk1.8, the method modified with static or default in the interface can have a method body, but not before 1.8

 

Four, JavaWeb session binding monitoring

Analysis:

 

  • HttpSessionAttributeListener : This listener interface can be implemented to obtain notifications of changes to the session attribute list in this web application;
  • HttpSessionBindingListener : Notify the object when the object is bound or unbound from a session. This object is notified by the HttpSessionBindingEvent object. This may be the result of the servlet program explicitly unbinding the attributes from the session, it may be because the session is invalid, or it may be due to the session timeout;
  • HttpSessionObjectListener : There is no such interface API;
  • HttpSessionListener : Notify the implementation class of this interface when the active session list in the web application changes. In order to receive the notification event, the implementation class must be configured in the deployment descriptor of the web application;
  • HttpSessionActivationListener : Objects bound to the session can listen to container events and notify them that the session will be passivated and the session will be activated. A container that migrates sessions or persistent sessions between virtual machines is needed to notify all the properties bound to the session that implements the interface.

 

Five, string

Analysis:

  • A:'a' is a character,'a' is a space and a, you must use "a";
  • B: String has a length() method
  • C: int cannot be directly converted to String type
  • D: Add string "100" at the end (when other types of data and string type data are calculated, it will be automatically converted to string type )

Guess you like

Origin blog.csdn.net/weixin_39478524/article/details/115102205