The latest Java interview questions

I compiled a free Java Advanced information, covering Java, Redis, MongoDB, MySQL, Zookeeper, Spring Cloud, Dubbo distributed high concurrency and other tutorials, a total of 30G, needs its own collection.
Portal: https://mp.weixin.qq.com/s/igMojff-bbmQ6irCGO3mqA

51, ExampleA inheritance class Exception, class inheritance ExampleB ExampleA. 
The following code fragment:

try {
throw new ExampleB("b")
} catch(ExampleA e){
System.out.println("ExampleA");
} catch(Exception e){ System.out.println("Exception");
}

 

Ask what the output of this code is executed? 
A: Output: ExampleA. (According to Richter substitution principle [supertype can be used where a certain subtypes can be used], ExampleA grab type of anomaly can grasp ExampleB catch block try block type of exception thrown)

Interview questions - say the operation of the following code. (The source of this problem is to "Java programming ideas" a book)
class Annoyance extends Exception {}
class Sneeze extends Annoyance {}
class Human {
public static void main(String[] args) 
throws Exception {
try {
try {
throw new Sneeze();
} 
catch ( Annoyance a ) {
System.out.println("Caught Annoyance");
throw a;
}
} 
catch ( Sneeze s ) {
System.out.println("Caught Sneeze");
return ;
}
finally {
System.out.println("Hello World!");
}
}
}

 

 

52, List, Set, Map whether inherited from the Collection interface? 
A: List, Set Shi, Map not. Map key-value mapping containers, and Set List with a clear distinction, the fragmented and stored Set of elements does not permit duplicates (mathematics set too), the container is a linear structure List for-value index case access elements.

53, set forth ArrayList, Vector, LinkedList storage performance and characteristics. 
A: ArrayList and Vector data is stored using an array, the array element number is greater than the actual data is stored so as to increase and insert elements, which allow the element directly indexed by serial number, but to insert the element array elements involved in memory operations like move, so index data fast and slow insert data, methods of Vector are modified by the addition of synchronized, so Vector is thread-safe container, but the poor performance than ArrayList, and therefore has a legacy container in Java. LinkedList implemented using a doubly linked list is stored (in the memory by the associated reference memory cell scattered added together to form a linear structure can be indexed by serial number, which compared to a continuous chain storage array storage, memory utilization higher), indexed by serial number before the data needs to traverse to or after, but only before and after the record of this can insert the data item, the insertion speed. Vector belong legacy container (container earlier versions of Java provided in addition, Hashtable, Dictionary, BitSet, Stack , Properties are the legacy container), is no longer recommended, but due to the ArrayList and LinkedListed are non-thread-safe, If the scene with a container of experience operating multiple threads, you can synchronizedList methodological tools category Collections will convert it into thread-safe container before use (this is the application for decorating mode, existing objects will pass another the constructor of a class of objects to create a new enhancement implementation).

NOTE: Properties class vessel left and Stack classes have serious problems in the design, Properties is a special key and key values ​​are mapped string, the design should be associated with one and two which Hashtable generic type parameter is set to String, but in the Java API Properties directly inherited Hashtable, which obviously is the inheritance of abuse. Here multiplexing mode code should Has-A relationship instead of Is-A relationship, on the other hand containers are all tools, tools inherited class itself is a wrong approach, the best way to use tools is Has-A relationship (association) or Use-A relationship (dependency). Similarly, Stack class extends Vector is also incorrect. Sun's engineers have also made such a stupid mistake, people marvel.

54, Collection and Collections of the difference? 
A: Collection is an interface, it is the parent interface Set, List and other containers; Collections is a utility class that provides a series of static methods to assist container operations, these methods include searching for containers, sorting, thread safety etc. Wait.

55, List, Map, Set three interfaces while the access elements, what are the differences? 
A: List a particular index accesses elements, there may be duplicate elements. Set not store duplicate elements (with the object equals () method to distinguish whether to repeat elements). Map save key-value pair (key-value pair) mapping, mapping relationship can be one or many. When the container has Set Map implementation version based on two hash tree storage and sorting access time complexity is O (1) based on the theoretical version stored hash, the version of the tree sort Based insert or delete elements would constitute a sort tree in accordance with the key element or elements (key) and to re-ordering to achieve the effect.

Guess you like

Origin www.cnblogs.com/z1009/p/12120068.html