Java---Common basic interview knowledge points

Source: Internet
Author: User
Tags bitwise

A bit of resources on the Internet, to organize a number of common Java basic interview knowledge points, hoping to help students who have just started or are learning.

1. Abstract

Abstraction is about ignoring aspects of a topic that are not related to the current goal, so that you can more fully focus on the aspects that are relevant to the current goal. Abstractions do not intend to understand all of the problems, but simply select one part of them, temporarily without some detail. Abstract includes two aspects, one is the process abstraction, the other is the data abstraction.

2. Inheritance

Inheritance is a hierarchical model of a junction class and allows and encourages the reuse of classes, which provides a way to articulate commonalities.

A new class of objects can be derived from existing classes, a process known as class inheritance. The new class inherits the attributes of the original class, which is called the derived class (subclass) of the original class, and the original class is called the base class of the new class (The parent Class). Derived classes can inherit methods and instance variables from their base classes, and classes can modify or add new methods to make them more suitable for special needs.

3. Encapsulation

Encapsulation is the process and data is surrounded, access to data only through the defined interface.

Object-oriented computing begins with this basic concept that the real world can be portrayed as a series of fully autonomous, encapsulated objects that access other objects through a protected interface.

4. polymorphism

Polymorphism refers to allowing objects of different classes to respond to the same message. Polymorphism consists of parameterized polymorphism and inclusion polymorphism.

Polymorphism language has the advantage of flexibility, abstraction, behavior sharing and code sharing, which solves the problem of application function with the same name.

5. The difference between String and StringBuffer

The Java platform provides two classes: string and StringBuffer, which can store and manipulate strings, which are character data that contain multiple characters. This string class provides a string of values that cannot be changed. The string provided by this StringBuffer class is modified. You can use StringBuffer when you know that character data is going to change. Typically, you can use Stringbuffers to dynamically construct character data.

6. What are the similarities and differences between abnormal operation and general anomaly?

An exception represents an unhealthy state that may occur during a program's run, and a run-time exception that represents an exception that may be encountered in a typical operation of a virtual machine is a common run error.

The Java compiler requires the method to declare a non-runtime exception that might occur, but does not require that a runtime exception that is not caught to be thrown must be declared.

7. Speak the life cycle of the servlet and tell the difference between servlet and CGI

When the servlet is instantiated by the server, the container runs its Init method, the service method is run 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 from CGI is that the servlet is in a server process, it runs its service method in a multithreaded manner, one instance can serve multiple requests, and its instances are generally not destroyed, and CGI generates a new process for each request, which is destroyed after the service is completed. So the efficiency is lower than the servlet.

8, say Arraylist,vector, LinkedList storage performance and Characteristics

Both ArrayList and vectors use arrays to store data, which is larger than the actual stored data in order to add and insert elements, both of which allow the element to be indexed directly by ordinal, but the insertion element involves memory operations such as array element movement, so the index data is fast and the data is inserted slowly. Vector because of the use of the Synchronized method (thread-safe), usually performance is worse than ArrayList, and LinkedList using a doubly linked list for storage, index data by ordinal need to be forward or backward traversal, but when inserting data only need to record the item before and after items can be , so the insertion speed is faster.

10, the difference between & and &&

