java common interview questions

1. What are the aspects of object-oriented features  ①Abstract : ②Inheritance : ③Encapsulation : ④Polymorphism
  

   

   

   

2. Is String the most basic data type?

Basic data types include byte , int, char, long, float, double, Boolean, and short.

The java.lang.String class is final , so it cannot be inherited or modified. in order to mention

Efficient and space-saving, we should use the StringBuffer class

3. What is the difference between int  and Integer?

Java  provides two different types: reference types and primitive types (or built-in types). Int is the primitive of java

The primitive data type, Integer is a wrapper class provided by java for int.

4. What is the difference between String  and StringBuffer?

The JAVA platform provides two classes: String and StringBuffer, which can store and manipulate strings, that is, packages

Character data with multiple characters. The String class provides numerically immutable strings. And the string provided by this StringBuffer class is modified. You can use StringBuffer when you know the character data is going to change. Typically, you can use StringBuffers to dynamically construct character data.   

5. Can an interface inherit an interface? Can  an abstract class implement an interface? Can an abstract class inherit a concrete class?

Interfaces can inherit interfaces. Abstract classes can implement (implements) interfaces, whether abstract classes can inherit entity classes, but only if the entity class must have a clear constructor.

6. Does the array have the method of length()? Does the String have the method of length()? Arrays do not have the method length(), but have the attribute length. String has length() method.

7. Is it possible to inherit the String class?

The String class is final and cannot be inherited.

8. If there is a return statement in try {}, will the code in finally {} following this try be executed, before or after return?

Will execute, execute before return .

9. What collection classes do you know? main method?

The most commonly used collection classes are List  and Map. Specific implementations of List include ArrayList and Vector, which are variable-sized lists that are more suitable for building, storing, and manipulating lists of elements of any type of object. List is suitable for accessing elements by numerical index. 
   Map provides a more general way of storing elements. The Map collection class is used to store pairs of elements (called "keys" and "values"), where each key maps to a value.

10. There are several implementation methods for multithreading. What are they?  There are two implementation methods for multithreading, namely inheriting the Thread class and implementing the Runnable interface. 

11. The built-in object and method request of JSP
represent the HttpServletRequest
object. It contains information about the browser request and provides

There are several  useful methods for obtaining cookie, header, and session data. 
response represents the HttpServletResponse object, and provides several responses to be sent back to the browser

The corresponding methods (such as cookies, headers, etc.) 
The out object is an instance of javax.jsp.JspWriter and provides several methods that you can use to send

The output result is sent back to the controller. 

pageContext represents a javax.servlet.jsp.PageContext object. It is used for easy access to various

A scoped namespace, an API for servlet-related objects, and methods that wrap common servlet-related functionality. 
    A session represents a requested javax.servlet.http.HttpSession object. Session can store the user's state information 
   applicaton represents a javax.servle.ServletContext object. This helps to find information about the servlet engine and the servlet environment 
   page represents a servlet instance spawned from that page

12. The life cycle of the servlet
The web
container loads the servlet , and the life cycle begins. servlet by calling the init() method of the servlet

initialization. It is implemented by calling the service() method, and different do***() methods are called according to different requests. end

Service, the web container calls the destroy() method of the servlet.

13. The method of object transfer between pages

request,session,application,cookie等

14. Can a ".java" source file include multiple classes (not inner classes)? What are the restrictions? 

Yes, only one class name must be the same as the file name.

15. How many types of streams are there in java ? JDK provides some abstract classes for each type of stream for inheritance, please name them which classes are?

Byte stream, character stream. Byte stream inherits from InputStream OutputStream , and character stream inherits from InputStreamReader OutputStreamWriter. There are many other streams in the java.io package, mostly for performance and ease of use.

16. What are the data types supported by Java?

byte

short

int

long

float

double

boolean

char

17. Does Java support multiple inheritance?

No, Java does not support multiple inheritance. Each class can only inherit from one class, but can implement multiple interfaces.

