Java face questions and Answers-(next) _java

Source: Internet
Author: User
Tags define local event listener garbage collection http request object serialization prepare stub web services

The first one discusses object-oriented programming and its features, common questions about Java and its functionality, Java collection classes, garbage collectors, this chapter focuses on exception handling, Java applets, SWING,JDBC, remote method Invocation (RMI), servlet, and JSP.

Exception handling

Java Small application (Applet)

Swing

Jdbc

Remote method call (RMI)

Servlet

Jsp

Exception handling

What are the two exception types in 43.Java? What's the difference between them?

There are two exceptions in Java: A checked (checked) exception and an unchecked (unchecked) exception. An unchecked exception does not need to be declared on a method or constructor, even if the execution of the method or constructor may throw such an exception, and unchecked exceptions can propagate to the method or outside of the constructor. Conversely, a checked exception must be declared on a method or constructor using a throws statement. Here are some small suggestions for Java exception handling.

What is the difference between exception and error in 44.Java?

Exception and error are all throwable subclasses. Exception is used for exceptions that can be caught by a user program. The error defines an exception that is not expected to be caught by the user program.

What's the difference between 45.throw and throws?

The Throw keyword is used to explicitly throw an exception in a program, whereas a throws statement is used to indicate an exception that the method cannot handle. Each method must specify which exceptions cannot be handled, so the method's caller is able to ensure that possible exceptions are handled and multiple exceptions are separated by commas.

