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


Juejin. Cn/post / 684490…

21~50题答案 : juejin.cn/post/684490…

51~95题 干 : juejin.cn/post/684490…



Java Interview Questions (2)

Directions: For this part, you are allowed 30 minutes to write a passage
Juejin. Cn/post / 684490…



答案 21~50

Juejin. Cn/post / 684490…




Java Interview Questions (2)

No difference between ArrayList and LinkedList.

What are the two ways to sort sets?

53, How to print an array in Java?

54, Is LinkedList in Java unidirectional or bidirectional?

55, What tree is used to implement TreeMap in Java? (the answer)

56. How is Hashtable different from HashMap?

57, How does a HashSet in Java work internally?

58. Write code to remove an element while iterating through an ArrayList.

59. Can we write a container class ourselves and then use the for-each loop code?

60. Default size for ArrayList and HashMap is majority?

61. Is it possible for two unequal objects to have the same HashCode?

62, Can two identical objects have different hash codes?

63. Can we use random numbers in hashCode ()?

What is the difference between a Comparator and Comparable in Java?

In my Java program, I have three sockets, how many threads do I need to process?

67, How to create a ByteBuffer in Java?

In Java, how to read and write ByteBuffer?

Is Java big-endian or small-endian?

70. What is the byte order in ByteBuffer?

71. What is the difference between direct buffers and indirect buffers in Java?

72. What is a memory mapped cache in Java?

73, What does the socket option TCP NO DELAY refer to?

What is the difference between TCP and UDP?

75. What is the difference between ByteBuffer and StringBuffer in Java? (the answer)

In Java, what best practices do you follow when writing multithreaded programs?

Name a few best practices for using Collections in Java

78. Name at least five best practices for using threads in Java.

79. Name 5 IO best Practices (Answer)

80. List five JDBC best practices that should be followed



Questions 21 to 50 have been updated

No difference between ArrayList and LinkedList.

The most obvious difference is that the underlying data structure of ArrrayList is an array, which supports random access, while the underlying data structure of LinkedList, a book list, does not. Using subscripts to access an element, the time complexity of ArrayList is O(1) and LinkedList is O(n).

What are the two ways to sort sets?

You can use ordered Collections, such as TreeSet or TreeMap, or you can use ordered Collections, such as list, and sort through collections.sort ().

53, How to print an array in Java?

You can print Arrays using the arrays.tostring () and arrays.deepToString () methods. Because Arrays don’t implement toString(), passing Arrays to system.out.println () won’t print out the contents of the array, but arrays.tostring () will print each element.

54, Is LinkedList in Java unidirectional or bidirectional?

Is a bidirectional linked list, you can check the JDK source. In Eclipse, you can use the shortcut Ctrl + T to open the class directly in the editor.

55, What tree is used to implement TreeMap in Java?

TreeMap in Java is implemented using red-black trees.

56. How is Hashtable different from HashMap?

There are many differences between the two classes, some of which are listed below:

A) Hashtable is a legacy of JDK 1, while HashMap was added later.

B) Hashtable is synchronous and slow, but HashMap has no synchronization policy, so it is faster.

C) A Hashtable does not allow an empty key, but a HashMap allows a null key.

57, How does a HashSet in Java work internally?

The inside of a HashSet is implemented using a HashMap. Since maps require keys and values, all keys have a default value. Similar to a HashMap, a HashSet does not allow duplicate keys. Only one NULL key is allowed, meaning that only one NULL object can be stored in a HashSet.

58. Write code to remove an element while iterating through an ArrayList.

The key to this question is whether the interviewer uses the remove() method of ArrayList or Iterator. Here’s a sample code, is to use the right way to realize in the process of traversing remove elements, and won’t appear abnormal ConcurrentModificationException sample code.

59. Can we write a container class ourselves and then use the for-each loop code?