18. What is a deadlock (deadlock) ?

A deadlock occurs when both processes are waiting for the other to finish executing before continuing. The result is that both processes are stuck in an infinite wait.

19. What is an expression (Expression) ?

A JSP expression is a Web server that converts the value of a scripting language expression into a String object and inserts it into the data stream returned to the client. Expressions are defined between the <%= and %> tags.

20. How to know which client machine is requesting your servlet ?

The ServletRequest class can find out the IP address or hostname of the client machine . The getRemoteAddr() method gets the IP address of the client host, and getRemoteHost() gets the host name.

21. What is the difference between the doGet() method and the doPost() method?

doGet : The GET method appends name-value pairs to the requested URL. Because URLs have a character limit, which in turn limits the number of parameter values ​​used in client requests. And the parameter values ​​in the request are visible, therefore, sensitive information cannot be passed in this way.

 

doPOST : The POST method overcomes the limitations of the GET method by placing the request parameter values ​​in the request body , so there is no limit to the number of parameters that can be sent. Finally, sensitive information passed through POST requests is invisible to external clients.

22. What is a Servlet ?

Servlets are Java classes used to process client requests and generate dynamic web content . Servlet is mainly used to process or store data submitted by HTML forms, generate dynamic content, and manage state information under the stateless HTTP protocol.

23. What is JDBC ?

JDBC is an abstraction layer that allows users to choose between different databases. JDBC allows developers to write database applications in JAVA without having to care about the details of the underlying specific database.

24. What does the Class.forName() method do?

This method is used to load the driver that establishes the connection to the database.

25.What are the advantages of PreparedStatement over Statement ?

PreparedStatements are precompiled, therefore, the performance will be better. At the same time, PreparedStatement can be reused for different query parameter values.

26.What is the difference between throws and throws ?

The throw keyword is used to explicitly throw exceptions in a program, whereas the throws statement is used to indicate exceptions that the method cannot handle. Each method must specify which exceptions cannot be handled, so the caller of the method can ensure that possible exceptions are handled. Multiple exceptions are separated by commas.

27. What is the importance of the finally block in exception handling ? (Translator's Note: The serial number of the author's title is wrong)

The finally block will always be executed whether or not an exception is thrown . Even if there is no catch statement and an exception is thrown at the same time, the finally block will still be executed. The last thing to say is that the finally block is mainly used to release resources, such as: I/O buffers, database connections.

28. What is the difference between ArrayList and LinkedList.

Both ArrayList and LinkedList implement the List interface with the following differences: 

1.ArrayList is an index-based data interface. Its bottom layer is an array. It can perform random access to elements in O(1) time complexity. Correspondingly, LinkedList is data stored in the form of a list of elements, each element is linked to its previous and subsequent elements, in this case, the time complexity of finding an element is O(n) . 

2. Compared with ArrayList , LinkedList's insertion, addition, and deletion operations are faster, because when elements are added to any position in the collection, there is no need to recalculate the size or update the index like an array.

3. LinkedList occupies more memory than ArrayList , because LinkedList stores two references for each node, one points to the previous element and one points to the next element.

29. Can Ajax do asynchronous requests?

Yes. Ajax requests are asynchronous by default. If you want to synchronously set async to false, the default is true

if it is jquery

$.ajax({

url: some.php,

async: false,

success : function(){

}

});

30. If a website has a large number of people logging in to visit it. Then there will be a lot of sessions , what should you do if you are a programmer.

Sessions are stored in memory by default, and memory resources are precious. The large amount of session data leads to high memory utilization. The following solutions solve the problem of session memory storage:

1. You can set the session timeout time, and the session will be automatically cleared when the timeout time is reached.

<session-config>

<session-timeout>20</session-timeout>

</session-config> 

2. Serialize the data in the session to the hard disk

3. Do not use session , use cookie (this method has security problems)

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325378307&siteId=291194637