Java interview questions

Source: Internet
Author: User
Tags finally block

Java interview questions

Basic knowledge:
1. simple principles and applications of the Exception Handling Mechanism in C ++ or Java.

When a Java program violates the Java Semantic Rules, the Java Virtual Machine will indicate an error as an exception. There are two types of violation of Semantic Rules. One is the built-in semantic check of the Java class library. For example, if the array subscript is out of the range, indexoutofboundsexception is triggered. When a null object is accessed, nullpointerexception is thrown. Another scenario is that Java allows programmers to extend this semantic check. programmers can create their own exceptions and choose when to use the throw keyword to cause exceptions. All exceptions are subclasses of Java. Lang. thowable.

2. the similarities and differences between Java interfaces and C ++ virtual classes.

Because Java does not support multi-inheritance, it is possible that a class or object must use methods or attributes in several classes or objects respectively. The existing single Inheritance Mechanism cannot meet the requirements. Compared with inheritance, interfaces are more flexible because they do not have any implementation code. After a class implements an interface, this class implements all the methods and attributes in the interface, and the attributes in the interface are under the default state are public static, and all methods are public by default. A class can implement multiple interfaces.

3. Advantages and principles of garbage collection. Two recovery mechanisms are also considered.

A notable feature of Java is the introduction of the garbage collection mechanism, which helps C ++ programmers solve the most troublesome memory management problems, it makes memory management unnecessary for Java programmers when writing programs. Because of the garbage collection mechanism, objects in Java do not have the concept of "Scope", and only objects can be referenced with "Scope ". Garbage collection can effectively prevent memory leakage and effectively use available memory. The garbage collector is usually used as a separate low-level thread to clear and recycle objects that have been killed in the memory heap or are not used for a long time, programmers cannot call the Garbage Collector to recycle an object or all objects in real time. The collection mechanism involves generational replication, garbage collection, marking, and incremental garbage collection.

4. Please state the thread synchronization method you know.

Wait (): puts a thread in the waiting state and releases the lock of the object it holds.

Sleep (): It is a static method that calls a running thread to capture interruptedexception exceptions.

Y (): Wake up a thread in the waiting state. Note that when this method is called, it cannot actually wake up a thread in the waiting state, instead, the JVM determines which thread to wake up, not by priority.

Allnotity (): Wake up all threads in the wait state. Note that it is not a lock for all wake-up threads but a lock for them to compete.

5. Describe the usage and functions of destructor and virtual functions.

6. What is the difference between error and exception?

Error indicates system-level errors and exceptions that the program does not need to handle,

Exception indicates the exceptions that need to be captured or processed by the program.

7. In Java, a class is declared as the final type. What does it mean?

Indicates that the class cannot be inherited. It is a top-level class.

8. Describe your most common programming style.

9. What is the difference between heap and stack.

Stack is a linear set. The operations for adding and deleting elements should be completed in the same segment. The stack is processed as follows.

Heap is an element of stack.

10. If the system needs to use an extra large integer (beyond the long length range), Please design a Data Structure to store this super large number and design an algorithm to implement the extra large integer addition operation ).

Public class bigint ()

