Java basic interview question, Javaweb face question

Source: Internet
Author: User
Tags assert thread class

Questions on Java basic plane

1. Basic Java Type (8) the number of bits and bytes per base type
BYTE 1 byte 8 bit
Short 2 bytes 16 bit
int 4 bytes 32 bits
Long 8 bytes 64 bit
Char 2-byte 16-bit
Float 4 bytes 32 bit
Double 8 bytes 64 bits
Boolean 1 byte 8 bit

2, int and integer difference
Integer is the wrapper class for int, and int is a basic data type of Java
An integer variable must be instantiated before it can be used, and the int variable does not need
An integer is actually a reference to an object, and when new is an integer, it actually generates a pointer to the object, while int stores the data value directly
The default value for integer is the default value of Null,int is 0

3. servlet life cycle
Create a Servlet instance
When the servlet is instantiated, the init () method that invokes the object is initialized
Call the Service () method of the object to process the request and return the processing result, before invoking the service.
To ensure INIT initialization has been successfully executed
When the servlet needs to be freed, call the object's destroy () method to end and release the resource

4. Software development process
Demand analysis
Overview design
Detailed design
Software delivery

5, three layer architecture
UI (Presentation layer): mainly refers to the interface that interacts with the user. Used to receive data entered by the user and to display the data that the user needs after processing.
BLL: (Business Logic Layer): A bridge between the UI layer and the DAL layer. Implement business logic. Business logic includes: validation, calculations, business rules, and so on.
DAL: (Data access Layer): dealing with databases. The main realization of the data increase, delete, change, check. Submits the data stored in the database to the business layer, while saving the data processed by the business layer to the database. (These operations are, of course, based on the UI layer.) The user's needs are reflected to the interface (UI), the UI is reflected to the BLL,BLL to reflect the data to the Dal,dal, and then one by one returns until the user's desired data is fed back to the user.

6, short S1 = 1; S1 = s1 + 1; what's wrong? Short S1 = 1; S1 + = 1; what's wrong?
for short S1 = 1; S1 = s1 + 1; Because the type of the expression is automatically promoted by the s1+1 operation, the result
is an int, and then assigns a value to the short type S1, the compiler reports an error that requires a cast type.
for short S1 = 1; S1 + = 1; since + = is a Java-language-defined operator, the Java compiler will treat it specially, so it compiles correctly.

7, the following statement altogether created how many objects: strings= "A" + "B" + "C" + "D";
String s = "a" + "B" + "C" + "D";
System.out.println (s = = "ABCD");
The result of the final printing should be true.
So just creating an object is equivalent to defining a word "ABCD" directly.
Character string

8. There is a return statement in try {}, then the code in the finally {} immediately after this try will not be executed, when executed, before or after the return?
General answer: Executes before the method returns the call. Accurately, it is executed in the middle of return. 9. Please write down the 5 runtimeexception you are most familiar with.
Conversion exception ClassCastException
Except for 0 exception ArithmeticException
Null pointer exception NullPointerException
Index out-of-bounds exception stringindexoutofboundsexception
Array out-of-bounds exception arrayindexoutofboundsexception

10. What is GC? Why do you have a GC?
GC is the meaning of garbage collection (Gabage Collection), memory processing is where programmers are prone to problems, forgetting or wrong memory recycling can cause program or system instability or even crashes, Java provides The GC feature automatically monitors whether an object exceeds the scope to achieve the purpose of automatically reclaiming memory, and the Java language does not provide a way to release the displayed operation of the allocated memory.

11. When to use Assert
Assertion (assertion) is a common debugging method in software development, which is supported in many development languages. In the implementation, assertion is a statement in the program, which checks a Boolean expression, a correct program must ensure that the value of the Boolean expression is true, if the value is False, the program is already in an incorrect state, assert A warning or exit will be given. In general, assertion is used to ensure the most basic and critical correctness of the program. Assertion inspection is usually turned on during development and testing. To improve performance, the assertion check is usually turned off after the software is released.

