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


Java Interview Questions (2)

81, Name some best practices for method overloading in Java.

Is SimpleDateFormat thread-safe in a multithreaded environment?

How to format a date in Java? For example, format to ddMMyyyy?

In Java, how to display the time zone in a formatted date?

85, What is the difference between java.util.Date and java.sql.Date?

In Java, how to calculate the difference between two dates?

How to convert a string YYYYMMDD to a date in Java?

How do I test static methods? (the answer)

90, How to use JUnit to test a method exception?

Which unit test library have you used to test your Java programs?

What’s the difference between @before and @beforeclass?

How do I check if a string contains only numbers? The solution

How to write an LRU cache using generics in Java?

Write a Java program to convert byte to long?

How do you invert a string without using a StringBuffer?

In Java, how do I get the highest frequency of words in a file?

How do I check if two given strings are in reverse order?

In Java, how do I print all permutations of a string?

100. In Java, how do I print duplicate elements in an array?

101. How to convert a string to an integer in Java?

How do I exchange the values of two integer variables without using temporary variables?

What is an interface? Why use interfaces instead of directly using concrete classes?

104. What is the difference between abstract classes and interfaces in Java?

105. Besides singletons, what other design patterns have you used in production?

Can you explain Richter’s substitution principle?

107. When can Demeter’s Law be violated? Why is this a problem?

108. What is the adapter mode? When to use it?

109. What are dependency injection and Inversion of control? Why do people use it?

What is an abstract class? How is it different from an interface? Why have you ever used abstract classes?

Which is better, constructor injection or setter dependency injection?

What is the difference between dependency injection and engineering patterns?

What is the difference between adapter mode and decorator mode?

What was the difference between the adapter pattern and the proxy pattern?

115. What is the template method pattern?



Questions 81 to 115 are based on the following table

81, Name some best practices for method overloading in Java.

Here are a few best practices for method overloading that you can follow to avoid the chaos of automatic boxing.

A) Do not overload a method that takes an int and another method that takes an Integer.

B) Do not overload methods with the same number of arguments but different order of arguments.

C) If the number of overloaded method parameters is more than 5, variable parameters are used.

Is SimpleDateFormat thread-safe in a multithreaded environment?

No, unfortunately, all DateFormat implementations, including SimpleDateFormat, are not thread-safe, so you should not use them in multithreaded sequences unless they are used in an external thread-safe environment. For example, restrict SimpleDateFormat to ThreadLocal. If you don’t, you may get incorrect results when parsing or formatting dates. Therefore, I highly recommend the Joda-time library for all practices in date and time handling.

How to format a date in Java? For example, format to ddMMyyyy?

In Java, you can use the SimpleDateFormat class or the Joda-time library to format dates. The DateFormat class allows you to format dates in a variety of popular formats. See the sample code in the answer that demonstrates formatting dates into different formats, such as DD-MM-YYYY or ddMMyyyy.

In Java, how to display the time zone in a formatted date?

SimpleDateFormat is used to format dates, but all DateFormat implementations, including SimpleDateFormat, are not thread-safe, so you should not use them in multithreaded sequences unless they are used in an external thread-safe environment. For example, restrict SimpleDateFormat to ThreadLocal. If you don’t, you may get incorrect results when parsing or formatting dates. Therefore, the Joda-time library is highly recommended for all practices of date and time processing.

85, What is the difference between java.util.Date and java.sql.Date?

In Java, how to calculate the difference between two dates?

How to convert a string YYYYMMDD to a date in Java?

How do I test static methods?

You can use the PowerMock library to test static methods.

90, How to use JUnit to test a method exception?

Use try, catch blocks for code that needs to test for exceptions. Public void testException(){try{long.parselong (null); } Catch (NumberFormatException Expected){}} Then use Junit’s fail function for code that does not throw an expected exception

Which unit test library have you used to test your Java programs?

What’s the difference between @before and @beforeclass?

@before: Execute once Before each test method, the method needs to be declared public

@beforeClass: Execute only once in a class and must be declared public static

How do I check if a string contains only numbers? The solution

Use Java built-in functions, regular expressions, and ASCII codes to determine

How to write an LRU cache using generics in Java?

This is a hybrid data structure, and we need to build a linked list based on the hash table. But Java already provides us with this form of data structure -LinkedHashMap! It even provides a way to override the recycle policy. The only thing we need to note is that the list is changed in the order of insertion, not access. However, one constructor provides an option to use the order of access.

Write a Java program to convert byte to long?

How do you invert a string without using a StringBuffer?

In Java, how do I get the highest frequency of words in a file?

This is an algorithm interview question, Java Chinese more.

1. Store the contents of the file into the String String.


