Home  >  Article  >  Java  >  10 Frequently Asked Java Interview Questions

10 Frequently Asked Java Interview Questions

王林
王林forward
2021-02-04 10:20:133037browse

10 Frequently Asked Java Interview Questions

The following are 10 questions that are often asked in recent interviews. They are now compiled and shared for reference.

1. Evaluate yourself on a 10-point scale - How good are you at Java?

If you are not sure about yourself or your proficiency in Java, then this is a very Tough questions. If you are a Java expert, you should keep it low. After this, you may get questions depending on the level you admit to. So, for example, if you said 10 but couldn't answer a fairly difficult question, that would be a disadvantage.

2. Explain the difference between Java 7 and Java 8.

Honestly, there are a lot of differences. Here, if you can list the most important ones, it will be enough. You should explain the new features in Java 8. For a complete list, visit the original website: Java 8 JDK.

The most important thing you should know is:

Lambda expressions, a new language feature, have been introduced in this version. Lambda expressions allow you to treat functions as method parameters or code as data. Lambda expressions allow you to express instances of single-method interfaces (called functional interfaces) more compactly.

Method references provide easy-to-read lambda expressions for methods that already have names.

Default methods allow new functionality to be added to a library's interfaces and ensure binary compatibility with code written for older versions of these interfaces.

Repeating annotations

Provides the ability to apply the same annotation type multiple times to the same declaration or type usage.

Type annotations

Type annotations provide the ability to apply annotations anywhere a type is used, not just in declarations. This feature supports improved code type checking when used with a pluggable type system. 3. Do you know what collection types there are?

Here you should know the most important point:

ArrayList

LinkedList

HashMap

HashSet

After this, you may have some questions like when should you use this particular collection type, what are the benefits compared to other types, how is it stored data, and how the data structures work behind the scenes.

The best approach here is to learn as much as possible about these collection types, as the variety of questions is almost inexhaustible.

4. What methods does the Object class have?

This is a very common question used to determine your familiarity with basic knowledge. These are the methods that every object has: The

Object class, which belongs to the java.lang package, is at the top of the class hierarchy tree. Every class is a direct or indirect descendant of the object class. Every class you use or write inherits an object's instance methods. You are not required to use any of these methods, however, if you choose to do so, you may need to override them with class-specific code.

The methods inherited from the Object discussed in this section are:

    protected Object clone() throws CloneNotSupportedException
  • Create and return a copy of this object.
  • public boolean equals(Object obj)
  • Identifies whether other objects are "equal" to this object.
  • protected void finalize() throws Throwable
  • The garbage collector is called on an object when it determines that there are no references to the object.
  • public final Class getClass()
  • Returns the runtime class of the object.
  • public int hashCode()
  • Returns the hashcode of the object
  • public String toString()
  • Returns the string representation of the object.
  • Object's notify, notifyAll and wait methods all play a role in the activities of independently running threads in the synchronization program. This will be discussed later and will not be discussed here.

There are five methods:

    public final void notify()
  • public final void notifyAll()
  • public final void wait()
  • public final void wait(long timeout)
  • public final void wait(long timeout, int nanos)
  • 5. Why are String objects immutable in Java?

1. Because strings are immutable in Java , to have String pool. In this way, the Java runtime saves a lot of Java heap space because different string variables can reference the same string variable in the pool. If String is not immutable, then String interning is not possible because if any variable changes value, it will be reflected in other variables.

(Recommendations for more related interview questions:

java interview questions and answers

)2. If the string is not immutable, then it will cause serious problems to the application. security threats. For example, the database username and password are passed as strings to get the database connection, socket programming host and port details passed as strings. Because String is immutable, its value cannot be changed. Otherwise, any hacker may change the reference value, causing security issues in the application.

3. Since String is immutable, it is safe for multi-threading, and a single string instance can be shared between different threads. For thread safety, avoid using synchronization; strings are implicitly thread safe.

4. Strings are used in Java class loaders. Immutability provides the security that the correct class is loaded by the class loader. For example, consider an instance where you are trying to load java.sql. The connection class is connected, but the referenced value is changed to myhacking. Connection class, which can perform unnecessary operations on the database.

5. Because String is immutable, its hashcode will be cached when created and does not need to be calculated again. This makes it a good candidate for keys in a map, and it is processed faster than other HashMap key objects. This is why String is the most commonly used object for HashMap keys.

6. What is the difference between Final, Finally and Finalize?

This question is my favorite.

final keyword is used in several contexts to define an entity that can only be allocated once.

Java finally block is a block used to execute important code, such as closing connections, streams, etc. Regardless of whether the exception is handled or not, the Java finally block is always executed. Java finally block follows try or catch block.

Finalize is a method called by the GarbageCollector (garbage collector) before deleting/destroying an object that is eligible for garbage collection to perform cleanup activities.

7. What is the problem with diamond?

The diamond problem reflects why multiple inheritance is not allowed in Java. If there are two classes that have a shared super class with a specific method, then it will be overridden in both subclasses. Then, if you decide to inherit from either subclass, the language has no way of deciding which method you want to call if you want to call it

We call this problem Diamond question. Its name comes from the image above, which depicts the warning.

8. How can you make a class immutable?

I think this is a very difficult problem. You need to make some modifications to your class to achieve immutability:

1. Declare the class as final so that it cannot be extended.

2. Make all fields private so that direct access is not allowed.

3. Do not provide setter methods for variables

4. Make all variable fields final so that their values ​​can only be assigned once.

5. Initialize all fields through the constructor that performs deep copying.

6. Perform object cloning in the getter method to return a copy instead of returning the actual object reference.

9. What does Singleton mean?

Singleton is a class that only allows one instance of itself to be created and provides access to the created instance. It contains static variables that can hold unique and private instances of itself. It can be used when the user wishes to limit the instantiation of a class to one object. This is often helpful when a single object is needed to coordinate operations across systems.

10. What is dependency injection?

This is the first question you must know when working in Java EE or Spring. Inversion of Control (IoC) is a design principle in object-oriented programming that can be used to reduce the coupling between computer codes. The most common method is called Dependency Injection (DI), and the other method is called Dependency Lookup. Through inversion of control, when an object is created, an external entity that controls all objects in the system passes the reference of the object it depends on to it. It can also be said that dependencies are injected into the object.

The component does not perform positioning queries and only provides ordinary Java methods for the container to determine dependencies. The container is solely responsible for the assembly of components. It will pass objects that meet dependencies to the required objects through JavaBean properties or constructors. The practice of injecting dependencies through JavaBean properties is called Setter Injection; the practice of passing dependencies as constructor parameters is called Constructor Injection

Summary

In this article, we discuss the top 10 Java interview questions, which I believe, based on my experience, are the most important questions today. If you know this, I believe you will have a huge advantage in the recruiting process.

If you have had similar experiences with this topic, or if you have some success stories, please share them in the comments below.

Related recommendations: java video tutorial

The above is the detailed content of 10 Frequently Asked Java Interview Questions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete