2021 summary of the most comprehensive collection of 106 high-frequency Java interviews (nine topics)

General questions about Java

1. What is JVM? Why is Java a cross-platform programming language?

The Java Virtual Machine (Java Virtual Machine) is a virtual machine that can execute Java bytecode. Each Java source file will be compiled into a bytecode file and then executed in the JVM. The reason why Java is designed to run on any platform without the need to rewrite or recompile under a different platform is due to the Java Virtual Machine (JVM), because the JVM knows the length of specific instructions and the underlying layer very well. The particularity of the hardware platform.

2. What is the difference between JDK and JRE?

The Java Runtime Environment (Java Runtime Environment) is a basic Java virtual machine for running Java programs, including a browser plug-in that executes applets. JDK (Java Development Kit) is a full-featured software development kit for Java to develop, compile and execute Java applications, including JRE, compilers and tools (such as JavaDoc and Java Debugger).

3. What does the "static" keyword mean? Can I override private or static methods in Java? keyword mean? Can you override private or static method in Java?

The static keyword means that when accessing this member variable or method, it is not necessary to obtain an instance of the class to which it belongs.

The static method in Java cannot be overridden, because the override mechanism is dynamic binding at runtime, while the static method is statically bound at compile time. The static method is not related to any specific instance of the class, so the concept of inheritance cannot be applied.

4. Can you access non-static variables in static methods?

The static variable in Java belongs to the corresponding class, and its value is the same for all instances of the class. The static variable is initialized when the JVM loads the class. If the code tries to access non-static variables, and not through the instance of the class, the compiler will report an error, because these non-static variables have not been created yet, and they are not associated with the instance.

5. What data types does Java support? What are Autoboxing and Unboxing?
The 8 basic data types supported by the Java language are as follows:

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char

Autoboxing refers to the automatic conversion done by the Java compiler between basic data types and corresponding wrapper classes. For example, the compiler converts int to Integer, doubles to Double, and so on. The reverse conversion is called unboxing.

6. What is the Override and Overload
of a method in Java ? The overload of a method in Java occurs under the condition that there are two or more method names in the same class with the same name but different parameter lists . On the other hand, the override of a method means that the subclass redefines the same method in the parent class. Override methods must have exactly the same method name, parameter list, and return type. The Override method does not restrict the access rights of the original method.

7. The concept of constructor, constructor overloading and copy constructor in
Java 8. Does Java support multiple inheritance?
9. What is the difference between an interface and an abstract class?
10. Pass by reference and pass by value

Java thread

11. What
is the difference between a process and a thread? A process is the execution of a program (that is, a running program), but a thread is an independent sequence of execution in a process. A process can contain many threads. A thread is sometimes called a lightweight process .

12. Tell me about the different ways to create threads. Which way do you prefer and explain why?
There are three ways to create threads:

  • Inherit the Thread class.
  • Implement the Runnable interface.
  • Create a thread pool through the Executor framework.

The preferred way is to implement the Runnable interface, because it does not need to inherit from the Thread class. When your program design requires multiple inheritance, using the interface will help. In addition, the thread pool is very efficient and easy to implement.

13. Explain the available thread states.
14. What is the difference between synchronization methods and synchronized blocks?
15. How does thread synchronization happen in the monitor? What levels of synchronization can you use?
16. What is a deadlock?
17. How to ensure that no deadlock occurs when N threads access N resources?

Java Collections

18. The basic interface of the
Java Collections framework ? The Java Collections framework provides a series of well-designed interfaces and classes that support object collection operations. The most basic interfaces in the Java Collections framework are:

  • Collection, represents a group of objects (elements).
  • Set, contains a collection of non-repeating elements.
  • List, an ordered collection of non-repeating elements.
  • Map, an object containing key-value pairs of non-duplicate keys.

19. Why does Collection not inherit the Cloneable and Serializable interfaces?

The Collection interface describes a group of objects composed of elements. Each specific implementation of Collection can choose its own way to manage elements. Some collections allow duplicate keys, while others do not.

The semantics and effects of copying and serialization will work when the actual implementation is processed. Therefore, the specific implementation of the collection class should determine how they will be copied and serialized.

20. What is Iterator?

The Iterator interface provides many methods for iterating collections. Each java collection (Collection) contains an iterator method that returns an Iterator instance. The iterator can remove elements in the underlying collection during the iterative process.

21. What is the difference between Iterator and ListIterator?
22. What is the difference between fail-fast and fail-safe?
23. How does HashMap in Java work?
24. What is the importance of hashCode() and equals() methods?
25.HashMap What are the differences with HashTable?
26. What is the difference between Array and ArrayList? When would you use Array compared to ArrayList?
27. What is the difference between ArrayList and LinkedList?
28. What are the Comparable and Comparator interfaces? List their differences.
29. What is Java Priority Queue?
30. What do you know about Big-O notation? Can you give some examples for different data structures?
31. How to weigh ordered arrays and unordered arrays?
32. What are the best practices for the Java collection framework?
33. What is the difference between Enumeration and Iterator interface? 34. What is the difference between
HashSet and TreeSet?

Garbage collector

35. What is the purpose of garbage collection in Java and when is it used?

Garbage collection is used to identify and discard objects that are no longer needed by the program in order to reclaim and reuse resources.

36. What is the purpose of the System.gc() and Runtime.gc() methods?

These methods are used to remind the JVM to start garbage collection. However, the timing of the start of garbage collection is determined by the JVM.

37. When is finalize() called? What is its purpose?

The finallize method is called by gc (garbage collector) before releasing the memory of the object. It is usually recommended to release the resources held by the object in this method.

38. If the reference of an object is set to null, will gc release the memory of the object immediately?
39. What is the structure of the Java heap? What is the Perm Gen (Permanent Generation) space in the heap?
40. Serial garbage The difference between the collector and the Throughput garbage collector?
41. When will the object be recycled?
42. The garbage collection occurs in the designated JVM area?

Exception handling

43. What
are the two types of exceptions in Java ? The difference between them? Java has two types of exceptions: checked and unchecked (checked and unchecked) exceptions. If unchecked exceptions may be executed in the method or constructor Throws to spread to the outside of the method or constructor, they do not need to declare the throws clause in the method or constructor. However, checked exceptions must be declared through the throws clause of the method or constructor. Recommendations on java exception handling Please refer to this Java exception handling.

44. What is the difference between exceptions and errors in Java?

Both Exception and Error are subclasses of the Throwable class. Exception is used for the abnormal conditions that the user program needs to catch. Error defines the unforeseen exceptions of the user program

45. The difference between throw and throws? The
keyword throw is used to explicitly throw an exception in the program. On the contrary, the throws clause is used to indicate the exceptions that are not handled in the method. Each method must explicitly specify which exceptions No processing, so that the caller of the method can prevent possible exceptions. Finally, multiple exceptions are separated by commas.

45. The importance of the finally block in exception handling?
46. ​​What happens to the exception object after the exception is handled?
47. How to distinguish the finally block from the finalize() method?

Java Applets

48. What is an Applet?
A Java Applet can be included in an HTML page and can be run in a browser with a Java client enabled. Applets can be used to create dynamic and interactive web applications.

49. Description of Applet Life Cycle
An Applet may go through the following states:

  • Init : Initialize every time you load
  • Start : start executing an applet
  • Stop : Stop executing an applet
  • Destroy : Perform final cleanup before uninstalling the applet

50. What happens when the applet loads?
First, an instance of the applet's control class will be created. Then, the applet is initialized, and finally the applet starts to run.

51. What is the difference between
Applets and Java applications? Applets need a browser that supports Java, but Java applications can be executed separately. However, they all need a Java virtual machine, JVM.

Also, a Java application needs a main method and a specific signature to ensure startup. Java applets do not need such things.

Finally, Java Applets usually use strict security policies, while Java applications usually use more relaxed security policies.

52. What are the restrictions imposed on Java Applet?
53. What is an untrusted applet?
54. What is the difference between a small program loaded through the file system when the Internet is loaded and an applet?
55. What is an applet class loader, and what does it provide?
56. What is applet security management and what does it provide?  Applet security management is a mechanism that restricts Java applets. The browser can only have one security manager. The security manager is created at startup, after which it cannot be replaced, reloaded, rewritten or extended.

Swing

57. What is the difference between Choice and List?
Choice is a compact way to display, it must be pulled down, so that the user can see the list of all available options. Choice can only select one option. List is displayed in a way that several List options are visible. List supports selecting one or more List options.

58. What is the layout manager?
The layout manager is used to organize the components in the container.

59. What is the difference between Scrollbar and JScrollPane?
Scrollbar is a component, but not a container, and ScrollPane is a container. ScrollPane handles its own events and performs its own sliding.

60. Which Swing methods are thread safe?
61. Name 3 sub-categories that support drawing.
62. What is cutting?
63. What is the difference between MenuItem and CheckboxMenuItem?
64. How are the elements of BorderLayout organized?
65. How are the elements of GridBagLayout organized?
66. What is the difference between Window and Frame?
67. The relationship between cropping and repainting?
68. What is the relationship between the event listener interface and the event adapter class?
69. How does a GUI component handle its own events?
70. What advantages does Java Layout Manager provide over traditional window systems?
71. What design pattern does Java use for all Swing components?