12. There are several ways to implement a thread in Java? What keyword modifies the synchronization method? Why is the Stop () and suspend () methods not recommended?
There are two implementation methods, namely, inheriting the Thread class and implementing the Runnable interface.
Using the Synchronized keyword to modify the synchronization method
Against the use of Stop () is because it is unsafe. It unlocks all locks acquired by the thread, and if the object
In an incoherent state, other threads can examine and modify them in that state. The results are difficult to detect
Find out where the real problem lies. The suspend () method is prone to deadlocks.
When calling suspend (), the target line routines stops, but still holds the lock that was acquired before. At this point, no other thread can access the locked resource unless the thread that is "suspended" resumes running. For any thread, if they want to restore the target thread while trying to use any of the locked resources, it will cause a deadlock. So should not be used
Suspend (),
Instead, place a flag in your thread class that indicates whether the thread should be active or suspended.
If the flag indicates that the thread should hang, use wait () to enter the wait state. If the flag indicates that the thread should be restored
Restart the thread with a notify ().

13. What are the similarities and differences between synchronous and asynchronous, and under what circumstances are they used separately? An example is described.
If the data will be shared between threads. For example, the data being written may be read by another thread later, or the data being read may have been written by another thread, then the data is shared and must be accessed synchronously.
When an application calls a method that takes a long time to execute on an object and does not want the program to wait for the method to be returned, it should use asynchronous programming, which is often more efficient in many cases with asynchronous approaches.

14. When a thread enters an synchronized method of an object, does the other thread have access to other methods of this object?
Whether the Synchronized keyword was added before other methods, if not added, it can.
If wait is called internally by this method, you can enter another synchronized method.
If all other methods add the Synchronized keyword and no wait is called internally, you cannot.
If the other method is static, it uses a synchronous lock that is the byte code of the current class and cannot be synchronized with a non-static method because the non-static method uses this.

Javaweb face question

1. Say a Servlet's life cycle?
The Web container loads the servlet, beginning with the life cycle. The servlet is initialized by calling the servlet's init () method. By invoking the service () method implementation, different do*** () methods are called depending on the request. To end the service, the Web container invokes the servlet's Destroy () method.

2. What is the difference between forward () and redirect () in the SERVLET API?
A: The former is only the control in the container of the steering, in the client browser address bar will not show the post-turn address, the latter is a full jump, the browser will get the address of the jump, and resend the request link. This way, you can see the link address after the jump from the address bar of the browser. Therefore, the former is more efficient, when the former can satisfy the need, try to use the forward () method, and, as such, it also helps to hide the actual link.
In some cases, for example, if you need to jump to a resource on a different server, you must use the
Sendredirect () method.

3. What are the built-in objects for JSP?
Request client requests, this request will contain parameters from the Get/post request
Response Web page back to the client's response
The properties of the PageContext Web page are managed here
Session period associated with the request
What the application servlet is doing
Output used to transmit a response
Config Servlet's architectural parts
Page JSP webpage itself
Exception exceptions for error pages, not captured

4. What are the similarities and differences between JSP and Servlet, and what are their connections?
JSP is the extension of servlet technology, which is essentially a simple way for servlets to emphasize the appearance of application.
The JSP is compiled with a "class servlet". The main difference between Servlets and JSPs is that the application logic of the servlet is in the Java file and is completely detached from the HTML in the presentation layer. The case for JSP is that Java and HTML can be combined into a file with a. JSP extension. JSPs focus on Views, and Servlets are used primarily for control logic.

5. How do the various parts of MVC have the technology to implement?
MVC is shorthand for Model-view-controller. The model represents the business logic of the application (implemented through the JAVABEAN,EJB component), the View is the presentation surface of the application (generated by the JSP page), and the controller is the process control that provides the application (typically a Servlet), which uses this design model to apply logic, The process and display logic are divided into different component implementations. These components can be interacted with and reused.

6. How do the various parts of MVC have the technology to implement?
MVC is shorthand for Model-view-controller. The model represents the business logic of the application (implemented through the JAVABEAN,EJB component), the View is the presentation surface of the application (generated by the JSP page), and the controller is the process control that provides the application (typically a Servlet), which uses this design model to apply logic, The process and display logic are divided into different component implementations. These components can be interacted with and reused.

7. Tomcat's Optimization experience
A: Remove the watch for Web. XML and edit the JSP into a Servlet in advance.
With surplus physical memory, increase the memory of the JVM used by Tomcat

How does the 1.Java hashmap work?

HashMap is a key value for the data structure, each key will have a corresponding value, the key is to identify such a value.

