Common interview questions in big factories, you make low-level mistakes accidentally, the latest Java common interview questions in 2020, basic questions (with detailed answers)

2020 latest Java collection of common interview questions + detailed answers (6)

 

The interview questions have been updated to the sixth module. Recently, I am also inquiring about their interview experience with recent partners in major factories, and strive to let everyone know the latest news and collect more comprehensive interview information. If you want to see the first few collections, you can go to my homepage to find them.

Some of the answers are summarized by myself, and some are collected on the Internet. Don't panic after watching these interviews! If you have more experience, you can share it in the comments. If you have any mistakes, you are welcome to point it out. Please let me know, thank you~

abnormal

61. The difference between throw and throws?

 

Throws is used to declare all the exception information that a method may throw. Throws is to declare the exception but not handle it, but upload the exception, whoever calls it, I will handle it. And throw refers to a specific type of exception thrown.

 

62. What is the difference between final, finally, and finalize?

 

  • Final can modify classes, variables, and methods. Modified classes indicate that the class cannot be inherited, modified methods indicate that the method cannot be rewritten, and modified variables indicate that the variable is a constant and cannot be reassigned.

  • Finally is generally used in the try-catch code block. When dealing with exceptions, we usually must execute the code method in the finally code block, which means that the code block will be executed regardless of whether an exception occurs. It is generally used to store some closed resources Code.

  • Finalize is a method, a method of the Object class, and the Object class is the parent class of all classes. This method is generally called by the garbage collector. When we call the gc() method of the System, the garbage collector calls finalize (),recycle rubbish. 

 

63. Which part of try-catch-finally can be omitted?

 

Answer: catch can be omitted

 

the reason:

A more strict statement is actually: try is only suitable for handling runtime exceptions, try+catch is suitable for handling runtime exceptions + ordinary exceptions. In other words, if you only use try to handle ordinary exceptions without catch processing, the compilation will not pass, because the compiler has a rigid rule that if you choose to catch ordinary exceptions, you must use catch to display the declaration for further processing. And runtime exceptions are not so stipulated at compile time, so catch can be omitted, and you can add the catch compiler to it.
 

In theory, the compiler looks at any code that is not pleasing to the eye and feels that there may be potential problems, so even if you add try to all the code, the code is just a layer of skin on the basis of normal operation during the runtime. But once you add try to a piece of code, it is equivalent to an explicit promise to the compiler to catch exceptions that this piece of code may throw instead of throwing it upward. If it is a normal exception, the compiler requires that it must be caught by catch for further processing; if it is abnormal at runtime, it is caught and then discarded and processed with +finally, or catch is added for further processing.
 

As for adding finally, it is the "finishing" processing that must be performed regardless of whether the exception is caught or not.

 

64. In try-catch-finally, if the catch is returned, will finally be executed?

 

Answer: It will be executed and executed before return.

 

Code example 1:

/*
 * java面试题--如果catch里面有return语句,finally里面的代码还会执行吗?
 */
public class FinallyDemo2 {
    public static void main(String[] args) {
        System.out.println(getInt());
    }

    public static int getInt() {
        int a = 10;
        try {
            System.out.println(a / 0);
            a = 20;
        } catch (ArithmeticException e) {
            a = 30;
            return a;
            /*
             * return a 在程序执行到这一步的时候,这里不是return a 而是 return 30;这个返回路径就形成了
             * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40
             * 再次回到以前的路径,继续走return 30,形成返回路径之后,这里的a就不是a变量了,而是常量30
             */
        } finally {
            a = 40;
        }

//      return a;
    }
}
 

Implementation results: 30

 

Code example 2:

package com.java_02;

/*
 * java面试题--如果catch里面有return语句,finally里面的代码还会执行吗?
 */
public class FinallyDemo2 {
    public static void main(String[] args) {
        System.out.println(getInt());
    }

    public static int getInt() {
        int a = 10;
        try {
            System.out.println(a / 0);
            a = 20;
        } catch (ArithmeticException e) {
            a = 30;
            return a;
            /*
             * return a 在程序执行到这一步的时候,这里不是return a 而是 return 30;这个返回路径就形成了
             * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40
             * 再次回到以前的路径,继续走return 30,形成返回路径之后,这里的a就不是a变量了,而是常量30
             */
        } finally {
            a = 40;
            return a; //如果这样,就又重新形成了一条返回路径,由于只能通过1个return返回,所以这里直接返回40
        }

//      return a;
    }
}
 

Implementation results: 40

Common knowledge points are easy to be overlooked, have you all answered them?

65. What are the common exception types?

 

  • NullPointerException: This exception is thrown when the application tries to access a null object.

  • SQLException: Provides exceptions about database access errors or other error information.

  • IndexOutOfBoundsException: Indicate that a sort index (for example, the sorting of an array, string or vector) is thrown out of range. 

  • NumberFormatException: This exception is thrown when the application tries to convert a string into a numeric type, but the string cannot be converted into an appropriate format.

  • FileNotFoundException: This exception is thrown when an attempt to open the file indicated by the specified path name fails.

  • IOException: This exception is thrown when a certain I/O exception occurs. This class is a general class of exceptions generated by failed or interrupted I/O operations.

  • ClassCastException: This exception is thrown when trying to cast an object to a subclass that is not an instance.

  • ArrayStoreException: An exception thrown when trying to store an object of the wrong type in an object array.

  • IllegalArgumentException: The thrown exception indicates that an illegal or incorrect parameter was passed to the method.

  • ArithmeticException: This exception is thrown when an abnormal operation condition occurs. For example, when an integer is "divided by zero", an instance of this class is thrown. 

  • NegativeArraySizeException: If the application attempts to create an array with a negative size, this exception is thrown.

  • NoSuchMethodException: This exception is thrown when a specific method cannot be found.

  • SecurityException: An exception thrown by the security manager, indicating that there is a security violation.

  • UnsupportedOperationException: This exception is thrown when the requested operation is not supported.

  • RuntimeExceptionRuntimeException: It is the superclass of exceptions that may be thrown during the normal operation of the Java virtual machine.

At last

The content of the interview questions is over here, I hope it will be helpful to everyone.

Finally, I want to say something to you. I have worked for so many years and have interviewed some people for others. Whether it is from the perspective of the interviewer or the leader, in addition to interview skills and experience, great technology and project experience are also their trump cards and confidence. Core technology sharing of first-tier manufacturers

 It took me a long time to sort out some learning materials. What I posted above is the tip of the iceberg in the materials. I hope I can help you! Click to learn together cipher: csdn

                         

  I will share more pure dry goods articles in the follow-up, and hope to really help you. Your support is my biggest motivation!

Guess you like

Origin blog.csdn.net/weixin_50333534/article/details/108784326