Analysis and Summary of Baidu Java R & D interview questions
1. Analysis of Common application scenarios in singleton mode.
Among the 23 design modes, the singleton mode is the largest. It is easy to understand, but applicable to scenarios. Are you really familiar with this? The use of Singleton is because there is no need to create an object for each request, which wastes both CPU and memory. The use of multiple examples is to prevent concurrency problems; that is to say, a request changes the state of the object. At this time, the object processes another request, and the previous request changes the state of the object, causing the object to process another request incorrectly.
Let's talk about the singleton Application Scenario in Java web: the database connection pool is the singleton mode, with only one connection pool manager managing multiple connection pool objects. Objects in common service and dao layers are generally single-instance (because all requests are processed by one object), and actions in struts2 are multiple examples, because each request is processed with a new object (because action contains the value of the request parameter, the status can be changed ). Log4j logging is also a singleton mode, because only one object is maintained from the beginning. (Generally, the log application of an application is implemented in singleton mode. This is generally because the shared log file is always in the open state, because only one instance can be operated, otherwise, the content cannot be appended ).
2. What is a balanced binary tree.
It is either an empty tree or a binary tree of the following nature: the absolute value of the depth difference between its left and right subtree cannot exceed 1, the left and right subtree are both a balanced binary tree.
3. What is a red/black tree.
The red and black trees are special balanced binary trees. Follow the red theorem and the black theorem. Red theorem: there cannot be two connected red nodes in one path; black theorem: the root node must be a black node, and all nodes are directed to the end of the tree, the number of black nodes must be equal.
4. What is B tree. (Binary Search Tree)
All nodes store one keyword. The left pointer of a non-leaf node points to a subtree smaller than its keyword, And the right Pointer Points to a subtree larger than its keyword. All non-leaf nodes have a maximum of two sons.
5. What is B-tree. (Multi-path Search Tree)
Root Node son tree [2, m]; non-root node non-leaf node son tree [m/2, m]; each node has a keyword number of [m/2-1, m-1
6. What is the B + tree.
The B + tree is a variant of the B-tree. Based on the B-tree definition, it is added that all keywords appear on the leaf node, and a chain pointer is added to all leaf nodes; the number of Subtrees and keywords for non-leaf nodes is the same.
Summary: The balanced binary tree is a binary search tree. It can ensure that the node is found in the log2 (n) time, while the performance of the common Binary Search Tree is similar to that of the linked list in the worst case. The time used is log (n ). The red/black tree is used for internal sorting, that is, it is fully stored in the memory. The internal implementation of Microsoft STL map and set is the red/black tree. Tree B is usually stored in the memory, and most of the data is stored in the memory. Because the number of B-tree layers is small, you can ensure that each operation reads as few disks as possible. When the data is small and can be fully stored in the memory, the time complexity of the red/black tree is lower than that of the B tree. On the contrary, when the data volume is large and the external storage occupies the main part, the B-tree has a faster speed because it reads less disk times.
Supplement the red-black tree: although the hash table is quickly searched, the hash table length will become longer and conflicts will increase as the data types increase, how can we achieve high performance in the case of large data volumes? At the same time, we know that the tree is a good data structure, which is used for insertion, deletion, and search. It is a very efficient data tree structure, but the problem is that in very bad circumstances, the operation is time-consuming and its performance is not guaranteed. For example, if the left and right subtree in the binary search tree are too far apart, it will be time-consuming to search. In this case, to ensure its efficiency, we must ensure that the Left and Right trees cannot be too far away. When inserting data into the tree, we should adjust the data according to certain rules to make it conform to the rules, this improves the overall and local search efficiency. This is the rule of the red and black trees.
7. Differences between final finally finalize.
Finalize is a method, and it is a method in the Object class. When the Garbage Collector determines that this object is not referenced, it calls the finalize method of the object. It is used to perform some necessary cleanup work before GC clears objects (for example, the finalize method of input/output connected objects can execute explicit I/O transactions, to interrupt the connection before permanently dropping the object ).
Finally is used in the try and catch exception mechanism and will be executed in any case.
The final modifier class indicates that the object cannot be inherited. It modifies the basic data type, that is, the constant is immutable. It modifies the reference type, and the object content pointing to is variable, and the object is immutable.
8. Error Exception RuntimeException
The inheritance structure of the Exception mechanism first, the base class is Throwable; Error and Exception inherit Throwable; RuntimeException and IOException inherit Exception (that is, the specific RuntimeException inherits RuntimeException ).
Error describes internal errors and resource depletion. Applications should not throw such types of objects, which are generally thrown by virtual machines ). The program level cannot be processed.
Exception includes RuntimeException and other non-RuntimeException. RuntimeException includes incorrect type conversion, out-of-bounds array access, and empty pointer for graph access. The principle for processing RuntimeException is: If RuntimeException occurs, it must be a programmer error. For example, you can check the array subscript and array boundary to avoid array out-of-bounds access exceptions. Non-RuntimeExceptionIOException): This type of exception is generally an external error, such as an attempt to read data from the end of the file. This is not a program error, but an external error in the application environment.
Summary: for example, 5/0 is a runtimeException. If this exception is not handled in the program, no error is reported and an exception is reported during running. A non-runtimeException exception is a try catch exception in the program. If it is not processed, an error is returned.
9. Differences Between TCP UDP HTTP HTTPS SFTP FTP
TCP reliable connection, three handshakes, four waves.
Unreliable UDP connection.
HTTPS is a network protocol built using SSL + HTTP for encrypted transmission and identity authentication. It is more secure than HTTP. It uses SSL, which is the Secure Socket Layer (SSL) for information exchange. You are charged for applying for a certificate for HTTP.
FTP is the file transfer protocol. On the website, if you want to share files with people, the most convenient way is to upload the files to the FTP server. Others can download the required files through the FTP client program. FTP file transmission must be performed through the port. Generally, the required port is control link-TCP port 21. Controller. It is used to send commands to the server and wait for the server to respond. Data Link --- TCP port 20. Data transmission port. Used to establish a data transmission channel. It is mainly used to send a file from the client to the server, send a file from the server to the client, and send a file or directory list to the client from the server. FTP supports active connection and passive connection to adapt to different network environments. Both modes are mainly for data links and are irrelevant to control links. SFTP is short for Secure File Transfer Protocol and is a Secure File Transfer Protocol. It provides a secure encryption method for transferring files. Almost the same as ftp syntax. SFTP is part of SSH and is a secure method for transferring files to the Blogger server. It does not have a separate daemon, and must use the sshd daemon to complete the corresponding connection operations. Therefore, SFTP is not like a server program in a sense, it is more like a client program. SFTP also uses encryption to transmit authentication information and transmitted data, so it is safe to use SFTP. However, because this transmission method uses encryption/decryption technology, the transmission efficiency is much lower than that of common FTP. If you require more network security, use FTP instead.
10. In Java parameter passing, value passing and reference passing are different.
When the value is transferred, the transmitted parameter is the copy transfer of the value. After the transfer is completed, there will be no difference. The parameter passed is the reference address. That is, the address of the memory space corresponding to the variable. The distinction between the two is similar to that in the C language, the function method is used to exchange the values of two numbers.
- Int I = 0;
- I = I ++;
-
- // The output I value is 0; the order should be: Calculate the expression value first, get 0, then I add 1, then I = 1, and finally assign a value, A value assignment is to assign a value to the expression. Therefore, I is changed to 0 again.
11. In server network programming, what are the solutions for session tracking?
Cookie; Session; URL rewriting;
Cookies are used in J2EE projects. Java encapsulates cookies into java. servlet. http. cookie classes. Each Cookie is an object of this Cookie class. The server operates on client cookies by operating on Cookie objects. Use request. getCookies () to obtain all cookies submitted by the client and return them in the form of a Cookie [] array. Use response. addCookie (Cookiecookie) to set cookies for the client. The Cookie object uses the key-value Attribute pair to save the user status. A Cookie object saves one attribute pair and one request or response uses multiple cookies at the same time ..
- // Obtain the cookie from the server
- Cookie [] cookies = request. getCookies ();
- For (int I = 0; cookies! = Null & I <cookies. length; I ++ ){
- Cookiecookie = cookies [I];
- If ("username". equals (cookie. getName ())){
- }
- }
-
- // Set the cookie on the server side
-
- String username = "sa ";
- Cookie usernameCookie = new Cookie ("username", username );
- Response. addCookie (usernameCookie );
Cookie cannot be cross-domain or the cookie's validity period.