HashMap based on the hashing principle, we store and retrieve objects through the put () and get () methods. When we pass a key-value pair to the put () method, it calls the Hashcode () method of the Key object to calculate the hashcode, allowing the bucket position to be found to store the value object. When the object is fetched, the correct key-value pair is found by the Equals () method of the Key object, and then the value object is returned. HASHMAP uses LinkedList to solve the collision problem, and when a collision occurs, the object is stored in the next node of the LinkedList. HashMap stores key-value pairs of objects in each LinkedList node.

2. What is a fail-safe iterator for fast failure?

A fast-failing Java iterator may cause concurrentmodifcationexception to be modified during the iteration of the underlying collection. Fail-Safe as an iteration of a replica that occurs in an instance, no exception is thrown. The fast-failing fail-safe paradigm defines how the system reacts when a fault is encountered. For example, a fast iterator ArrayList for failure and an iterator for fail-safe concurrenthashmap.

What is 3.Java blockingqueue?

Java Blockingqueue is part of a concurrent collection util package. The Blockingqueue queue is a support operation that waits for an element to become available to retrieve, and also to store elements when the space is available.

4. When do I use Concurrenthashmap?

In question 2 We see that Concurrenthashmap is used as an instance of a fail-safe iterator that allows full concurrent retrieval and update. When there is a large number of concurrent updates, Concurrenthashmap can be used at this time. This is very similar to Hashtable, but Concurrenthashmap does not lock the entire table to provide concurrency, so concurrenthashmap performance seems better from this point. So concurrenthashmap should be used when there is a lot of updates.

5. Which list implements the fastest insertion?

LinkedList and ArrayList are another implementation of a different list of variables. The advantage of ArrayList is that the dynamic growth array is well suited for use in situations where the total length is unknown at initial time. The advantage of LinkedList is that it is inserted and deleted in the middle position, and the speed is the fastest.

The LinkedList implements a list interface that allows null elements. Additionally LinkedList provides an additional Get,remove,insert method at the first or the tail of the LinkedList. These operations make the LinkedList available as a stack (stack), queue, or two-way queue (deque).

ArrayList implements a variable-size array. It allows all elements, including null. Each ArrayList instance has a capacity (capacity), which is the size of the array used to store the elements. This capacity automatically increases as new elements are added, but the growth algorithm is not defined. When you need to insert a large number of elements, you can call the Ensurecapacity method before inserting to increase the capacity of the ArrayList to improve insertion efficiency.

The difference between 6.Iterator and Listiterator

Listiterator has the Add () method, you can add objects to the list, and iterator cannot.
Both Listiterator and iterator have the Hasnext () and Next () methods, which can be traversed sequentially, but Listiterator have the hasprevious () and previous () methods to enable reverse (sequential forward) traversal. Iterator is not allowed.

Listiterator can locate the current index position, Nextindex () and Previousindex () can be implemented. Iterator does not have this feature.
Can implement the Delete object, but Listiterator can implement the object modification, the set () method can be implemented. Iierator can only traverse and cannot be modified.

7. What is copyonwritearraylist, and how does it differ from ArrayList?

Copyonwritearraylist is a thread-safe variant of ArrayList, where all mutable operations (add, set, and so on) are implemented by a new copy of the underlying array. Compared to ArrayList it is slower to write because it requires a snapshot of the instance.

Copyonwritearraylist write operations require large-area copy arrays, so performance must be poor, but the read operation because the object and write operation is not the same object, read between the need to lock, the synchronization between read and write only after writing through a simple "=" To point a reference to a new array object, which hardly takes time, so that the read operation is very safe, suitable for use in multi-threading, never concurrentmodificationexception, So copyonwritearraylist is suitable for scenarios where read operations are much larger than write operations, such as caching.

8. The difference between iterators and enumerations

If the interviewer asks this question, his intentions must be to differentiate iterator from the two aspects of enumeration:

Iterator allows the removal of elements from the underlying collection.
The method name of the iterator is standardized.

How does 9.Hashmap sync?

When we need a synchronized hashmap, there are two options:

Use Collections.synchronizedmap (.. ) to synchronize the HashMap.
Using the Concurrenthashmap

The preferred choice between these two options is to use Concurrenthashmap, because we do not need to lock the entire object and get the lock through the Concurrenthashmap partition map.

The difference between 10.IdentityHashMap and HashMap

Identityhashmap is the implementation of the map interface. Different from HashMap, the reference equality is used here.

In HashMap if two elements are equal, then Key1.equals (Key2)

In Identityhashmap if two elements are equal, then key1 = = Key2

Java basic interview question, Javaweb face question

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: 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.