Java Common face test questions (including answers)

Source: Internet
Author: User
Tags execution expression final finally block garbage collection goto sleep string
First, talk about final, finally, finalize the difference.
Final modifier (keyword) If a class is declared final, it means that it cannot derive a new subclass and cannot be inherited as a parent class. Thus a class cannot be declared as abstract and declared final. Declaring a variable or method final can guarantee that they will not be changed in use. A variable declared as final must be given an initial value at the time of declaration, and can only be read and not modified in future references. A method that is declared final is also used only and cannot overload
Finally? Provides a finally block to perform any cleanup operations when the exception is processed. If an exception is thrown, the matching catch clause executes, and then the control enters the finally block (if any).
Finalize method name. Java technology allows you to use the Finalize () method to do the necessary cleanup before the garbage collector clears the object out of memory. This method is invoked by the garbage collector on this object when it is determined that the object is not referenced. It is defined in the Object class, so all of the classes inherit it. Subclasses override the Finalize () method to defragment system resources or perform other cleanup work. The Finalize () method is invoked on this object before the garbage collector deletes the object.

Second, Anonymous Inner class (anonymous inner Class) can extends (inherit) other classes, can implements (implement) interface (interface)?
An anonymous inner class is an inner class without a name. You cannot extends (inherit) other classes, but an inner class can be implemented as an interface by another inner class.

Third, Static Nested class and Inner class, the more the more the better (the face of the question some very general).
Nested class (generally C + +), Inner class (generally Java). The most important difference between a Java inner class and a C + + nested class is whether there is a reference to an external point. Concrete Visible http://www.frontfree.net/articles/services/view.asp?id=704&page=1
Note: Static inner class (Inner Class) means that 1 creates an object of a static inner class, does not require an external class object, and 2 cannot access an external class object from an object in a static inner class

The difference between the four,& and the &&.
& is a bitwise operator. && is a boolean logical operator.

Five, the difference between HashMap and Hashtable.
Are classes that are part of the map interface, which enables you to map unique keys to specific values.
HASHMAP classes are not categorized or sorted. It allows a null key and multiple null values.
Hashtable is similar to HASHMAP, but does not allow null keys and null values. It's also slower than HASHMAP, because it's synchronized.

VI, the difference between Collection and collections.
Collections is a Java.util class that contains a variety of static methods for collection operations.
Collection is a Java.util interface, which is the parent interface of various collection structures.


VII, when to use Assert.
An assertion is a statement that contains a Boolean expression, which is assumed to be true when the statement is executed. If the expression evaluates to False, then the system reports a assertionerror. It is used for debugging purposes:
ASSERT (a > 0); Throws an assertionerror if a <= 0
Assertions can be of two forms:
Assert Expression1;
Assert Expression1:expression2;
Expression1 should always produce a Boolean value.
Expression2 can be any expression that results in a value. This value is used to generate a String message that displays more debugging information.
The assertion is disabled by default. To enable assertions at compile time, you need to use the source 1.4 tag:
Javac-source 1.4 Test.java
To enable assertions at run time, you can use-enableassertions or-ea tags.
To choose to disable assertions at run time, you can use-da or-disableassertions tags.
To enable assertions in a system class, you can use-esa or-DSA tags. You can also enable or disable assertions on a package basis.
You can place assertions at any location that is expected to not be reached normally. Assertions can be used to validate parameters that are passed to a private method. However, assertions should not be used to validate parameters that are passed to the public method, because the public method must check its arguments, regardless of whether the assertion is enabled. However, you can test the post condition with assertions in a public method or in a Non-public method. In addition, assertions should not change the state of the program in any way.


What is the GC, VIII? Why should there be a GC? (Basis).
GC is a garbage collector. Java programmers do not have to worry about memory management because the garbage collector is automatically managed. To request garbage collection, you can call one of the following methods:
System.GC ()
Runtime.getruntime (). GC ()

Ninth, String s = new string ("XYZ"), several string Object created?
Two objects, one is "XyX", and one is the reference object s that points to "XyX".

Tenth, Math.Round (11.5) and how much? How much is math.round (-11.5)?
Math.Round (11.5) returns (long) 12,math.round (-11.5) returns (long)-11;

11th, short S1 = 1; S1 = s1 + 1; what's wrong? Short S1 = 1; S1 + 1; what's wrong?
Short S1 = 1; S1 = s1 + 1; wrong, S1 is short, s1+1 is int and cannot be explicitly converted to short type. Can be modified to S1 = (short) (S1 + 1). Short S1 = 1; S1 = 1 correct.

12th, what is the difference between sleep () and wait ()? Thread-loving favorite
The sleep () method is a method that causes a thread to stop for a period of time. After the sleep interval expires, the thread does not necessarily resume execution immediately. This is because at that point, other threads may be running and are not scheduled to abort execution unless (a) the "waking" thread has a higher priority
(b) The running thread is blocked for other reasons.
When wait () is a thread interaction, if a thread makes a waiting () call to a synchronization object x, the thread suspends execution and the object is queued until it wakes up or waits.



13th, does Java have goto?
The reserved word in Goto?java is not now used in Java.

14th, does the array have a length () method? Does string have a method of length ()?
The array has no length () method, and has a property of length.
String has this method of length ().

