Java interview question Summary 2 (Continuous updates), java questions

Source: Internet
Author: User
Tags date now

Java interview question Summary 2 (Continuous updates), java questions

1. How to format a date

Answer:
Date now = new Date ();

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");

String formatNow = sdf. format (now );

The formatNow variable is the formatted date.

2. What are several implementation methods for multithreading? There are several synchronous methods. What are them?

Answer: (1) multithreading can be implemented in two ways: Inheriting the Thread class or implementing the Runnable interface.
Java. lang. thread, and override the run () method, put the Thread's execution body into it; implement java. lang. runnable interface to implement its run method and put the thread's execution body into it.

There is no big difference between the two implementation methods. It is easy to implement the method of inheriting the Thread class, but the class that inherits it cannot inherit other classes, so it cannot inherit useful methods of other classes. This problem does not exist when the Runnable interface is used. In addition, this implementation separates the thread subject from the thread object, which is logically clear, we recommend that you use this method more.
(2) There are two ways to implement synchronization: one is the synchronization method and the other is the synchronization code block.

The synchronous method is to add the synchronized keyword before the return type of the method.

The synchronization code block is synchronized (the object to be synchronized is written here ){...}

3. Let's talk about the differences between clustered indexes and non-clustered indexes, as well as their respective advantages and disadvantages.

Answer:
Clustered index: the data stored in the table is stored in the order of indexes, and the retrieval efficiency is higher than that of normal indexes. However, the impact on the addition, modification, and deletion of data is greater than that of non-clustered indexes, the data storage sequence in the table is not affected. The retrieval efficiency is lower than that of clustered indexes, and the impact on data addition, modification, and deletion is small.

4. What are the conditions for deadlock? How to overcome it?

Answer: four conditions for deadlock:

Mutual Exclusion condition: A resource can only be used by one process at a time.

Request and retention conditions: when a process is blocked by requesting resources, it will not release the obtained resources.

Non-deprivation condition: resources obtained by a process cannot be forcibly deprived before they are used.

Cyclic waiting condition: a type of cyclic waiting resource relationship is formed between several processes.

These four conditions are necessary for a deadlock. As long as a deadlock occurs in the system, these conditions must be met. As long as one of the above conditions is not met, no deadlock will occur.

Solution to the deadlock:

A. undo all the deadlocked processes. B. Undo the deadlocked processes one by one until the deadlocked process does not exist. c. Force the resources occupied by the deadlocked processes one by one until the deadlocked process disappears. D. Forcibly deprive other processes of sufficient resources and allocate them to the deadlock process to relieve the deadlock.

5. In network programming of the server, the methods for solving session tracing are as follows:

A. use cookies.

B. Use URL rewriting.

C. Use a hidden form field.

D. None of the above methods can be used independently.

Answer: ABC

6. 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 "Scope" concept, and only object references have "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.

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

8. 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, call one of the following methods: System. gc () Runtime. getRuntime (). gc ()

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

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.