Pitfalls in the Java interview

Source: Internet
Author: User
Tags finally block goto

First, to talk about final, finally, finalize the difference. Most often asked.

Second, Anonymous Inner class (anonymous inner Class) can extends (inherit) other classes, can implements (implement) interface (interface)?

Thirdly, the Static Nested class and the Inner class are different, the more said the better (some of the questions are very general).

The difference between,& and &&. This is a very small question.

The difference between HashMap and Hashtable. Frequently asked.

The difference between the Collection and the collections. You mustn't say that one is singular and one is plural.

VII, when to use the Assert. An API-level technician might ask this.

What's the GC? Why do you have a GC? Basis.

The ninth, string s = new string ("XYZ"), and the creation of several string Object?

Tenth, Math.Round (11.5) how much? Math.Round (-11.5) how much?

11th, 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.

12th, what is the difference between sleep () and wait ()? Engage in threading favorites.

13th, does Java have a goto? Very 13 question, if which interview asks this question, I advise you still not to enter this company.

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

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

16th, the elements in set cannot be duplicated, so what is the method used to distinguish between duplicates or not? Are you using = = or equals ()? What is the difference between them?

17th, give me one of your most common runtime exception. If you don't answer this question, the interviewer will think you have no practical programming experience.

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

19th, List, Set, map inherit from collection interface?

20th, what is the difference between abstract class and interface? Frequently asked.

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

22nd, can interfaces inherit interfaces? is an abstract class achievable (implements) interface? Does an abstract class inherit entity classes (concrete Class)?

23rd, start a thread with run () or start ()?

24th, can the constructor constructor be override?

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

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

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

28th, 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.

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

30th, 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?

31st, whether the Swtich can function on a byte, whether it can function on a long, whether it can work on a string?

32nd, the programming question: Write a singleton out.

Here's the answer.

First, to talk about final, finally, finalize the difference.

final-modifier (keyword) If a class is declared final, it means that it can no longer derive a new subclass and cannot be inherited as a parent class. Therefore, a class cannot be declared abstract and declared final. Declaring variables or methods as final ensures that they are not changed in use. A variable declared as final must be given an initial value at the time of declaration, and can only be read in subsequent references and cannot be modified. A method that is declared final can also be used only and cannot be overloaded

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 the control enters the finally block, if any.

The finalize-method name. Java technology allows the use of the Finalize () method to do the necessary cleanup before the garbage collector clears objects from memory. This method is called by the garbage collector to this object when it determines that the object is not referenced. It is defined in the Object class, so all classes inherit it. Subclasses override the Finalize () method to organize system resources or perform other cleanup work. The Finalize () method is called on the object before the object is deleted by the garbage collector.

Second, Anonymous Inner class (anonymous inner Class) can extends (inherit) other classes, can implements (implement) interface (interface)?

The anonymous inner class is an inner class without a name. Other classes cannot be extends (inherited), but an inner class can be implemented as an interface by another inner class.

Thirdly, the Static Nested class and the Inner class are different, the more said the better (some of the questions are very general).