both & and && can be used as the logical AND operator, representing logic and (and), when the result of an expression on either side of the operator is true, the entire result is true, otherwise the result is false if one of the parties is false.
&& also has a short-circuit function, that is, if the first expression is false, the second expression is no longer evaluated, for example, for an if (str! = null &&!str.equals (")) expression, when STR is NULL, The following expression does not execute, so the nullpointerexception will not appear if you change && to & NullPointerException exception will be thrown. if (x==33 & ++y>0) y grows, if (x==33 && ++y>0) does not grow,
& can also be used as a bitwise operator , when an expression on either side of the & operator is not a Boolean,,& represents bitwise AND operation, and we typically use 0x0f to perform a & operation with an integer to get the minimum 4 bit bits of that integer, for example, 0x31 & 0x0f The result is 0x01. Note: This topic first said the similarities between the two, and the special points of && and &, and cited some classic examples to show that they understand thoroughly deep, practical experience.

11. The difference 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 the HASHMAP allows null (NULL) key value (key), because of non-thread security, the efficiency may be higher than Hashtable.

HashMap allows NULL to be used as a entry key or value, and Hashtable is not allowed.

HashMap hashtable contains method removed, changed to Containsvalue and ContainsKey. Because the contains method is easy to cause misunderstanding.

Hashtable inherits from the dictionary class, and HashMap is an implementation of the map interface introduced by Java1.2.

The biggest difference is that the Hashtable method is synchronize, and HashMap is not, when multiple threads access Hashtable, they do not need to synchronize their methods, and HashMap must provide external synchronization.

The Hash/rehash algorithms used by Hashtable and HashMap are probably the same, so there is no big difference in performance.

12, final, finally, finalize the difference

Final is used to declare properties, methods, and classes, respectively, that the property is immutable, that the method is not overridden, and that the class is not inheritable.
Finally is part of the exception-handling statement structure, which indicates that it is always executed.
Finalize is a method of the object class that, when executed by the garbage collector, calls this method of the reclaimed object, and can override this method to provide garbage collection of other resource recycles, such as closing the file.

13. What is the difference between sleep () and wait ()?

Sleep is a threading class (thread) method that causes this thread to pause execution for a specified time, giving the execution opportunity to other threads, but the monitoring state remains and is automatically restored when it is finished. Calling sleep does not release the object lock.
Wait is a method of the object class that calls the wait method on this object to cause this thread to discard the object lock, enter the waiting lock pool waiting for this object, and only after the Notify method (or Notifyall) is issued for this object the thread enters the object lock pool ready to get the object lock into the running state.

14. The difference between overload and override

Override override and overload overload of the method are different manifestations of Java polymorphism. Overriding Overrid is a representation of polymorphism between a parent class and a subclass, and overloading overload is a representation of polymorphism in a class.

If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (Overrid). When an object of a subclass uses this method, the definition in the subclass is called, and for it the definition in the parent class is "masked". If more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types, which is called a method overload (overloading). The overload method is to change the type of the return value.

15. What is the difference between error and exception?

Error indicates a serious problem in situations where recovery is not impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such situations.

Exception represents a design or implementation issue. That is, it means that if the program runs normally, it never happens.

16. Simple principle and application of exception handling mechanism in Java

When a Java program violates the semantics of Java rules, the Java virtual machine will represent the error that occurred as an exception.

There are 2 cases of violating semantic rules:

One is the semantic check built into the Java class library. For example, array subscript is out of bounds, indexoutofboundsexception is thrown, and NullPointerException is thrown when a null object is accessed.

Another scenario is that Java allows programmers to extend this semantic check, and programmers can create their own exceptions and freely choose when to throw exceptions with the throw keyword. All exceptions are subclasses of java.lang.Thowable.

17, describe the JVM load class file principle mechanism

The loading of classes in the JVM is implemented by ClassLoader and its subclasses, and Java ClassLoader is an important Java Runtime system component. It is responsible for locating and loading classes of class files at run time.

18, List, MAP, set three interfaces, access to elements, what are the characteristics of each?

Lists hold elements in a particular order, and can have repeating elements. Set cannot have duplicate elements, internal ordering. Map saves the Key-value value.

19. Can the switch statement function on a byte, can it function on a long string?

In switch (EXPR1), EXPR1 can only be an integer expression or enumeration constant (larger font), an integer expression can be an int primitive type or an integer wrapper type, because, Byte,short,char can be implicitly converted to int, so, These types, as well as these types of wrapper types, are also available. Obviously, long and string types do not conform to the syntax of switch, and cannot be implicitly converted to int types, so they cannot be used in Swtich statements.

20, 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 of type int, and when assigned 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.

21. Can I store a Chinese character in char type?

Char-type variables are used to store Unicode encoded characters, and the Unicode encoding character set contains Chinese characters, so the char type can of course store Chinese characters. However, if a particular Chinese character is not included in the Unicode encoding character set, then this char variable cannot store this particular character. Supplemental Note: Unicode encoding consumes two bytes, so variables of type char are also two bytes.

Note: The latter part of the answer, although not in the positive answer, but, in order to show their knowledge and performance of their own understanding of the depth of the problem, can answer some relevant knowledge, do sparing, liker.

22. Use the most efficient method to calculate 2 times 8.

2 << 3, because moving a number to the left n bit, is the equivalent of multiplying by 2 of the N-square, then, a number multiplied by 8 to move it to the left 3 bits, and the bit operation CPU directly supported, the most efficient, so, 2 times 8 and the most efficient method is 2 << 3.

23. Can I include more than one class (not an inner class) in a ". Java" source file? What are the restrictions?

There can be multiple classes, but only one public class, and the class name of public must match the file name.

Java---Common basic interview knowledge points

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.