15th, the difference between overload and override. Can the overloaded method change the type of the return value?
The overridden overriding and overloaded overloading of methods are different manifestations of Java polymorphism. Overriding overriding is a manifestation of polymorphism between parent classes and subclasses, and overload overloading is an expression of polymorphism in a class. If you define a method in a subclass with the same name and parameters as its parent class, we say that the method is overridden (overriding). When an object of a subclass uses this method, the definition in the subclass is invoked, and the definition in the parent class is "masked". If multiple methods with the same name are defined in a class, either with a different number of parameters or with different parameter types, they are called overloads of the Method (overloading). The overloaded method is to change the type of the return value.

16th, the elements in set can not be repeated, then what is the method to distinguish between repetition or not? Is it with = = or equals ()? What's the difference between them?
The elements in set cannot be duplicated, so the iterator () method is used to distinguish between duplicates. Equals () is to interpret whether two sets are equal.
The Equals () and = = methods Determine whether the reference value points to the same object equals () is overridden in the class in order to return true values when the contents and types of the two detached objects match.

17th, give me one of your most common runtime exception.
ArithmeticException, Arraystoreexception, Bufferoverflowexception, Bufferunderflowexception, CannotRedoException, Cannotundoexception, ClassCastException, Cmmexception, 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 an impossible but difficult situation with a serious problem. For example, memory overflow. It is impossible to expect the procedure to handle such a situation.
Exception represents a design or implementation problem. That is, it means that if the program is working correctly, it will never happen.


19th, List, Set, does map inherit from collection interface?
List,set is

Map is not

20th, what is the difference between abstract class and interface?
A class that declares the existence of a method without implementing it is called an abstract class (abstract class), which is used to create a class that embodies certain basic behaviors and declares a method for that class, but it cannot implement the class in that class. Cannot create an instance of an abstract class. However, you can create a variable whose type is an abstract class and have it point to an instance of a specific subclass. Cannot have an abstract constructor or an abstract static method. Subclasses of an abstract class provide implementations for all abstract methods in their parent class, otherwise they are also abstract classes. Instead, implement the method in the subclass. Other classes that know their behavior can implement these methods in the class.
Interface (interface) is a variant of an abstract class. In an interface, all methods are abstract. Multiple inheritance can be achieved by implementing such an interface. All the methods in the interface are abstract, and none have a program body. Interfaces can only define static final member variables. The implementation of an interface is similar to a subclass, except that the implementation class cannot inherit the behavior from the interface definition. When a class implements a special interface, it defines the method (which is given to the program body) of all such interfaces. It can then invoke the method of the interface on any object of the class that implements the interface. Because of an abstract class, it allows you to use the interface name as the type of the reference variable. The usual dynamic binder will be in effect. A reference can be converted to an interface type or transformed from an interface type, and the instanceof operator can be used to determine whether a class of an object implements an interface.

21st, can the method of abstract be static at the same time, can it be native at the same time, can it be synchronized?
Are not

22nd, is the interface inheritable to the interface? Does an abstract class implement (implements) interfaces? Can an abstract class inherit an entity class (concrete Class)?
Interfaces can inherit interfaces. Abstract classes can implement (implements) interfaces, and abstract classes can inherit entity classes, provided that an entity class must have an explicit constructor.

23rd, start a thread with run () or start ()?
Starting a thread calls the start () method so that the virtual processor represented by the thread is operational, meaning that it can be scheduled and executed by the JVM. This does not mean that the thread will run immediately. The run () method can produce flags that must exit to stop a thread.



24th, can the constructor constructor be override?
Constructor constructor cannot be inherited, so overriding cannot be overridden, but can be overloaded overloading.

25th, can you inherit the string class?
The string class is a final class and therefore cannot be inherited.

26th, when a thread enters an synchronized method of an object, can other threads enter 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 the finally {} after this try be executed, when it is executed, before or after it?
Will execute, before return.


28th, Programming question: Calculate 2 times 8 in the most efficient way?
Programmers with a C background are particularly interested in asking such questions.

2 << 3

29th, two object values are the same (x.equals (y) = = true), but can have different hash code, this sentence is correct?
No, there's the same hash code.

30th, when an object is passed as a parameter to a method, this method can change the property of the object and return the result of the change, so is this the value pass or the reference pass?
is a value pass. The Java programming language is passed parameters only by value. When an object instance is passed to a method as a parameter, the value of the parameter is a reference to that object. The contents of an object can be changed in the method being invoked, but the reference to the object will never change.


31st, does the Swtich function on a byte, whether it acts on a long, and whether it acts on a string?
Switch (EXPR1), expr1 is an integer expression. Therefore, the arguments passed to the switch and case statements should be int, short, char, or byte. Long,string can not act on Swtich.

32nd, the programming question: writes a singleton to come out.
The main purpose of Singleton mode is to ensure that only one instance of a class classes exists in a Java application.
General singleton patterns usually have several forms:
The first form: Define a class whose constructor is private, it has a static private class variable that is instantiated when the class is initialized, gets a reference to it through a public getinstance method, and then invokes the method.
public class Singleton {
Private Singleton () {}
Is it strange to define an instance of yourself inside yourself?
Note that this is private only for internal calls
private static Singleton instance = new Singleton ();
This provides a static method for external access to this class, which can be accessed directly from the
public static Singleton getinstance () {
return instance;
}
}
Second form:
public class Singleton {
private static Singleton instance = NULL;
public static synchronized Singleton getinstance () {
This method is better than the previous one, without having to generate objects every time, just for the first time
Generate an instance when using, improve efficiency!
if (instance==null)
Instance=new Singleton ();
return instance; }
}
Other forms:
Defines a class whose constructors are private and all methods are static.
The first form is generally considered to be more secure.


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.