Java interview Summary

Source: Internet
Author: User
Java interview knowledge points (most of them have been seen, but they are still very useful ~~ Turn to stay)

It's a classic thing, and it's everywhere. First, write the original article:
First, let's talk about the 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.

Second, can anonymous inner class (anonymous internal class) be extends (inherited) other classes, or implements (implemented) interface (Interface )?

It can inherit other classes or complete other interfaces. This method is often used in swing programming.

Third, the difference between static nested class and inner class is that the more you say, the better (the more general the interview questions are ).

Static nested class is an internal class declared as static. It can be instantiated without relying on external class instances. In general, internal classes must be instantiated before they can be instantiated.

Fourth, the difference between & and.

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

Fifth, 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 hashmap allows null key values ), because of non-thread security, the efficiency may be higher than that of hashtable.

Sixth, the difference 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.

7. When to use assert.

1.4 new keywords (syntax) are used to test the Boolean expression status and can be used to debug programs. If the expression is true, the following statement is executed. Otherwise, assertionerror is thrown. In addition, assert: indicates that if the expression is true, the following expression is ignored. Otherwise, the value of the following expression is used as the construction parameter of assertionerror. Note that the-source 1.4 parameter must be added during compilation; otherwise, an error is returned.] Add the-ea parameter during running; otherwise, the assert row is ignored.

Eighth, what is GC? Why does GC exist?
  
GC is the meaning of garbage collection (gabage collection). Memory Processing is a place where programmers are prone to problems. Forgetting or wrong memory collection can lead to instability or even crash of programs or systems, the GC function provided by Java can automatically monitor whether the object exceeded the scope to automatically recycle the memory. the Java language does not provide a display operation to release the allocated memory.

Ninth, string S = new string ("XYZ"); how many string objects are created?

Two

10. How much is math. Round (11.5? Math. Round (-11.5) and so on?

Math. Round (11.5) = 12math. Round (-11.5) =-11round method returns a long integer closest to the parameter. After the parameter is added to 1/2, ask for its floor.

11th, short S1 = 1; S1 = S1 + 1; what is the error? Short S1 = 1; S1 + = 1; what is the error?

Short S1 = 1; S1 = S1 + 1; (S1 + 1 is an int type and requires forced conversion) Short S1 = 1; S1 + = 1; (can be compiled correctly)

12th. 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.

13th. Does Java have a goto?

There are no 13 questions. If any interview asks this question, I suggest you stay away from the company.

14th, does the array have the length () method? Does string have the length () method?

The array does not have the length () method. It has the Length attribute.
String has the length () method.

15th, the difference 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, it calls the definition in the subclass. 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.
16th. The elements in the set cannot be repeated. How can we identify whether the elements are repeated or not? Is = or equals () used ()? What are their differences?

The elements in the set cannot be repeated, so the iterator () method is used to identify whether the elements are repeated or not. Equals () is used to determine whether two sets are equal.
The equals () and = Methods Determine whether the reference value points to the same object equals () is overwritten in the class, in order to return the true value when the content and type of the two separated objects match.

17th. Give me the most common runtime exception.

Refer to the following:
Arithmeticexception,
Arraystoreexception,
Bufferoverflowexception,
Bufferunderflowexception,
Cannotredoexception,
Cannotundoexception,
Classcastexception,
Cmcmexception,
Concurrentmodificationexception,
Domexception,
Emptystackexception,
Illegalargumentexception,
Illegalmonitorstateexception,
Illegalpathstateexception,
Illegalstateexception,
Imagingopexception,
Indexoutofboundsexception,
Missingresourceexception,
Negativearraysizeexception,
Nosuchelementexception,
Nullpointerexception,
Profiledataexception,
Providerexception,
Rasterformatexception,
Securityexception,
Systemexception,
Undeclaredthrowableexception,
Unmodifiablesetexception,
Unsupportedoperationexception

18th, 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 problem. That is to say, it indicates that if the program runs normally, it will never happen.

19th, list, set, and map are inherited from the collection interface?

List, set is
Map is not

20th. What is the difference between abstract class and interface?

The class that declares a method rather than implementing it is called abstract class. It is used to create a class that reflects some basic behaviors and declare a method for this class, however, this class cannot be implemented in this class. You cannot create an abstract instance. However, you can create a variable whose type is an abstract class and point it to an instance of a specific subclass. Abstract constructors or abstract static methods are not allowed. The subclasses of abstract classes provide implementation for all abstract methods in their parent classes. Otherwise, they are also abstract classes. Instead, implement this method in the subclass. Other classes that know their behavior can implement these methods in the class.

An interface is a variant of an abstract class. All methods in the interface are abstract. Multi-inheritance can be achieved by implementing such an interface. All methods in the interface are abstract, and none of them have a program body. The interface can only define static final member variables. The implementation of an interface is similar to that of a subclass, except that the implementation class cannot inherit behaviors from the interface definition. When a class implements a special interface, it defines (to be given by the program body) all the methods of this interface. Then, it can call the interface method on any object that implements the interface class. Because there is an abstract class, it allows the interface name as the type of the referenced variable. Normally, dynamic Association editing will take effect. The reference can be converted to the interface type or from the interface type. The instanceof operator can be used to determine whether the class of an object implements the interface.

21st can abstract methods be both static, native, and synchronized?

None

22nd. Can an interface inherit an interface? Can an abstract class implement the (implements) interface? Can an abstract class inherit a concrete class )?

Interfaces can inherit interfaces. Abstract classes can be implemented (implements) interfaces, and whether abstract classes can inherit object classes, provided that object classes must have clear constructors.

23rd. Is run () or start () used to start a thread ()?

When a thread is started, the START () method is called to make the virtual processor represented by the thread in a running state, which means that it can be scheduled and executed by JVM. This does not mean that the thread will run immediately. The run () method can generate the exit sign to stop a thread.

24th. Can constructor be overwritten?

Constructor cannot be inherited, so overriding cannot be overwritten, but overloading can be overloaded.

25th. Can I inherit the string class?

The string class is a final class, so it cannot be inherited.

26th. After a thread enters a synchronized method of an object, can other threads access other methods of this object?

No. One Synchronized Method of an object can only be accessed by one thread.
27th, there is a return statement in try {}, so will the code in finally {} following this try be executed? When will it be executed, before or after return?

Will be executed, before return.

28th. Programming question: how many equals 2x8 in the most efficient way?

Programmers with a C background are particularly fond of asking such questions.
2 extends collection A collection that does not contain repeated elements. More specifically, set does not contain E1 and E2 elements that meet e1.equals (E2), and contains at most one null element. As its name implies, this interface imitates the set abstraction in mathematics.
Check the document.
22. "Whether the abstract class can inherit the object class, but the premise is that the object class must have a clear constructor. "What does it mean? Who defined it?
27. This is complicated, for example

Public class main {

Public static void main (string [] ARGs ){
System. Out. println (test ());
}

Public static Boolean test (){
Boolean B = false;
Try {

Return B = 4> 3;
} Finally {
System. Out. println (B );
// Return false;
}
}
}
In this example, we can see that the expression after return has been computed, and the Return Statement is suspended. If return is not found in finally, the return statement will be returned.
If return exists in finally, return is returned in finally.
29. No one can meet a noncompliant programmer.

31. What about enumeration?
32. Notes are incorrect.

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.