Yes, you can write your own container class. If you want to iterate with Java’s enhanced loops, you just need to implement the Iterable interface. If you implement the Collection interface, you have this property by default.

60. Default size for ArrayList and HashMap is majority?

In Java 7, the default size for ArrayList is 10 elements, and the default size for HashMap is 16 elements (which must be a power of 2). This is the code snippet for the ArrayList and HashMap classes in Java 7:

// From arrayList. Java JDK 1.7 private static final int DEFAULT_CAPACITY = 10; //from HashMap.java JDK 7 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16Copy the code

61. Is it possible for two unequal objects to have the same HashCode?

It is possible that two objects that are not equal may have the same HashCode value, which is why there are conflicts in the HashMap. The rule for equal Hashcode values simply says that if two objects are equal, they must have the same Hashcode value, but there is nothing about unequal objects.

62, Can two identical objects have different hash codes?

No, according to Hash Code, that’s not possible.

63. Can we use random numbers in hashCode ()?

No, because the objects’ HashCode values must be the same. See the answer for more on overriding the hashCode() method in Java.

What is the difference between a Comparator and Comparable in Java?

The Comparable interface is used to define the natural order of objects, while the Comparator is typically used to define user-customized orders. Comparable is always only one, but you can have more than one comparator to define the order of objects.

65. Why do I need to override hashCode when overriding equals?

Because there is a mandatory specification to override both hashcode and equal methods, many container classes, such as HashMap and HashSet, rely on hashcode and equals.

In my Java program, I have three sockets, how many threads do I need to process?

It depends on whether you’re doing it in parallel or in serial.

67, How to create a ByteBuffer in Java?

byte[] bytes = new byte[10];
ByteBuffer buf = ByteBuffer.wrap(bytes);Copy the code

In Java, how to read and write ByteBuffer?

Is Java big-endian or small-endian?

70. What is the byte order in ByteBuffer?

71. What is the difference between direct buffers and indirect buffers in Java?

72. What is a memory mapped cache in Java?

73, What does the socket option TCP NO DELAY refer to?

What is the difference between TCP and UDP?

75. What is the difference between ByteBuffer and StringBuffer in Java?

In Java, what best practices do you follow when writing multithreaded programs?

A) Give the thread a name to help with debugging.

B) Minimize the scope of synchronization, instead of synchronizing the entire method, only the key parts.

C) If possible, use volatile rather than synchronized.

D) Use higher-level concurrency tools such as BlockingQueue, CountDownLatch, and Semeaphore instead of wait() and notify() for interthread communication.

E) Use concurrent collections in preference to synchronizing collections. Concurrent collections provide better scalability.

Name a few best practices for using Collections in Java

A) Use the right collection class, for example, ArrayList instead of Vector if you don’t need to synchronize lists.

B) Use concurrent collections in preference to synchronizing collections. Concurrent collections provide better scalability.

C) Use interfaces to represent and access collections, such as using List to store ArrayList, using Map to store HashMap, and so on.

D) Use iterators to loop through collections.

E) Use generics when using collections.

78. Name at least five best practices for using threads in Java.

This question is similar to the previous one and you can use the answer above. For threads, you should:

A) Name the thread

B) Separate threads from tasks and use thread pool actuators to perform Runnable or Callable.

C) Use thread pools

Name five IO best practices

IO is critical to the performance of Java applications. Ideally, you should not avoid IO operations on your application’s critical path. Here are some Java IO best practices you should follow:

A) Use buffered IO classes instead of reading bytes or characters individually.

B) Use NIO and NIO2

C) Close the stream ina finally block, or use a try-with-resource statement.

D) Use memory-mapped files to get faster IO.

80. List five JDBC best practices that should be followed

There are many best practices, and you can list them according to your preference. Here are some more general principles:

A) Use batch operations to insert and update data

B) Use preparedStatements to avoid SQL exceptions and improve performance.

C) Use a database connection pool

D) Obtain result sets by column names, not by column subscripts.



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!