Java interview Frequently asked Questions + answers

Source: Internet
Author: User
Tags assert bitwise bitwise operators set time

First, Anonymousinnerclass (anonymous inner Class) can extends (inherit) other classes, can implements (implement) interface (interface)?

No, for the anonymous inner class, the words you see are in place:

New < class or interface > < body of class >

This form of the new statement declares a novel anonymous class that expands on a given class or implements a given interface. He also creates a new instance of that class and returns it as a result of the statement.

This is the essence of the anonymous class, which is itself an inheritance of a class or an interface implementation, so inheriting other classes or excuses is meaningless. Smelting in eclipse confirms my idea. Whether inheriting other classes or implementing other excuses displays the syntax error on token.

Second, Staticnestedclass and Innerclass different, said the more the better (some of the questions are very general).

Static nested class refers to statically nested classes, or nested classes, which are commonly used in C + +, and inner class refers to internal classes, which is the term in Java.

The inner class is the general term of a class inner class, which is divided into four kinds: member class, static member class, local class, anonymous class. Where anonymous classes are special cases of local classes. Both the member class and the static member class exist in the top-level code of the class. equals the relationship between the static and non-static methods of the class. The difference is that the member class depends on the class instance and the static member class is not dependent. So the former can only access instance methods and members, while the latter can only access static methods and members. They are all used to create a related only to the current class. Dependency classes unrelated to other classes. Whether static depends on whether an instance of the class is dependent. Local classes are equivalent to local variables. exists in the local code of the class. It is equivalent to arbitrarily defining and using classes in main (). The only difference is that it can only use the final type of local variables. This is related to the garbage collection mechanism. That is, local variables are recycled after the code block ends. The object is not necessarily. So the object can only use final local variables. Similarly, local code blocks that exist in local classes can also have static and non-static differences. The common application of local class crime is anonymous class. Anonymous classes are local classes of nameless children. Often appears in the add-on listener in swing design.

The static nested class is equivalent to that of the inner class.

The difference between the third,& and the &&.

This is a very small question.

& and ^,<<,<<<,| belong to bitwise operators, where & is bitwise AND, for example,,1&1=1,1&0=0,&& is a logical operator that handles true and false values, such as,true& &true=true.

The difference between HashMap and Hashtable.

Frequently asked.

HashMap can be seen as a substitute for Hashtable, HashMap value and key can be null, and Hashtable not, and Hashtable is thread-synchronized, and HashMap is not. Hashtable with iterator traversal, HASHMAP with enumeration traversal. The default size of the hash array in Hashtable is 11, and the increment is old*2+1. The default size of the hash array in HashMap is 16, and must be a 2 index. The method of calculating index is different, Hashtable directly uses Hashcode () to obtain, HashMap to Hashcode recalculate.

The difference between collection and collections.

You mustn't say that one is singular and one is plural.

Collection is the upper interface of the combination class, the sub-interface has list and set, etc., Collections is a tool class under Java.util, and provides some column static methods to synchronize the collection search sort thread.

Six, when to use the Assert.

An API-level technician might ask this.

Used in debugging and optimization to determine whether a logical expression is true or false, and if it is fake, terminates the program and returns an exception. The advantage of assert over if is that you can choose to turn on the Assert check, which means that the test code generated by the assert can be ignored at the time of the official release, which brings great convenience, The default assert check is off. In addition, JUnit has similar but more powerful testing capabilities than assert.

What is a GC?


GC is garbage collection (garbage collection), is the Java used to reclaim memory of a way, the main implementation method has reference count, tag recycling, replication cleanup, etc., GC can avoid memory leaks and stack overflow, effectively improve the efficiency of memory utilization, It also frees programmers from tedious memory management.

The string S=new string ("xyz"); How many stringobject are created?

2, 1 in the text pool, 1 in the heap, first create 1 "XYZ" objects in the text pool, and then copy an "XYZ" object in the heap and assign it to the reference S.

Ninth, Math.Round (11.5) How much? Math.Round (-11.5) how much?

Math.Round (x) equals (int) Math.floor (x+o.5f),

So Math.Round (11.5) =math.floor (11.5+0.5) =math.floor (12) = 12;

Math.Round ( -11.5) =math.floor (-11) =-11;

Tenth, short s1=1;s1=s1+1; what's wrong? Short s1=1;s1+=1; What's wrong?

Interview questions are very sick, to be prepared for abuse.

S1=s1+1 error, s1+1 is int, int cannot be assigned to S1. You need to display the conversion, s1= (int) (s1+1). And s1+=1 will not go wrong, as for the reason, some people say and the compiler mechanism, need to see the compiler principle, say the compiler principle of what the most annoying, and so on.

11th, what is the difference between sleep () and wait ()?

Engage in threading favorites.

1,sleep () is a static method in Java.lang.Thread, and wait () is a method in Java.lang.Object;

2,sleep () is used as the current thread to block itself, and resumes after the set time, and wait () is used for the current thread to determine other threads blocking, which is the performance of thread communication;

3,sleep () does not release resources, wait () releases resources;

4,sleep () must catch an exception, and wait () is not required.

12th, does Java have a goto?

Very 13 question, if which interview asks this question, I advise you still not to enter this company.