45. When handling exceptions, what is the importance of finally code blocks? (Translator Note: The serial number of the author's title is wrong)

Finally code blocks are always executed, regardless of whether an exception is thrown. Even if there is no catch statement and throws an exception, finally code blocks are still executed. The last thing to say is that finally code blocks are primarily used to release resources, such as I/O buffers, database connections.

46. What happens to the exception object after exception handling is completed?

The exception object is reclaimed during the next garbage collection.

What is the difference between a 47.finally code block and a Finalize () method?

Finally code blocks are executed regardless of whether an exception is thrown, which is primarily used to free the resources that the application occupies. The Finalize () method is a protected method of the object class that is invoked by the Java virtual machine before the object is garbage collected.

Java Small application (Applet)

48. What is an applet?

Java applets are programs that can be included in HTML pages and can be executed by Java-enabled client browsers. Applets are primarily used to create dynamically interacting Web applications.

49. Explain the life cycle of applets

Applets can go through the following states:

    1. Init: Each time it is loaded, it is initialized.
    2. Start: Run the applet.
    3. Stop: Ends the applet.
    4. Destroy: Before uninstalling the applet, do the final cleanup work.

50. What happens when an applet is loaded?

First, create an instance of the Applet control class, initialize the applet, and finally start running.

What is the difference between 51.Applet and normal Java applications?

Applets are run in Java-enabled browsers, and Java applications are stand-alone Java programs that can run outside of the browser. However, they all need to have Java virtual machines.

Further, the Java application requires a main function with a specific method signature to begin execution. Java applets do not need such a function to begin execution.

Finally, Java applets typically use very restrictive security policies, and Java applications generally use looser security policies.

What are the limitations of the 52.Java applet?

The following restrictions are imposed on applets, mainly for security reasons:

    1. Applets are not able to load class libraries or define local methods.
    2. Applets cannot read and write files on host hosts.
    3. The applet cannot read specific system properties.
    4. The applet cannot initiate a network connection unless it is with a host.
    5. Applets are not able to open any other programs on the host.

53. What is an untrusted applet?

Untrusted applets are Java applets that do not have access to or execute local system files, and all downloaded applets are untrusted by default.

54. What is the difference between an applet loaded from a network and an applet loaded from a local file system?

When the applet is loaded from the network, the applet is loaded by the Applet class loader and is limited by the Applet security Manager.

When the applet is loaded from the client's local disk, the applet is loaded by the file system loader.

Applets that are loaded from the file system allow the client to read files, write files, load class libraries, and also allow other programs to execute, but do not pass byte-code checksums.

What is a 55.applet class loader? What kind of work does it do?

When the applet is loaded from the network, it is loaded by the Applet class loader. The class loader has its own Java namespace hierarchy. The class loader guarantees that classes from the file system have a unique namespace, and that classes from network resources have unique namespaces.

When the browser loads the applet over the network, the applet's class is placed in the private namespace associated with the applet's source. The classes that are loaded by the ClassLoader are then validated by the validator. The validator checks to see if the class file format complies with the Java language Specification, ensures that no stack overflow (heap overflow) or underflow (underflow) is present, and that the parameters passed to the bytecode instruction are correct.

What is the 56.applet security Manager? What kind of work does it do?

Applet Security Manager is a mechanism to impose restrictions on applets. The browser can have only one security manager. The security manager is created when it is started, and cannot be overwritten or expanded after it has been replaced.

Swing

57. What's the difference between a pop-up selection menu (Choice) and a list?

The choice is shown in a compact form and requires a drop down to see all the options. Only one option can be selected at a time in choice. The list can also have multiple elements visible, supporting the selection of one or more elements.

58. What is a layout manager?

The layout manager is used to organize components in a container.

59. What is the difference between the scroll bar (Scrollbar) and the scrolling panel (jscrollpane)?

ScrollBar is a component, not a container. And ScrollPane is a container. ScrollPane handles scrolling events on its own.

60. Which swing methods are thread-safe?

There are only 3 thread-safe methods: Repaint (), revalidate (), and invalidate ().

61. Say three kinds of components that support redrawing (painting).

Canvas, Frame, Panel, and applet support redrawing.

62. What is cropping (clipping)?

A cut is done by restricting the drawing operation of a given region or shape.

What is the difference between 63.MenuItem and Checkboxmenuitem?

The Checkboxmenuitem class inherits from the MenuItem class, and the Support menu options can be checked or unchecked.

64. What is the layout of the elements inside the Edge layout (borderlayout)?

The elements inside the borderlayout are laid out according to the container's east and west.

65. What is the layout of the elements inside the grid package layout (gridbaglayout)?

The elements inside the gridbaglayout are laid out according to the grid. Elements of different sizes may occupy more than 1 rows or columns of the grid. Therefore, the number of rows and columns can be different sizes.

What's the difference between 66.Window and frame?

The Frame class inherits the window class, which defines a main application window that can have a menu bar.

67. What is the connection between cropping (clipping) and redrawing (repainting)?

When the window is redrawn by the AWT redraw thread, it sets the cropping area to the area of the window that needs to be redrawn.

68. What is the relationship between the event listener interface (Event-listener interface) and the event adapter (event-adapter)?

The event listener interface defines the methods that the event handler must implement for a particular event. The event adapter provides the default implementation for the event listener interface.

How does a 69.GUI component handle its own events?

The GUI component can handle its own events as long as it implements the corresponding event listener interface and acts as an event listener.

What are the advantages of 70.Java Layout Manager over traditional window systems?

Java uses Layout Manager to place components on all window platforms in a consistent fashion. Because the layout manager is not bound to the absolute size and location of the components, they are able to adapt to a particular platform across the Windows system.

What design pattern does the 71.Java swing component use?
The swing components in Java use the MVC (view-model-Controller) design pattern.

Jdbc

72. 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 being concerned with the details of the underlying specific database.

73. Explain the role of the under Driver (Driver) in JDBC.

JDBC drivers provide specific vendor implementations of the JDBC API interface class, and drivers must provide implementations of these classes under the java.sql package: Connection, Statement, Preparedstatement,callablestatement, ResultSet and driver.

What is the effect of the 74.class.forname () method?

This method is used to load the drive to establish a connection to the database.

What advantages does 75.PreparedStatement have over statement?

Preparedstatements is precompiled, so performance will be better. At the same time, different query parameter values, PreparedStatement can be reused.

76. When do you use CallableStatement? What is the method used to prepare the callablestatement?

CallableStatement is used to execute stored procedures. Stored procedures are stored and provided by the database. Stored procedures can accept input parameters, or they can have return results. It is very encouraging to use stored procedures because it provides security and modularity. The way to prepare a callablestatement is:

Callablestament.preparecall ();

77. What does the database connection pool mean?

It can be time-consuming to turn off database connections like this, especially when the number of clients is increasing, and the cost is very high. Many database connections can be established and maintained in a pool when the application server is started. The connection request is provided by a connection in the pool. After the connection is used, return the connection to the pool for more requests in the future.

Remote method call (RMI)

78. What is RMI?

Java Remote method Invocation (Java RMI) is an object-oriented equivalence form provided by the Java API to remote procedure call (RPC), which supports the direct transmission of serialized Java objects and distributed garbage collection. Remote method calls can be viewed as steps to activate a method on a remotely running object. RMI is transparent to the caller because the caller feels that the method executes on an object that is running locally. Take a look at some of the RMI considerations.

What are the basic principles of 79.RMI architecture?

The RMI architecture is based on a very important principle of separation of behavior definitions and behavior implementations. RMI allows the code that defines the behavior to be separated from the code that implements the behavior, and runs on different JVMs.

What are the layers of the 80.RMI architecture?

The RMI architecture is divided into the following layers:

Stub and Skeleton layer (stub and skeleton layer): This layer is transparent to the programmer, it is primarily responsible for blocking the client-issued method call request, and then redirecting the request to the remote RMI service.

Remote reference layer (Sqlremote Reference Layer): The second level of the RMI architecture is used to resolve client references to server-side remote objects. This layer resolves and manages the client's reference to the server-side remote object. The connection is point-to-point.

Transport layer (Transport layer): This layer is responsible for connecting the two JVMs participating in the service. This layer is built on a TCP/IP connection between machines on the network. It provides basic connectivity services, as well as a number of firewall penetration strategies.

What role does the remote Interface in 81.RMI play?

The remote interface is used to identify which methods are interfaces that can be invoked by a non-local virtual machine. The remote object must implement the remote interface directly or indirectly. A class that implements a remote interface should declare the implemented remote interface, define a constructor for each remote object, and provide an implementation for all remote interface methods.

What role does the 82.java.rmi.naming class play?

The Java.rmi.Naming class is used to store and retrieve references to remote objects in the remote object registry. Each method of the naming class receives a string object in a URL format as its argument.

What does the 83.RMI binding (Binding) mean?

A binding is the process of querying for a remote object and associating it with a remote object or a name that will be used after registration. Remote objects can be associated with names by using the bind () or rebind () method of the naming class.

What is the difference between bind () and rebind () methods for the 84.Naming class?

The bind () method is responsible for binding the specified name to the remote object, and the Rebind () method is responsible for rebind the specified name to a new remote object. If the name has already been bound, the previous binding will be replaced.

85. What are the steps to allow the RMI program to function correctly?

In order for the RMI program to work correctly, you must include the following steps:

    1. Compile all the source files.
    2. Use rmic to generate stubs.
    3. Start Rmiregistry.
    4. Start the RMI server.
    5. Run the client program.

What role does the stub of 86.RMI play?

A stub of a remote object acts as a representative of a remote object or as a proxy. The caller invokes the method on the local stub, which is responsible for executing the method on the remote object. When the stub method is invoked, the following steps are experienced:

    1. Initializes a connection to the JVM that contains the remote object.
    2. Serializes the parameters to the remote JVM.
    3. Waits for the result of the method invocation and execution.
    4. The value returned by deserialization or the exception where the method does not execute successfully.
    5. Returns the value to the caller.

87. What is distributed garbage collection (DGC)? How does it work?

DGC is called distributed garbage collection. RMI uses DGC to do automatic garbage collection. Garbage collection is difficult because RMI contains references to remote objects across virtual machines. DGC uses a reference-counting algorithm to provide automatic memory management to remote objects.

What is the purpose of using the RMI Security Manager (Rmisecuritymanager) in 88.RMI?

Rmisecuritymanager uses downloaded code to provide a security manager that can be used by RMI applications. If the security manager is not set, the RMI class loader does not download any classes from the remote.

89. Explain the marshalling and demarshalling.

When an application wants to pass a memory object across the network to another host or to persist to storage, it must convert the object's representation in memory into a suitable format. This process is called marshalling, and vice versa is demarshalling.

90. Explain the serialization and deserialization.

Java provides a mechanism called object serialization, which represents an object as a series of bytes containing the object's data, the object's type information, the type information of the data inside the object, and so on. Therefore, serialization can be seen as a way to flatten objects by storing them on disk or by reading them from disk and rebuilding objects. Deserialization is the opposite step in converting an object from a flat state into an active object.

Servlet

91. What is a servlet?

A servlet is a Java class used to process client requests and generate dynamic Web page content. The servlet is primarily used to process or store data submitted by HTML forms, generate dynamic content, and manage state information under a stateless HTTP protocol.

92. Describe the architecture of the servlet.

All the servlet has to implement the core interface is Javax.servlet.Servlet. Each servlet must implement this interface directly or indirectly, or inherit Javax.servlet.GenericServlet or Javax.servlet.http.HTTPServlet. Finally, the servlet uses multithreading to serve multiple requests in parallel.

What's the difference between 93.Applet and servlet?

Applets are client-side Java programs that run on the client host's browser. The servlet is the component that runs on the server side of the Web service. Applets can use user interface classes, while the servlet does not have a user interface, instead, the servlet waits for the client's HTTP request and then responds to the request.

What's the difference between 94.GenericServlet and HttpServlet?

Genericservlet is a generic protocol-independent servlet that implements the servlet and ServletConfig interfaces. The servlet that inherits from Genericservlet should overwrite the service () method. Finally, to develop a servlet that can be used on a Web page to service a request using HTTP protocol, your servlet must inherit from HttpServlet. Here is an example of a servlet.

95. Explain the lifecycle of the next servlet.

For each client request, the servlet engine loads the servlet, invokes its init () method, and completes the servlet initialization. The Servlet object then handles all subsequent requests from the client by calling the service () method individually for each request, and finally calls the servlet (a servlet instead of a server) destroy () method to remove the servlet.

What is the difference between the 96.doGet () method and the Dopost () method?

The Doget:get method appends the name-value pair to the requested URL. Because the URL has a limit on the number of characters, it limits the number of parameter values that are requested on the client. and the parameter values in the request are visible, so sensitive information cannot be passed in this way.

The Dopost:post method overcomes the restriction of the Get method by placing the request parameter value in the request body, so the number of parameters that can be sent is unrestricted. Finally, sensitive information passed through post requests is not visible to external clients.

97. What is a Web application?

A Web application is a dynamic extension of the Web or an application server. There are two types of Web applications: performance-oriented and service-oriented. Performance-oriented Web applications produce Web pages that contain interactions of many markup languages and dynamic content as responses to requests. Service-oriented Web applications implement the endpoints of Web services (endpoint). Generally speaking, a Web application can be viewed as a set of servlet installed below a specific subset of the server URL namespace.

98. What is a service-side inclusion (Server Side include)?

Server-side inclusion (SSI) is a simple, interpreted server-side scripting language that is mostly used on the web and embedded in servlet tags. The most common scenarios for SSI include one or more files to a Web page on a Web server. When a browser accesses a Web page, the Web server replaces the servlet label in the Web page with the text generated by the corresponding servlet.

99. What is a servlet chain (servlet chaining)?

A servlet chain is a way to send the output of a servlet to another servlet. The output of the second servlet can be sent to the third servlet, and so on. The last servlet on the chain is responsible for sending the response to the client.

100. How do I know which client's machine is requesting your servlet?

The ServletRequest class can find the IP address of the client machine or the host name. The Getremoteaddr () method obtains the IP address of the client host, Getremotehost () can obtain the hostname. Look at the example here.

What is the structure of the 101.HTTP response?

The HTTP response consists of three parts:

Status code: Describes the status of the response. Can be used to check whether the request was completed successfully. In the case of a request failure, the status code can be used to identify the cause of the failure. If the servlet does not return a status code, a successful status code HTTPSERVLETRESPONSE.SC_OK is returned by default.

HTTP headers (HTTP headers): They contain more information about the response. For example, the head can specify an expiration date that is considered to have expired, or a coded format that specifies the content of the transport entity that is used to secure the user. How to retrieve HTTP headers in Serlet look here.

Body: It contains the contents of the response. It can contain HTML code, pictures, and so on. The body consists of data bytes that are transmitted immediately after the head in the HTTP message.

102. What is a cookie? What is the difference between session and cookie?

A cookie is a piece of information that the Web server sends to the browser. The browser stores cookies for each Web server in the local file. When the browser sends a request to a particular Web server, all cookies stored for that server are sent at the same time. The differences between session and Cookie are listed below:

The session should work correctly regardless of how the client browser is set up. The client may choose to disable cookies, but the session still works because the client cannot disable session sessions on the server.

Session and cookies are also different in terms of the amount of data stored. A session can store any Java object, and a cookie can store only string-type objects.

103. What protocols are used by browsers and servlet communications?
The HTTP protocol is used by browsers and servlet Communications.

104. What is an HTTP tunnel?

HTTP tunneling is a technology that encapsulates multiple network protocols for communication using HTTP or HTTPS. Therefore, the HTTP protocol acts as a wrapper over the network protocol used for communication. The request to cover up the requests of other protocols as HTTP is the HTTP tunnel.

What is the difference between 105.sendRedirect () and Forward () methods?

The Sendredirect () method creates a new request, and the forward () method simply forwards the request to a new target. After redirection (redirect), objects that have previously requested scope are invalidated because a new request is generated, and after forwarding (forwarding), objects that have previously requested scope are accessible. Sendredirect () is generally considered to be slower than forward ().

106. What is URL encoding and URL decoding?

URL encoding is responsible for the URL inside the space and other special characters to replace the corresponding hexadecimal representation, the opposite is decoding.

Jsp

107. What is a JSP page?

A JSP page is a text document that contains two types of text, static data and JSP elements. Static data can be represented in any text-based format, such as HTML or XML. JSP is a mixture of static content and dynamically generated content of the technology. Look at the example of JSP here.

How is the 108.JSP request processed?

The browser first requests a page that ends with a. jsp extension, initiates a JSP request, and then the Web server reads the request and uses the JSP compiler to convert the JSP page into a servlet class. It should be noted that only when the first request page or the JSP file changes when the JSP file will be compiled, and then the server calls the Servlet class, processing browser requests. Once the request completes, the servlet sends the response to the client. Here's how to get the request parameters in the JSP.

What are the advantages of 109.JSP?

The advantages of using JSP are listed below:

    1. JSP pages are dynamically compiled into a servlet, so developers can easily update the presentation code.
    2. JSP pages can be precompiled.
    3. JSP pages can be easily combined with static templates, including HTML or XML, and can be easily combined with code that generates dynamic content.
    4. Developers can provide custom JSP tag libraries that allow page designers to access in class XML format.
    5. Developers can make logical changes in the component layer without having to edit pages that use the application-level logic alone.

110. What is JSP instruction (directive)? What are the different types of directives in the JSP?

Directive is the instruction that the JSP engine handles when the JSP page is compiled into a servlet. Directive is used to set page-level directives, insert data from external files, and specify a custom tag library. Directive is defined between <%@ and%>. The different types of directive are listed below:

Contains directives (include Directive): Used to include the contents of files and merged files to the current page.
Page directive: Used to define specific attributes in a JSP page, such as error pages and buffers.
TAGLIB directive: Used to declare a custom tag library used in a page.

111. What is a JSP action (JSP action)?

The JSP action controls the behavior of the servlet engine with the structure of the XML syntax. JSP actions are executed when the JSP page is requested. They can be inserted dynamically into a file, reusing JavaBean components, forwarding users to other pages, or generating HTML code for Java plug-ins. The actions that are available are listed below:

    1. jsp:include-contains a file when the JSP page is requested.
    2. jsp:usebean-Find or initialize the JavaBean.
    3. jsp:setproperty-sets the JavaBean properties.
    4. Jsp:getproperty-gets the properties of the JavaBean.
    5. Jsp:forward-forward the request to the new page.
    6. jsp:plugin-generates code for a particular browser.

112. What is Scriptlets?

In JSP technology, Scriptlet is a piece of Java code embedded in a JSP page. Scriptlet is all that is inside the tag, where the user can add any valid scriplet between the label and the label.

113. Where is the declaration (decalaration)?

A declaration is similar to a variable declaration in Java, and it is used to declare a variable that is subsequently used by an expression or scriptlet. The added declaration must be wrapped with a start and end tag.

114. What is an expression (Expression)?

"The list is very long and can be published in, on, and below."

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

115. What does the implied object mean? What hidden objects are there?

The JSP implied object is some Java object in the page, and the JSP container makes these Java objects available to the developer. Developers can use them directly without a clear statement. The JSP implied object is also called a predefined variable. The hidden objects in the JSP page are listed below:

    1. Application
    2. Page
    3. Request
    4. Response
    5. Session
    6. exception
    7. Out
    8. Config
    9. PageContext

The above is the Java interview title of the data collation, thank you for your support of this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.