{

Int [] arrone = new arrone [1000];

String intstring = "";

Public int [] Arr (string S)

{

Intstring = s;

For (INT I = 0; I <arrone. leght; I ++)

{

11. If you want to design a graphic system, please design basic graphical components (point, line, rectangle, triangle) for simple implementation

12. Let's talk about the differences between final, finally, and finalize.

Final? Modifier (keyword) If a class is declared as final, it means that it cannot generate a new subclass and cannot be inherited as a parent class. Therefore, a class cannot be declared both abstract and final. Declare variables or methods as final to ensure that they are not changed during use. Variables declared as final must be declared with an initial value, which can only be read and cannot be modified in future references. Methods declared as final can only be used and cannot be overloaded.

Finally? Finally blocks are provided for troubleshooting. If an exception is thrown, the matched catch clause is executed, and the control enters the Finally block (if any ).

Finalize? Method Name. Java technology allows you to use the finalize () method to clear objects from the memory before the Garbage Collector clears them. This method is called by the garbage collector when it determines that this object is not referenced. It is defined in the object class, so all classes inherit it. Subclass overwrites the finalize () method to sort system resources or perform other cleanup tasks. The finalize () method is called before the Garbage Collector deletes an object.

13. Can anonymous inner class (anonymous internal class) be extends (inherited) other classes, or implements (implemented) interface (Interface )?

An anonymous internal class is an internal class without a name. It cannot be extends (inherited) other classes, but an internal class can be used as an interface and implemented by another internal class.

 

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

Nested class (generally C ++) and inner class (generally Java ). The biggest difference between Java internal classes and C ++ Nested classes is whether there are external references. For details, see http: // www.frontfree.net/articles/services/view.asp? Id = 704 & page = 1

Note: The static internal class (inner class) means that 1 creates an object of the static internal class and does not need an external class object, 2. You cannot access an external class object from an object of a static internal class.

Fourth, the difference between & and.

& Is a bitwise operator. & Is a Boolean logical operator.

15. Differences between hashmap and hashtable.

 

All belong to the map interface class, which maps the unique key to a specific value.

 

The hashmap class is not classified 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 synchronized.

16. Differences between collection and collections.

Collections is a Java. util class that contains various static methods related to set operations.

Collection is an interface under java. util. It is the parent interface of various collection structures.

17. When to use assert.

An assertion is a statement that contains a Boolean expression. When executing this statement, it is assumed that the expression is true. If the expression is calculated as false, the system reports an assertionerror. It is used for debugging purposes:

Assert (a> 0); // throws an assertionerror if a <= 0

There are two types of assertions:

Assert expression1;

Assert expression1: expression2;

Expression1 should always generate a Boolean value.

Expression2 can be any expression that generates a value. This value is used to generate a string message that displays more debugging information.

Assertions are disabled by default. To enable assertions during compilation, use the source 1.4 flag:

Javac-source 1.4 test. Java

To enable assertions at run time, you can use-enableassertions or-ea flag.

To disable assertions during running, use the-da or-disableassertions flag.

To enable assertions in the system class, you can use the-ESA or-DSA tag. You can also enable or disable Assertion Based on the package.

You can place assertions on any location that is not expected to arrive normally. Assertions can be used to verify parameters passed to private methods. However, assertions should not be used to verify the parameters passed to the public method, because public methods must check their parameters whether or not assertions are enabled. However, you can use assertions to test the post-condition either in a public method or in a non-public method. In addition, assertions should not change the state of the program in any way.

18. What is GC? Why does GC exist? (Basic ).

GC is the garbage collector. Java Programmers don't have to worry about memory management, because the Garbage Collector automatically manages. To request garbage collection, you can call one of the following methods:

System. GC ()

Runtime. getruntime (). GC ()

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

Two objects, one being "xyx" and the other being referenced object s pointing to "xyx.

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

Math. Round (11.5) returns (long) 12, math. Round (-11.5) returns (long)-11;

21, 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; error: S1 is short type, S1 + 1 is int type, cannot be converted to short type explicitly. It can be changed to S1 = (short) (S1 + 1 ). Short S1 = 1; S1 + = 1 is correct.

22. What is the difference between sleep () and wait? Thread favorites

The sleep () method is used to stop a thread for a period of time. After the sleep interval expires, the thread may not resume execution immediately. This is because at that time, other threads may be running and not scheduled to give up execution, unless (a) the thread "wakes up" has a higher priority (B) the running thread is blocked for other reasons.

When wait () is a thread interaction, if the thread sends a wait () call to a synchronization object X, the thread will pause the execution and the called object will enter the waiting state, wait until the wake-up or wait time is reached.

23. Is there a goto in Java?

Goto? Reserved Words in Java, which are not currently used in Java.

 

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

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

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

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.