Goto is a Java keyword, but Java does not support Goto,goto, which can affect the readability of the program and make the logic seem confusing. My Java tutorial said that for the same reason, to minimize the use of continue and break, and can use the equivalent of no continue/break loop instead, not to mention Goto ... In a word, Java does not support Goto.

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

Array is a member variable, length. String is the member function length (); I do not know what the meaning of this question, usually use eclipse, will care about this ...

14th, the difference between overload and override. Can the overloaded method change the type of the return value?

Frequently asked.

I wrote an essay detailing the relationship between overloads, overrides, and polymorphism, and the return value type is not included in the function signature, so only overloads that have different return values are not true.

15th, the elements in set cannot be duplicated, so what is the method used to distinguish between duplicates or not? Is it a hashcode () or Equals ()? What's the difference?

This is a lookup mechanism problem of the collection class, in the collection class, to determine whether two elements are the same, is compared with the Equals method, the existence of hashcode is that the element can be quickly allocated an index to store. Collection can be seen as a lot of large boxes, index is the number of boxes, the first thing to be thrown into the hash to determine the index, throw into the response box, and then drink the box of other things equals () to compare whether the same. In addition Equal items must have the same hashcode, and items of unequal are not necessarily. Elements with the same hashcode are not necessarily equal. The elements of different hashcode certainly ranged. The above rules can imagine the situation where equal items must be divided in the same box.

16th, give me one of your most common runtimeexception.

If you don't answer this question, the interviewer will think you have no practical programming experience.

NullPointerException, null reference exception. To tell the truth, there is this in the soft written question, I even understand the meaning of the topic is wrong, did not recognize that runtime exception is the runtime exception.

17th, what is the difference between error and exception?

Why is the water problem most likely to appear?

Feel this problem is very water, to say C + + words have a difference, feel Java difference is not big. Anyway, in the soft topic I was talking nonsense. Simply speaking from a literal point of view, error is a serious error, such as some bugs generated by the JVM, which will stop the program from running or produce incorrect results. Exception is a somewhat predictable error that can be captured and processed. In fact, error can also be captured ...

18th, does list,set,map inherit from the collection interface?

Collection sub-interfaces include Beancontext, Beancontextservices, Blockingqueue<e>, List<e>, Queue<e>, Set<E >,sortedset. Map not included

Almost a Web page to be fooled, or Java Doc Comparative professional.

19th, what is the difference between AbstractClass and interface?

Frequently asked.

Really is often asked ...

Abstract classes can have abstract methods and common methods, or they can have their own data members. Interfaces only allow constants, abstract methods, and static class members. Interfaces can be inherited more, abstract classes do not. When an interface is implemented, all methods must be overridden. When an abstract class is inherited, if an abstract method is not overridden, the subclass is also an abstract class.

20th, whether the abstract method can be static at the same time, whether it can be native at the same time, can it be synchronized?

The Abstact method requires a subclass rewrite, and the overriding concept is that it cannot be static in terms of instance methods. For the same reason, to rewrite can not be native, abstract method in the abstract class, no instance, cannot be called, there is no content, so synchronized meaningless. The modifiers for the abstract method are private and public only.

21st, can interfaces inherit interfaces? Can an abstract class implement an (implements) interface? Does an abstract class inherit entity classes (Concreteclass)?

An interface can inherit an interface, an abstract class can implement an interface, and an abstract class can inherit an entity class. In other words, abstract classes are no different than ordinary classes, except that they cannot be instantiated.

22nd, start a thread with run () or start ()?

With start (), Start () implements multiple processes by calling run ().

23rd, can the constructor constructor be override?

Constructors cannot be overridden.

24th, is it possible to inherit the string class?

The string class is final and cannot be inherited.

25th, when a thread enters an synchronized method of an object, does the other thread have access to other methods of this object?

Multithreaded programming involves not much, the use of the lock is less poor

1 You can enter other non-synchronous methods for this object.

2 cannot enter this object this synchronization method

3 cannot enter this object other synchronization methods

26th, there is a return statement in try{}, then the code in the finally{} immediately following this try will not be executed, when executed, before or after the return?

Executed before return, there is a procedure for proof:

The result is:



Return 1

27th, the programming question: the most efficient way to figure out 2 times 8 and so on?

Programmers with C backgrounds are particularly fond of asking such questions.

28th, two object values are the same (X.equals (y) true), but can have different hashcode, is that right?

This sentence itself is not wrong, equals and hashcode are the methods of object, can be overridden and determined by the programmer's own algorithm, can do the above requirements, but the problem is if the elements in the collection, this will bring some errors, some unpredictable errors, So it is forbidden to do so in collection.

29th, when an object is passed as a parameter to a method, this method can change the properties of the object and return the changed result, so is this a value pass or a reference pass?

Reference passing, there is no doubt that object passing can only be a reference. You can change the content of the reference, but not change the reference itself.

30th, whether the Swtich can function on a byte, whether it can function on a long, whether it can work on a string?

The expression in a switch statement can only be an integer type, that is, it must be INT,CHAR or enumerated type data. Cannot be a Boolean or floating-point type, or even other types of integer data (Byte,short and long).

It should be quite authoritative to extract it from the textbook.

31st, the programming question: Write a singleton out.

Singleton mode is to ensure that a class has only one instance, application-level singleton seems more complex, but the simplest singleton is very well implemented.

Java interview Frequently asked Questions + answers

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.