Explore the ten most common interview questions in Java

  • 2020-04-01 02:05:06
  • OfStack

First, talk about the final, finally, finalize distinction.
The final? Modifier (keyword) if a class is declared final, it cannot be subclassed or inherited as a parent. So a class cannot be declared both abstract and final. Declaring variables or methods as final ensures that they are not changed in use. Variables that are declared final must be given an initial value at the time of declaration, and can only be read in later references, not modified. Methods that are declared final are also available only and cannot be overloaded
The finally? Provide a finally block to perform any cleanup on exception handling. If an exception is thrown, the matching catch clause is executed, and control goes into a finally block, if any.
Finalize? The method name. Java technology allows you to use the finalize() method to do the necessary cleanup before the garbage collector clears the object from memory. This method is called on the object by the garbage collector when it determines that the object is not referenced. It is defined in the Object class, so all classes inherit it. A subclass overrides the finalize() method to tidy up system resources or do other cleanup. The finalize() method is called on an object before it is deleted by the garbage collector.

Second, the difference between a HashMap and a Hashtable.
A class that is part of the Map interface that maps unique keys to specific values.
The HashMap class has no sorting or sorting. It allows a null key and multiple null values.
Hashtable is similar to a HashMap, but does not allow for null keys and values. It is also slower than a HashMap because it is synchronous.

Third, String s = new String("xyz"); How many String objects are created?
Two objects, one is "xyx" and one is the reference object s pointing to "xyx".

Fourth, what's the difference between sleep() and wait()? A favorite thread
The sleep() method is a way to stop the thread for a period of time. The thread does not necessarily resume execution immediately after the sleep interval expires. This is because at that point, other threads may be running and not scheduled to abort execution, unless (a) the thread that "wakes up" has a higher priority
(b) the running thread is blocked for other reasons.
A wait() is a time when threads interact, and if a thread makes a wait() call to a synchronous object x, that thread pauses and the called object enters a wait state until it is awakened or the wait time is up.

Fifth, short s1 = 1; S1 = s1 + 1; What's wrong? Short s1 = 1; S1 + = 1; What's wrong?
Short s1 = 1; S1 = s1 + 1; No, s1 is short, s1+1 is int, so you can't explicitly convert to short. It can be modified as s1 =(short)(s1 + 1). Short s1 = 1; S1 plus 1 is correct.

Sixth, the difference between Overload and Override. Overloaded methods can change the type of return value?
Rewriting method Overloading and Overriding Overloading is a Java polymorphism of different performance. Rewrite the Overriding is one of the polymorphic between parent and child, Overloading Overloading is a class a polymorphism in performance. If a method defined in a subclass and its parent class have the same name and parameters, we say that this method be rewritten (Overriding). When an object in a subclass USES this method, it invokes the definition in the subclass, to which the definition in the parent class is "masked." If multiple methods with the same name are defined in a class that either have different number of arguments or have different parameter types, it is called Overloading of the method. Overloaded methods can change the type of return value.

Seventh, the elements in a Set cannot be repeated, so what is the way to distinguish between duplicates? Equals or equals()? What's the difference?
Elements in a Set cannot be duplicated, so use the iterator() method to distinguish between duplicates. Equals () is to determine whether two sets are equal.
The equals() and == methods determine whether the reference value to the same object equals() is overridden in the class in order to return the true value if the contents and types of the two separate objects match.

Eighth, what is the difference between error and exception?
An error indicates a serious problem where recovery is not impossible but difficult. For example, memory leaks. It is impossible to expect a program to handle such a situation.
Exception represents a design or implementation problem. That is, it represents a situation that would never have happened if the program had been running properly.

Ninth, give me the runtime exception that you see most often.
ArrayStoreException ArithmeticException, BufferOverflowException BufferUnderflowException, CannotRedoException, CannotUndoException, ClassCastException, CMMException, ConcurrentModificationException, DOMException EmptyStackException, IllegalArgumentException, IllegalMonitorStateException, IllegalPathStateException, an IllegalStateException,
ImagingOpException IndexOutOfBoundsException, MissingResourceException NegativeArraySizeException, NoSuchElementException, NullPointerException, ProfileDataException, ProviderException, SecurityException RasterFormatException, SystemException UndeclaredThrowableException, UnmodifiableSetException, UnsupportedOperationException

Tenth, the elements in a Set can't be repeated, so what's the way to tell if they're repeated or not? Equals or equals()? What's the difference?
Elements in a Set cannot be duplicated, so use the iterator() method to distinguish between duplicates. Equals () is to determine whether two sets are equal.
The equals() and == methods determine whether the reference value to the same object equals() is overridden in the class in order to return the true value if the contents and types of the two separate objects match.  


Related articles: