125 Summary of common Java interview pen questions (1)

Source: Internet
Author: User
Tags float double

1. Abstraction:

Abstraction is to ignore those aspects irrelevant to the current target in a topic, so that you can pay more attention to the aspects related to the current target. Abstraction is not intended to understand all problems
But only select a part of it, and do not need some details for the moment. Abstract: Process abstraction and data abstraction.


2. Inheritance:


Inheritance is a hierarchical model that connects classes and allows reuse of encouraging classes. It provides a way to clearly express commonalities. A new class of an object can be derived from an existing class. This process is called class inheritance. The new class inherits the features of the original class. The new class is called the derived class (subclass) of the original class, and the original class is called the base class (parent class) of the new class ). A derived class can inherit methods and instance variables from its base class, and the class can modify or add new methods to make it more suitable for special needs.

3. encapsulation:

Encapsulation is to enclose the process and data. Data access can only be performed through the defined interface. Object-oriented computing begins with the basic concept that the real world can be depicted as a series of completely autonomous and encapsulated objects that access other objects through a protected interface.

4. Polymorphism:


Polymorphism allows different types of objects to respond to the same message. Polymorphism includes parameterized polymorphism and inclusion polymorphism. Polymorphism language is flexible, abstract, behavior sharing, code
Benefits of sharing
The same name of the application function.

5. Is string the most basic data type?

Basic data types include byte, Int, Char, long, float, double, Boolean, and short. Java
The. Lang. string class is of the final type. Therefore, it cannot be inherited or modified. To improve efficiency and save space, we should use the stringbuffer class

6. What is the difference between int and integer?

Java provides two different types: The reference type and the original type (or the built-in type ). Int Is the original data type of Java, and integer is the encapsulation class provided by Java for int. Java provides encapsulation classes for each original type. The primitive type encapsulation class Boolean char character byte short int integer long float double reference type and primitive type behavior are completely different, and they have different semantics. The reference type and the original type have different features and usage, they include: size and speed problems, which type of data structure this type uses
Storage, which is the default value specified when the reference type and original type are used as instance data of a class. The default value of the instance variables referenced by the object is null, and the default value of the original type of instance variables is related to their types.

7. Differences between string and stringbuffer

The Java platform provides two classes: string and stringbuffer, which can store and operate strings, that is, character data containing multiple characters. This string class provides a string whose values cannot be changed. The stringbuffer class provides strings for modification. You can use stringbuffer when you know that character data is changing. Typically, you can use stringbuffers to dynamically construct character data.

8. What are the similarities and differences between runtime exceptions and general exceptions?

An exception indicates an abnormal state that may occur during the running of the program. An exception indicates an exception that may occur during common operations on the virtual machine. It is a common running error. The Java compiler requires that methods must declare and throw possible non-runtime exceptions, but do not require that they throw uncaptured runtime exceptions.


9. Speak Servlet
And tell the difference between Servlet and CGI.


After the servlet is instantiated by the server, the container runs its init method, runs its service method when the request arrives, and the service method automatically dispatches the doxxx method (doget, dopost) corresponding to the request, etc, the destroy method is called when the server decides to destroy the instance. The difference with CGI is that the servlet is in a server process and runs its service method in multi-thread mode. An instance can serve multiple requests, and its instance is generally not destroyed, CGI generates a new process for each request. After the service is completed, it is destroyed, so the efficiency is lower than that of servlet.

10. Specify the storage performance and features of arraylist, vector, and sorted list.


Both arraylist and vector use arrays to store data. The number of elements in the array is greater than that in the actual data storage to add and insert elements. They allow the element to be indexed by serial number directly, however, inserting elements involves memory operations such as array element movement, so index data is fast and data insertion is slow. Because vector uses the Synchronized Method (thread-safe), its performance is generally inferior to that of arraylist, the sorted list uses a two-way linked list for storage. Data indexed by serial number needs to be traversed in the forward or backward direction. However, when inserting data, you only need to record the items before and after this item, so the insertion speed is fast.

11. What technologies are EJB implemented based on? And the difference between sessionbean and entitybean, and the difference between statefulbean and statelessbean.