JDBC

72. What is JDBC?
JDBC is an abstraction layer that allows users to choose between different databases. JDBC enables developers to write database applications in Java without having to care about the underlying details of a particular database.

73. Explain the role of the driver in JDBC.
The JDBC driver provides a database vendor-specific implementation of the abstract classes provided by the JDBC API. Each driver must provide the following class implementations of the java.sql package: Connection, Statement, PreparedStatement, CallableStatement, ResultSet and Driver.

74. What is the purpose of the Class.forName method?
This method is used to load the driver to establish a connection with the database.

75. Compared with Statement, the advantages of PreparedStatement?
76. The purpose of CallableStatement ? Point out the method used to create CallableStatement.
77. What is the connection pool?

Remote Method Invocation (RMI)

78. What is RMI?
Java Remote Method Invocation (RMI) is a Java API that implements object-oriented equivalent remote procedure call (RPC) methods, including direct transmission of serialized Java classes and distributed garbage collection stand by. Remote method invocation (RMI) can also be seen as the process of activating a method on a remotely running object. RMI provides location transparency because the user believes that a method is executed on an object that is running locally. RMI Tips here.

79. What is the basic principle of RMI's architecture?
The most important principle of RMI's architecture is to treat the definition of behavior and the implementation of behavior separately. RMI allows the defined behavior and implementation behavior to remain independent, and code that runs in a separate JVM.

80. What is the architecture layer of RMI?
The structure of RMI is mainly divided into the following layers:

  • Stub and Skeleton layer: This layer is located below the developer view. This layer is responsible for intercepting the methods of the client request interface and redirecting these requests to the remote RMI service.
  • Remote reference layer: The second layer of the architecture handles the resolution of remote object references from the client to the server. This layer resolves and manages references from clients to remote service objects. The connection is one-to-one (unicast) connection.
  • Transport layer: This layer is mainly responsible for connecting the two JVMs participating in the service. It is based on the TCP/IP of the machine connected through the network, provides basic connectivity, and some firewall penetration strategies.

81. What is the role of the remote interface in RMI?
82.
What role does the java.rmi.Naming class play? 83. What is the meaning of binding in RMI?
84. The difference between bind and rebind in Naming class?
85. Steps to run RMI program?
86. Stub in RMI Role?
87. What is DGC? How does it work?
88. What is the purpose of using RMISecurityManager in RMI?
89. Explain the grouping and ungrouping.
90. Explain serialization and deserialization.

Servlets

91. What is a servlet? A
servlet is a Java programming language class used to process client requests and generate dynamic web content. Servlets are mostly used to process or store data submitted by HTML forms, provide dynamic content and manage state information that is not in the HTTP stateless protocol.

92. Explain the architecture of a Servlet. The
core abstraction must be that all servlets must implement the javax.servlet.Servlet interface. Each servlet must implement this interface directly or indirectly, and can also inherit from 
javax.servlet.GenericServlet or
javax.servlet.http.HTTPServlet. The last thing I want to mention is that each servlet can use multiple threads to service multiple requests.

93. What is the difference between an Applet and a Servlet?
An Applet is a client java program running in the web browser of the client machine. On the contrary, a servlet is a container of services running on a web server. An applet can use the user interface class, but a servlet cannot have a user interface. Instead, a servlet waits for the client's HTTP request and generates a response for each request.

94. What is the difference between GenericServlet and HttpServlet?
GenericServlet is a universal protocol-independent servlet that implements the Servlet and ServletConfig interfaces. Those servlets that inherit from the GenericServlet class will override the service method. The last thing I want to mention is that in order to develop an HTTP servlet that uses HTTP protocol services for Web users, your servlet must be inherited from HttpServlet instead. Check out the example of Servlet.

95. Explain the life cycle of a Servlet.
96 What is the difference between .doGet() and doPost()?
97.
What is a web application? 98. What is a server-side inclusion (SSI)?
99. What is a servlet chain?
100. How to know the client information requesting a servlet?
101.
What is the structure of an Http response? 102. What is a cookie ? What is the difference between session and cookie?
103. What protocol does the browser and servlet communicate through?
104. What is the HTTP channel?
105. The difference between sendRedirect and forward methods?
106. What is URL encoding and decoding?

The editor has compiled the above interview collections, and there are more redis, high concurrency, multithreading, microservices, distributed, MySQL, database, thread, lock, jvm, Java virtual machine, spring and other interview question banks. Share them together for your reference. You can click on the portal if you need it ! !

Guess you like

Origin blog.csdn.net/Java6888/article/details/114881991