Java interview question Summary 1

Source: Internet
Author: User

1. Simple Principle and Application of the Exception Handling Mechanism in Java.

When JavaProgramWhen Java's Semantic Rules are violated, 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 implementations.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 died or are not used in the memory heap 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. thread synchronization method.

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 calling this method, it cannot exactly 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. 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.

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

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

8. 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-The Finally block is provided during exception handling to perform any cleanup operations. 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.

9 does anonymous inner class (anonymous internal class) support extends (inherited) other classes and 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.

10 differences between static nested class and inner class
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.
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.

Java interview questions: October 22, 1234

 
 

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

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

13. Differences between collection and collections.

Collection is an interface under java. util. It is the parent interface of various collection structures.
Collections is a Java. util class that contains various static methods related to set operations.

14. 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 and display more debugging
The string message of the 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.

15. 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 ()

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

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

17 what 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;

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

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

20 does Java have a goto?
Reserved Words in GOTO-Java are not currently used in Java.

 

Java interview questions:I II 3. Thu

  • Tags:Java Interview Questions
  • 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.