2, use split() to split the string, because you can just replace English Spaces or comma separations, which is similar to Chinese, to split an array.


3. Go through all the words in the number group and count the number of occurrences of key= word and value= word in the result Map.


4. Use TreeSet type to sort the results in Map according to the number of statistics.


5. Output the highest sorted top N result

How do I check if two given strings are in reverse order?

The main idea is, from the beginning of the character, and another from the end of the character, the first judge whether the length is the same, different directly can not reverse text. And then compare.

In Java, how do I print all permutations of a string?

100. In Java, how do I print duplicate elements in an array?

101. How to convert a string to an integer in Java?

String s=”123″;

int i;

I = integer.parseint (s);

I = integer.valueof (s).intValue();

How do I exchange the values of two integer variables without using temporary variables?

Addition and subtraction, multiplication and division, xor

What is an interface? Why use interfaces instead of directly using concrete classes?

Interfaces are used to define apis. It defines the rules that classes must follow. At the same time, it provides an abstraction because the client only uses interfaces, which can have multiple implementations, such as the List interface, where you can use arrayLists that are randomly accessible, or linkedLists that are easy to insert and delete. No code is allowed in interfaces to ensure abstraction, but in Java 8 you can declare static default methods in interfaces that are concrete.

104. What is the difference between abstract classes and interfaces in Java?

There are many differences between abstract classes and interfaces in Java, but one of the most important is that Java limits a class to inheriting only one class, but can implement multiple interfaces. Abstract classes can define the default behavior of a family of classes, while interfaces can define the types better, which helps to implement the polymorphism mechanism later.

105. Besides singletons, what other design patterns have you used in production?

This is based on your experience. In general, you can say dependency injection, factory mode, decorator mode, or observer mode, whichever you’ve used before. But be prepared to answer subsequent questions based on the model you choose.

Can you explain Richter’s substitution principle?

First of all, this is a requirement of the compiler, and if you don’t do this, you won’t be able to compile. Second, in object-oriented programming, the big principle of inheritance is that objects of any subclass can be used as objects of the parent class.

107. When can Demeter’s Law be violated? Why is this a problem?

Demeter’s Rule advises “only talk to friends, not strangers” to reduce coupling between classes.

108. What is the adapter mode? When to use it?

The adapter pattern provides transformations to interfaces. If your client uses some interfaces, but you have other interfaces, you can write an adaptation to connect to those interfaces.

109. What are dependency injection and Inversion of control? Why do people use it?

Inversion of Control (IOC) is the core idea of the Spring framework. In my own words, if you want to do something, don’t try new, just say what you want to do and outsource it. Dependency injection (DI) in my shallow idea is expressed through references to interfaces and constructors. Turn things around and pass them on to where they need to be used

What is an abstract class? How is it different from an interface? Why have you ever used abstract classes?

A. Interfaces for specifications and abstract classes for commonalities.

B. Classes that declare the existence of a method without implementing it are called abstract classes

C. Interfaces are variants of abstract classes. In an interface, all methods are abstract.

Which is better, constructor injection or setter dependency injection?

Each approach has its disadvantages and advantages. Constructor injection ensures that all injections are initialized, but setter injection provides greater flexibility to set optional dependencies. If XML is used to describe dependencies, Setter injection is more readable and writable. The rule of thumb is to force dependencies to use constructor injection and optional dependencies to use setter injection.

What is the difference between dependency injection and engineering patterns?

Although both patterns separate object creation from application logic, dependency injection is cleaner than engineering. With dependency injection, your class is a POJO, and it only knows about dependencies and doesn’t care how they get them. With the factory pattern, your classes need to get dependencies through the factory. Therefore, using DI is easier to test than using factory mode.

What is the difference between adapter mode and decorator mode?

Although the structure of the adapter pattern and the decorator pattern are similar, each pattern appears for a different purpose. The adapter pattern is used to bridge two interfaces, while the decorator pattern is intended to add new functionality to a class without modifying it.

What was the difference between the adapter pattern and the proxy pattern?

This problem is similar to the previous one, the adapter pattern and the proxy pattern differ in their intent. The structure is the same because both the adapter pattern and the proxy pattern encapsulate classes that actually perform actions, but the adapter pattern is used for transformations between interfaces, while the proxy pattern adds an additional middle layer to support allocation, control, or intelligent access.

115. What is the template method pattern?

The template approach provides a framework for algorithms that you can configure or define yourself. For example, you can think of a sorting algorithm as a template. It defines the steps for sorting, but you can use Comparable or something similar in its language for specific comparisons, and the specific policies are up to you to configure. The method of outlining an algorithm is known as the template method.



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!