Nested class (generally C + +), Inner class (usually Java's argument). The biggest difference between a Java inner class and a C + + nested class is whether it has an external reference. Specifically visible http://www.frontfree.net/articles/services/view.asp?id=704&page=1

Note: the static inner Class (Inner Class) means that 1 creates an object of the static inner class, does not require an external class object, and 2 cannot access an external class object from an object of a static inner class

The difference between,& and &&.

& is a bitwise operator. && is a boolean logical operator.

The difference between HashMap and Hashtable.

Are classes that are part of the map interface and implement a unique key mapping to a specific value.

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 is also slower than HASHMAP, because it is synchronous.

The difference between the Collection and the collections.

Collections is a Java.util class that contains a variety of static methods related to collection operations.

Collection is an interface under Java.util, which is the parent interface of various collection structures.

VII, when to use the Assert.

An assertion is a statement that contains a Boolean expression that 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.

Assertions are 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 the system class, you can use the-esa or-DSA tags. You can also enable or disable assertions on a package basis.

You can place assertions at any location that you expect to not arrive normally. Assertions can be used to validate parameters passed to a private method. However, assertions should not be used to validate parameters passed to public methods, because the public method must check its parameters regardless of whether the assertion is enabled or not. However, assertions can be used in public methods or in non-public methods to test the post conditions. In addition, the assertion should not change the state of the program in any way.

What's the GC? Why do you have a GC? (base).

GC is a garbage collector. Java programmers don't 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 ()

The ninth, string s = new string ("XYZ"), and the creation of several string Object?

Two objects, one is "XyX", and the other is a reference object pointing to "XyX" s.

Tenth, Math.Round (11.5) how much? Math.Round (-11.5) how much?

Math.Round (11.5) returns (long) 12,math.round (-11.5) return (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; error, S1 is a short type, s1+1 is an int type, cannot be converted to short type explicitly. Can be modified to S1 = (short) (S1 + 1). Short S1 = 1; S1 + = 1 correct.

12th, what is the difference between sleep () and wait ()? Threading a Favorite

The sleep () method is the method that causes the 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 not being dispatched to abort execution unless (a) the "Wake Up" thread has a higher priority (b) The running thread is blocked for other reasons.

When wait () is a thread interaction, if the thread makes a wait () call to a synchronization object x, the thread pauses execution, and the object goes into a wait state until it wakes up or waits until the time is up.

13th, does Java have a goto?

The reserved words in Goto-java are not currently used in Java.

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

The array does not have the length () method, which has the length property.

String has the length () method.

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 a method are different manifestations of Java polymorphism. Overriding overriding is a representation of polymorphism between a parent class and a subclass, and overloading overloading 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 (overriding). 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 overloaded method is to change the type of the return value.

16th, the elements in set cannot be duplicated, so what is the method used to distinguish between duplicates or not? Are you using = = or equals ()? What is the difference between them?

The elements in the set cannot be duplicated, so use the iterator () method to distinguish between duplicates or not. Equals () is the interpretation of two sets for equality.

The Equals () and = = methods Determine whether the reference value points to the same object Equals () is overwritten in the class so that when the contents and types of the two detached objects match, the truth is returned.

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

19th, List, Set, 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 and does not implement it is called an abstract class, which is used to create a class that embodies some basic behavior, declares a method for that class, but does not implement the class in that class. An instance of the abstract class cannot be created. However, you can create a variable whose type is an abstract class that points to an instance of a specific subclass. Cannot have abstract constructors or abstract static methods. The subclasses of the 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.

An interface (interface) is a variant of an abstract class. In an interface, all methods are abstract. Multiple inheritance can be obtained by implementing such an interface. All the methods in the interface are abstract, without a program body. An interface 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 by the program body) to all such interfaces. It can then invoke the interface's method on any object that implements the interface's class. 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 take effect. A reference can be converted to an interface type or converted from an interface type, and the instanceof operator can be used to determine whether the class of an object implements an interface.

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

Are not

22nd, can interfaces inherit interfaces? is an abstract class achievable (implements) interface? Does an abstract class inherit entity classes (concrete Class)?

Interfaces can inherit interfaces. Abstract classes can implement (implements) interfaces, whether an abstract class can inherit an entity class, but only if the entity class must have an explicit constructor.

23rd, start a thread with run () or start ()?

Starting a thread is calling the start () method so that the virtual processor represented by the thread is in a running state, which means it can be dispatched and executed by the JVM. This does not mean that the thread will run immediately. The run () method can produce a flag that must be exited to stop a thread.

24th, can the constructor constructor be override?

The constructor constructor cannot be inherited, so overriding cannot be overridden, but can be overloaded with overloading.

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

The string class is the final class and cannot be inherited.

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

No, an synchronized method of an object can only be accessed by one thread.

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

Executes before the return.

28th, 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. 2 << 3

29th, two object values are the same (x.equals (y) = = true), but can have different hash code, this sentence is not 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 properties of the object and return the changed result, so is this a value pass or a reference pass?

is a value pass. The Java programming language only passes parameters by value. When an object instance is passed as a parameter to a method, the value of the parameter is a reference to the object. The contents of the object can be changed in the called method, but the object's reference is never changed.

31st, whether the Swtich can function on a byte, whether it can function on a long, whether it can work on a string?

Switch (EXPR1), expr1 is an integer expression. So 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: Write a singleton out.

The main purpose of the singleton mode is to ensure that only one instance of a class is present in a Java application.

The general singleton pattern usually has several forms:

The first form: Defines a class whose constructor is private, and it has a static private class variable that, when the class is initialized, gets a reference to it through a public getinstance method, and then calls the method in it.

public class Singleton {

Private Singleton () {}

Is it strange to define an instance of yourself within 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

public static Singleton getinstance () {

return instance;

}

}

The second form of:

public class Singleton {

private static Singleton instance = NULL;

public static synchronized Singleton getinstance () {

This method is better than the above, and does not have to be generated every time, just the first time

Generate an instance when used, improve the efficiency!

if (instance==null)

Instance=new Singleton ();

return instance; }

}

Other forms:

Defines a class whose constructor is private, and all methods are static.

The first form is generally considered to be more secure.

Hashtable and HashMap

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

HashMap allows NULL to be a entry key or value, and Hashtable does not allow

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

The biggest difference is that the method of Hashtable is synchronize, and HashMap is not, in

When multiple threads access Hashtable, they do not need to implement synchronization for their methods, while HashMap

You must provide external synchronization for it.

Hashtable and HashMap use the same hash/rehash algorithms, so there's no big difference in performance.

Pitfalls in the Java interview

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.