EJB includes Session Bean, Entity Bean, and message driven bean, which are implemented based on technologies such as JNDI, RMI, and JAT. Sessionbean in J2EE
Applications are used to complete some server-side business operations, such as accessing the database and calling other EJB components. Entitybean is used to represent the data used in the application system. For clients, sessionbean is a non-persistent object that implements some business logic running on the server. For clients, entitybean is a persistent object, which represents an object view of an object stored in persistent storage, or
The entity implemented by the application. Session Bean can be further subdivided into stateful Session Bean and stateless Session Bean. Both session beans can execute the system logic in method, the difference is that the stateful session bean can record the caller's status. Therefore, a user usually has a corresponding stateful Session Bean entity. Although stateless Session Bean is also a logical component, it is not responsible for recording the user status, that is, when the user calls stateless Session Bean, the EJB container does not find the entity of the specific stateless Session Bean to execute this method. In other words, when several users execute a stateless Session Bean's methods, they will execute the same bean instance. In terms of memory, compared with stateful Session Bean, stateful Session Bean consumes J2EE
Server
 
Large memory, but stateful Session Bean has the advantage that it can maintain the user's status.

12. Differences between collection and collections.


Collection is the upper-level interface of the Collection class. Its inherited interfaces include set and list. collections is a help class for collection classes. It provides a series of static methods for searching, sorting, thread security, and other operations on various sets.


13. Difference Between & and.

& Is a bitwise operator that represents bitwise and operation, & is a logical operator that represents logic and (and ).

14. Differences between hashmap and hashtable.

Hashmap is a lightweight Implementation of hashtable (non-thread-safe implementation). They all complete the map interface. The main difference is that hashmap allows null key values ), because of non-thread security, the efficiency may be higher than that of hashtable. Hashmap allows null as the key or value of an entry, whereas hashtable does not. Hashmap removes the contains method of hashtable and changes it to containsvalue and containskey. The contains method is easy to misunderstand. Hashtable inherits from the dictionary class, while hashmap is an implementation of the map interface introduced by java1.2. The biggest difference is that the hashtable method is synchronize, but hashmap is not. When multiple threads access hashtable, they do not need to implement synchronization for their own methods, hashmap must provide external synchronization for it. The hash/rehash algorithms used by hashtable and hashmap are roughly the same, so there is no big difference in performance.

15. Differences between final, finally, and finalize.

Final is used to declare attributes. Methods and classes indicate that attributes are unchangeable, methods cannot be overwritten, and classes cannot be inherited. Finally is a part of the structure of the exception handling statement, indicating that it is always executed. Finalize is a method of the object class. This method is called when the garbage collector is executed. It can overwrite this method to collect other resources during garbage collection, for example, close a file.

16. What is the difference between sleep () and wait? Sleep is a thread method, which causes the thread to suspend the execution for a specified time and give the execution opportunity to other threads. However, the monitoring status remains unchanged and will be automatically restored after the time. Calling sleep does not release the object lock. Wait is an object-class method. Calling the wait method for this object causes this thread to discard the object lock and enter the waiting lock pool for this object. Only the notify method (or notifyall) is issued for this object) then this thread enters the object lock pool and prepares to get the object lock and enters the running state.


17. Differences between overload and override. Can the overloaded method change the type of the returned value?


Overriding and overloading are different manifestations of Java polymorphism. Overriding is a manifestation of the polymorphism between the parent class and the Child class, and overloading is a manifestation of the polymorphism in a class. If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). When a subclass object uses this method, the definition in the subclass is called. For it, the definition in the parent class is "blocked. If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, it is called overloading ). The overloaded method can change the type of the returned value.

18. What is the difference between error and exception?

Error indicates that recovery is not a serious problem that is impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such a situation. Exception indicates a design.
Or implementation problems. That is to say, it indicates that if the program runs normally, it will never happen.


19. What are the similarities and differences between synchronization and Asynchronization? Under what circumstances should they be used separately? Examples.

If data is shared among threads. For example, if the data being written may be read by another thread or the data being read may have been written by another thread, 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 return of the method, asynchronous programming should be used, in many cases, adopting asynchronous channels is often more efficient.

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.