You must learn to know. The __java of the basic face test of Java

Source: Internet
Author: User
Tags comparable data structures float double garbage collection inheritance serialization thread class

1. What does the "static" keyword mean? Whether a private or static method can be overridden in Java (override).

Answer: the "static" keyword indicates that a member variable or a member method can be accessed without an instance variable of the class to which it belongs.
The static method in Java cannot be overridden because method overrides are dynamically bound based on Run-time, and the static method is statically bound at compile time. The static method is not relevant to any instance of the class, so it is not conceptually applicable.

2. Whether a non-static variable can be accessed in a static environment.

Answer: The static variable belongs to the class in Java, and its value is the same in all instances. The static variable is initialized when the class is entered by Java Virtual aircraft. If your code tries to access a non static variable without an instance, the compiler will complain because the variables have not yet been created and are not associated with any instances.

What data types are supported by 3.Java. What is an automatic disassembly box.

Answer: The basic data types in the Java language Support 8 are:
byte short int long float double Boolean char
Automatic boxing is a transformation of the Java compiler between the base data type and the corresponding object wrapper type. For example: Convert int into integer,double into double, and so on. The opposite is automatic unpacking.

4.Java, what is a constructor. What is a constructor overload. What is a copy constructor.

Answer: When a new object is created, the constructor is invoked. Each class has a constructor. The Java compiler creates a default constructor for this class when the programmer does not provide a constructor for the class.
Constructor overloads and method overloads in Java are very similar. You can create multiple constructors for a class. Each constructor must have its own unique argument list.
Java does not support copy constructors like C + +, which is different because Java does not create a default copy constructor if you do not write the constructor yourself.

5.Java supports multiple inheritance.

Answer: In Java, classes do not support multiple inheritance, only single inheritance (that is, a class has only one parent class). But interfaces in Java support multiple inheritance, that is, a sub-interface can have multiple parent interfaces. (interface is used to extend the function of an object, a sub-interface inherits multiple parent interfaces, stating that the sub-interface extends multiple functions, and when the class implements the interface, the class extends the corresponding functionality).

6. What is the difference between an interface and an abstract class?

Answer: Java provides and supports the creation of abstract classes and interfaces. Their implementation has something in common, and the difference is:
All methods in an interface are implicitly abstract. Abstract classes, in turn, can contain both abstract and non-abstract methods.
A class can implement many interfaces, but can inherit only one abstract class
Classes can not implement all the methods declared by abstract classes and interfaces, and of course, in this case, the class must also be declared abstract.
An abstract class can implement an interface without providing an interface method implementation.
The variables declared in the Java interface are final by default. An abstract class can contain non final variables.
The member functions in the Java interface are public by default. The member functions of an abstract class can be private,protected or public.
Interfaces are absolutely abstract and cannot be instantiated. An abstract class can also not be instantiated, but it can be invoked if it contains the main method.
You can also refer to the difference between an abstract class and an interface in JDK8

7. What is value passing and reference passing.
object is passed by value, meaning that a copy of the object is passed. Therefore, even changing the copy of the object does not affect the value of the source object.
The object is passed by reference, which means that it is not a real object, but a reference to the object. As a result, the external changes to the referenced object are reflected on all objects.
8. What is the difference between process and thread?
A process is an executing application, and a thread is a sequence of execution within a process. A process can have more than one thread. Threads are also called lightweight processes.
9. There are several different ways to create threads. Which one do you like? Why.
There are three ways to create threads:
Inherit Thread class
Implement Runnable interface
Applications can use the executor framework to create thread pools
Implementing the Runnable interface is a more popular approach because it does not need to inherit the thread class. In the case where other objects have been inherited in the application design, this requires multiple inheritance (and Java does not support multiple inheritance) and can only implement interfaces. At the same time, the thread pool is very efficient and easy to implement and use.

10. A general explanation of the several available states of the thread.

