2019 Java Interview Questions medium Advanced series 228

Java Interview Questions (1)

The answer to questions 1 to 20 in the first update is analyzed

21~50

51~95

Java Interview Questions (2)

Directions: For this part, you are allowed 30 minutes to write a passage

答案 21~50

答案 51~80

81. What is the meaning of the passage

Java Interview Questions (2)

116. When to use the Visitor pattern?

117. When to use composite mode?

118. What’s the difference between inheritance and composition?

Describe overloading and overwriting in Java.

120. What is the difference between nested public static classes and top-level classes in Java?

121. What is the difference between composition, aggregation, and association in OOP?

Give me an example of a design pattern that meets the open and close principle.

123. What is the difference between abstract Factory pattern and prototype pattern?

What is the difference between a nested static class and a top-level class?

Can you write a regular expression to determine if a string is a number?

127, What is the difference between checked and unchecked exceptions in Java?

Throws (throws) throws (throws

129, Java, Serializable and Externalizable difference?

130. What is the difference between A DOM and SAX parser in Java?

Name three new features in JDK 1.7.

132. Name five new features introduced in JDK 1.8.

133, What is the difference between Maven and ANT in Java?

Questions 116 to 133 are based on the following table

116. When to use the Visitor pattern?

The visitor pattern addresses, but is not directly associated with, adding operations at the inheritance level of a class. This mode adopts the form of double distribution to increase the middle layer.

117. When to use composite mode?

The composite pattern uses a tree structure to show the inheritance relationship between parts and the whole. It allows clients to treat individual objects and object containers in a uniform manner. Use composition mode when you want to show the inheritance of parts to the whole of an object.

118. What’s the difference between inheritance and composition?

Although both allow code reuse, composition is more flexible than inheritance because composition allows you to choose different implementations at run time. Code implemented by composition is also simpler to test than inheritance.

Describe overloading and overwriting in Java.

Overloading and overwriting both allow you to implement different functions with the same name, but overloading is a compile-time activity, while overwriting is a run-time activity. You can override methods in the same class, but only in subclasses. Rewriting must have inheritance.

120. What is the difference between nested public static classes and top-level classes in Java?

Classes can have multiple nested public static classes inside them, but a Java source file can have only one top-level public class, and the top-level public class name must be the same as the source file name.

121. What is the difference between composition, aggregation, and association in OOP?

If two objects are related to each other, they are said to be related. Composition and aggregation are two forms of association in object orientation. A combination is a stronger association than an aggregation. In composition, one object is the owner of another, while aggregation is the use of one object by another. If object A is composed of object B, then B does not exist if A does not exist, but if object A aggregates an object B, then B can exist alone even if A does not exist.

Give me an example of a design pattern that meets the open and close principle.

The open closed principle requires that your code be open for extension and closed for modification. This means that if you want to add a new feature, you can easily add new code without changing the already tested code. There are several design patterns that are based on the open closed principle, such as the policy pattern. If you need a new policy, you just implement the interface, add configuration, and don’t change the core logic. One working example is the collections.sort () method, which is policy-based and follows the open and close principle. You don’t need to modify sort() for new objects. All you need to do is implement your own Comparator interface.

123. What is the difference between abstract Factory pattern and prototype pattern?

Abstract Factory pattern: Typically implemented by the factory method pattern. But a factory often contains multiple factory methods to produce a series of products. This pattern emphasizes that the client code is guaranteed to use only one series of products at a time. When switching to another series of products, switch to a factory class. The prototype pattern: The biggest disadvantage of the factory approach is that there is an equally complex factory class inheritance system for each product class of the inheritance system. Can we put factory methods in the factory class into the product class itself? If so, you can combine the two inheritance systems into one. This is the idea behind the prototype pattern, where the clone factory method returns a copy (either a shallow copy or a deep copy, at the designer’s option). To ensure that the user code can be dynamically bound to generate the required concrete classes by calling Clone with a pointer. These prototype objects must be constructed in advance. Another advantage of the prototype pattern over the factory method pattern is that copying is generally more efficient than construction.

124. When to use the premium mode?

The meta pattern avoids creating too many objects by sharing objects. In order to use the share mode, you need to make sure that your objects are immutable so that you can safely share them. The String pool, the Integer pool, and the Long pool in the JDK are all good examples of using the meta schema.

What is the difference between a nested static class and a top-level class?

A common top-level class has the same source file name as the class name, whereas nested static classes do not have this requirement. A nested class is inside a top-level class, and the name of the top-level class needs to be used to refer to the nested static class, such as HashMap.Entry, which is a nested static class, HashMap, which is a top-level class, and Entry, which is a nested static class.

Can you write a regular expression to determine if a string is a number?

A numeric string can contain only digits, such as 0 through 9 and the beginning of + and -. With this information, you can use the following regular expression to determine whether a given string is a number. Import java.util.regex.Pattern and java.util.regx.matcher first

public Boolean isNumeric(String str){
	Pattern pattern = Pattern.compile("[0-9] *");
	Matcher isNum = pattern.matcher(str);
	if( !isNum.matches() ){
		return false;
	}
	return true;
}Copy the code

127, What is the difference between checked and unchecked exceptions in Java?

The checked exception compiler checks during compilation. For such exceptions, methods enforce handling or declare them via throws clause. One of these cases is a subclass of Exception but not RuntimeException. Unchecked is a subclass of RuntimeException that is not checked by the compiler at compile time.

Throws (throws) throws (throws

Throw is used to throw an instantiated object of the java.lang.Throwable class. This means that you can throw an Error or an Exception using the keyword throw, as in: Throw New IllegalArgumentException(” size must be multiple of 2 “) And throws an exception as part of the method declaration and signature so that the caller can handle it. In Java, any unchecked checked exception is forced to be declared in the THROWS clause.

129, Java, Serializable and Externalizable difference?

Serializable interface Serializable interface is an interface that serializes Java classes so that they can be transferred over the network or their state can be stored on disk. It is the default serialization method embedded in the JVM and is expensive, fragile, and insecure. Externalizable allows you to control the entire serialization process, specify specific binary formats, and add security mechanisms.


130. What is the difference between A DOM and SAX parser in Java?

The DOM parser loads the entire XML document into memory to create a DOM model tree, which makes it faster to find nodes and modify the XML structure, whereas the SAX parser is an event-based parser that does not load the entire XML document into memory. For this reason, DOM is faster and requires more memory than SAX, and is not suited for parsing large XML files.

Name three new features in JDK 1.7.

While JDK 1.7 isn’t as big as JDK 5 and 8, there are a number of new features, such as try-with-resource statements, that allow you to use streams or resources without having to manually close them. Fork-join pooling implements the Java version of Map-Reduce to some extent. Allow String variables and text in Switch. The diamond operator (<>) is used for type inference and eliminates the need to declare generics on the right side of variable declarations, resulting in stronger, cleaner code that can be read and written. Another feature worth mentioning is improved exception handling, such as allowing multiple exceptions to be caught in the same catch block.

132. Name five new features introduced in JDK 1.8.

Java 8 is a groundbreaking release in Java history. Here are five key features in JDK 8:

(1)Lambda expressions that allow anonymous functions to be passed like objects

(2)Stream API, make full use of modern multi-core CPU, can write very simple code

(3)Date and Time API. Finally, there is a stable, simple Date and Time library for you to use

(4) Extended methods, now the interface can have static, default methods.

(5) Repeat annotations. Now you can use the same annotations on the same type multiple times.

133, What is the difference between Maven and ANT in Java?

While both function as build tools for creating Java applications, Maven does more, providing a standard Java project structure based on the concept of convention over configuration while automatically managing dependencies (the JAR files that the application depends on) for the application. Ant is simply a software build tool, while Maven is positioned as a software project management and understanding tool. Maven has the following major functions in addition to Ant:

(1) Use Project Object Model to manage software projects;


(2) More implicit rules are built in to make building files easier;


(3) Built-in dependency management and Repository to achieve dependency management and unified storage;


(4) Built-in lifecycle of software construction;

The last

Welcome everyone to pay attention to my public account [Programmer Chase wind], sorted out 1000 2019 Java interview questions of many companies more than 400 pages of PDF documents, articles will be updated in it, sorted information will also be placed in it.





If you like the article, please remember to like it, thank you for your support!