1.  New ( new ): A new Thread object was created. 2.  ( runnable ): After the thread object was created, other threads (such as  main  threads) called the object's  start  () method. The thread of this state is in the running thread pool, waiting to be selected by the thread dispatch, to obtain the  cpu  right of use  . 3.  Run ( running ): A Running state ( runnable ) thread obtained  cpu  time slice ( timeslice )  , executing program code. 4.  blocking ( block ): Blocking state refers to the thread for some reason to give up  cpu  right to use, that is, let the  cpu timeslice&nbsp, temporarily stop running. Until the thread enters the operational ( runnable ) state, it has the opportunity to get  CPU timeslice  again to the run ( running ) state. The blocking situation is divided into three kinds: (i) .  wait for blocking: the thread execution  o . wait  () method of running ( running ), jvm  the line Cheng Into the wait queue ( waitting queue ). (ii) .  synchronous blocking: When a thread running ( running ) acquires a synchronized lock on an object, if the synchronization lock is occupied by another thread,  JVM  puts the thread into the lock pool ( lock pool  ). (iii) .  other obstructions: Running ( running ) thread execution  Thread . sleep  ( long ms ) or  t . join  () method, or when a  I / O  request is issued,, jvm  the thread to a blocking state.              when the  sleep  () state times out,  join  () waits for the thread to terminate or timeout, Or the thread is transferred back into the operational ( runnable ) state when the  I / O  process is finished. 5.  Death ( dead ): Thread  run  (),  main  ()   method execution ended, or an exception exited the  run  () method. The thread ends the lifecycle. The thread of death cannot be resurrected again. 11. What is the difference between the sync method and the synchronized code block?
Difference:
The synchronization method defaults to use this or the current class object as a lock;
Synchronized code blocks can choose what to lock, more granular than the synchronization method, we can choose to sync only a part of the problem of synchronization of the code rather than the whole method;
12. Inside the monitor, how to do thread synchronization. What level of synchronization the program should take.
Monitors and locks are used together in a Java virtual machine. Monitor monitors a synchronized block of code, ensuring that only one thread executes the synchronized code block at a time. Each monitor is associated with an object reference. The thread does not allow synchronization code to execute until the lock is acquired.
13. What is a deadlock (deadlock).
A deadlock occurs when two processes are waiting for each other to execute before continuing. The result is that two processes are trapped in infinite waiting.
14. How to ensure that n threads can access n resources without causing deadlocks.
When using multithreading, a very simple way to avoid deadlocks is to specify the order in which locks are acquired and to force threads to acquire locks in the order specified. Therefore, if all threads lock and release locks in the same order, there is no deadlock.
What are the basic interfaces of the 15.Java collection class framework?
The collection class interface specifies a set of objects called elements. Each specific implementation class of a collection class interface can choose to save and sort elements in its own way. Some collection classes allow duplicate keys, some of which are not allowed.
The Java Collection class provides a well-designed set of interfaces and classes that support manipulating a set of objects. The most basic interfaces within the Java Collection class are:
Collection: Represents a set of objects, each of which is its child element.
Set: Collection that does not contain duplicate elements.
List: Sequential collection, and can contain duplicate elements.
Map: Keys can be mapped to values (value) of the object, the key can not be repeated.
16. Why the collection class does not implement the cloneable and serializable interfaces.
The semantics and meaning of cloning (cloning) or serialization (serialization) are related to specific implementations. Therefore, it is up to the concrete implementation of the collection class to decide how to be cloned or serialized.
17. What is an iterator (iterator).
The iterator interface provides many ways to iterate over the elements of a collection. Each collection class contains an instance of an iterator that can return instances of the
Iterative methods. Iterators can delete elements of the underlying collection in the course of an iteration, but you cannot call the collection's
Remove (Object Obj) can be removed by the Remove () method of the iterator.
What is the difference between 18.Iterator and listiterator?
Here is a list of their differences:
Iterator can be used to traverse the set and list collection, but Listiterator can only traverse the list.
Iterator to the set can only be forward traversal, listiterator can be both forward and backwards.
Listiterator implements the iterator interface and includes other functions, such as adding elements, replacing elements, getting the index of the previous and last elements, and so on.
19. What is the difference between rapid failure (fail-fast) and security failure (fail-safe).
Iterator security failure is based on a copy of the underlying collection, so it is not affected by modifications on the source collection. All of the collection classes below the Java.util package are fast failures, and all the classes underneath the Java.util.concurrent package are security failures. A fast-fail iterator throws a Concurrentmodificationexception exception, and a security-failed iterator never throws such an exception.
What is the working principle of the hashmap in 20.Java?
HashMap in Java store elements in the form of key-value pairs (key-value). HashMap requires a hash function that uses the hashcode () and Equals () methods to add and retrieve elements to the collection/from the collection. When the put () method is invoked, HashMap calculates the hash value of the key and then stores the key value pairs on the appropriate index in the collection. If the key already exists, value is updated to the new value. Some of the key features of HashMap are its capacity (capacity), load factor (loading factor), and expansion limit (threshold resizing).
The importance of the 21.hashCode () and Equals () methods is reflected in where.
HashMap in Java Use the hashcode () and Equals () methods to determine the index of key-value pairs, which are also used when the value is obtained from the key. If the two methods are not implemented correctly, two different keys may have the same hash value, and therefore may be considered equal by the collection. Also, these two methods are used to discover duplicate elements. So the realization of these two methods is very important to the accuracy and correctness of hashmap.
What is the difference between 22.HashMap and Hashtable?

HashMap and Hashtable both implement the map interface, so many features are very similar. However, they have the following different points:
HashMap allows keys and values to be null, while Hashtable does not allow keys or values to be null.
Hashtable is synchronized, and HashMap is not. Therefore, HashMap is more suitable for single-threaded environments, while Hashtable is suitable for multi-threaded environments.
HashMap provides a collection of keys that are available for application iterations, so HashMap is fast failing. On the other hand, Hashtable provides an enumeration of keys (enumeration). Hashtable is generally considered to be a legacy class.
23. What is the difference between array and list (ArrayList). When you should use array instead of ArrayList.
The different points of array and ArrayList are listed below:
An array can contain basic types and object types, and ArrayList can only contain object types.
The array size is fixed, and the size of the ArrayList is dynamically variable.
ArrayList offers more methods and features, such as: AddAll (), RemoveAll (), iterator (), and so on.
For basic type data, the collection uses automatic boxing to reduce the coding effort. However, this approach is relatively slow when dealing with a fixed sized base data type.
What is the difference between 24.ArrayList and LinkedList?
Both ArrayList and LinkedList have implemented the list interface, and they have the following different points:
ArrayList is an indexed data interface, with an array at the bottom. It can randomly access elements in O (1) time complexity. In this case, LinkedList stores its data in the form of a list of elements, each of which is linked to its previous and subsequent elements, in which cases the time complexity of finding an element is O (n). Add and delete operations are faster than arraylist,linkedlist inserts, because when an element is added to any location in the collection, you do not need to recalculate the size or update the index as an array.
LinkedList is more memory than ArrayList because LinkedList stores two references for each node, one pointing to the previous element and one to the next.
You can also refer to ArrayList vs. LinkedList.
25.Comparable and comparator interface is what to do. List the differences between them.
Java provides a comparable interface that contains only one CompareTo () method. This method can be sorted for two objects. Specifically, it returns a negative number, 0, and a positive number to indicate that the input object is less than, equal to, greater than the existing object.
Java provides comparator interfaces that contain compare () and Equals () two methods. The Compare () method is used to sort two input parameters, return a negative number, 0, and a positive number indicates that the first argument is less than, equal to, and greater than the second argument. The Equals () method requires an object as a parameter that determines whether the input parameter is equal to the comparator. This method returns true only if the input parameter is also a comparator and the input parameter is the same as the current comparator sort result.
26. What is a Java Priority queue (Priority queue).
Priorityqueue is an unbounded queue based on a priority heap whose elements are sorted in natural order (natural orders). At the time of creation, we can give it a comparer that is responsible for sorting the elements. Priorityqueue do not allow null values because they do not have a natural order, or they do not have any associated comparators. Finally, Priorityqueue is not thread-safe, and the time complexity of team and Out is O (log (n)).
27. Do you know the big O sign (big-o notation)? Can you give examples of different data structures?
The Big O notation describes the size of the algorithm, or how good it is in the worst-case scenario, when the elements inside the data structure increase.
The Big O symbol can also be used to describe other behaviors, such as memory consumption. Because the collection class is actually a data structure, we typically use the Big O notation to choose the best implementation based on time, memory, and performance. Large o notation can give a good description of the performance of a large number of data.
28. How to weigh the use of unordered arrays or ordered arrays.
The best thing about an ordered array is that the time complexity of the lookup is O (log n), and the unordered array is O (n). The disadvantage of an ordered array is that the time complexity of the insert operation is O (n), because the value of the element needs to be moved back to place the new element. Instead, the insertion time complexity of unordered arrays is constant O (1).
What are the best practices for the 29.Java collection class framework?
Choosing the type of collection you want to use correctly depends on the performance of your application, such as: if the size of the element is fixed and you know beforehand, we should use an array instead of a ArrayList.
Some collection classes allow the initial capacity to be specified. Therefore, if we can estimate the number of stored elements, we can set the initial capacity to avoid recalculation of hash values or expansion.
For type safety, the reason for readability and robustness is always to use generics. At the same time, using generics can also avoid run-time classcastexception.
Using the invariant Class (immutable Class) provided by JDK as a key to the map avoids hashcode () and Equals () methods for our own classes.
Interfaces are better than implementations when programming.
If the underlying collection is actually empty, return a set of 0 or an array, and do not return NULL.
What is the difference between the 30.Enumeration interface and the iterator interface?
The enumeration speed is twice times the iterator and consumes less memory. However, iterator is far more secure than enumeration because other threads cannot modify objects in the collection that is being traversed by the iterator. At the same time, iterator allows the caller to delete elements from the underlying collection, which is impossible for enumeration.
What is the difference between 31.HashSet and TreeSet?
HashSet is implemented by a hash table, so its elements are unordered. Add (), remove (), the time complexity of the contains () method is O (1).
TreeSet, on the other hand, is implemented by a tree-shaped structure in which elements are ordered. Therefore, add (), remove (), the time complexity of the contains () method is O (Logn).
What is the purpose of garbage collection in 32.Java? When to do garbage collection.
The purpose of garbage collection is to identify and discard objects that are no longer used to release and reuse resources.
33.SYSTEM.GC () and RUNTIME.GC () will do something.
These two methods are used to prompt the JVM for garbage collection. However, it depends on the JVM to start or defer garbage collection immediately.
When the 34.finalize () method is invoked. What is the purpose of the destructor (finalization).
When the garbage collector (garbage Colector) decides to recycle an object, it runs the Finalize () method of the object but unfortunately in Java, garbage collection may never occur if memory is always sufficient, meaning that filalize () may never be executed , apparently counting on it to do the finishing work is unreliable. So what does Finalize () do? Its main purpose is to reclaim the memory requested by the special channel. Java programs have garbage collector, so the memory problem is usually not bothered by programmers. But there is a jni (Java Native Interface) that invokes the Non-java program (c or C + +), and the Finalize () job is to reclaim this part of the memory.
35. If the object's reference is set to NULL, the garbage collector immediately frees the memory occupied by the object.
No, in the next garbage collection cycle, this object will be recyclable.
What is the structure of the 36.Java heap?

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.