[interview questions] Java Basics - Summary of common interview questions p3

Prepare for the internship and regularly summarize the interview questions of the regular exam. Let's cheer up! 🎯

Previous articles:

Reference article:

  • https://blog.csdn.net/qq_45966440/category_10889559.html
  • https://blog.csdn.net/weixin_43591980/category_10638797.html?spm=1001.2014.3001.5515
  • https://javaguide.cn/java/basis/java%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E6%80%BB%E7%BB%93/
  • https://pdai.tech/

be careful:

If there are mistakes in this article, please correct them in the comment area! 🍭

1. What are the functions of each part of try catch finally?

  • try block: used to catch exceptions. It can be followed by zero or more catch blocks. If there is no catch block, it must be followed by a finally block

  • catch block: used to handle exceptions caught by try

  • Finally block: the statements in the finally block will be executed whether or not exceptions are caught or handled. When a return statement is encountered in a try or catch block, the finally statement block will be executed before the method returns

👨‍💻 The interviewer asked: will finally be executed?

Not necessarily. finally blocks will not be executed in the following three special cases:

  1. System. Is used in a try or finally block Exit (int) exits the program. However, if system Exit (int) after the exception statement, finally will be executed
  2. The thread on which the program is located dies
  3. Turn off the CPU

👨‍💻 The interviewer continued to ask: which part of ftry catch finally can be omitted?

catch can be omitted.

Try is only suitable for handling runtime exceptions, and try+catch is suitable for handling runtime exceptions + ordinary exceptions.

In other words, if you only try to handle ordinary exceptions without catch, the compilation will not pass, because the compiler has a hard rule that if you choose to catch ordinary exceptions, you must use catch to display the declaration for further processing. The runtime exception is not so specified at compile time, so catch can be omitted

2. Tell me the difference between Error and Exception?

The parent class of Error class and Exception class is Throwable class. The main differences are as follows:

  • Error class

    It indicates that the JVM cannot handle errors, and we can't catch them through catch. For example, Java virtual machine running error, virtual machine memory shortage error, class definition error, etc. When these exceptions occur, the Java virtual machine (JVM) generally selects thread termination

  • Exception class

    Exceptions that can be handled by the program itself can be caught by catch. Exceptions can be divided into checked exceptions (which must be handled) and unchecked exceptions (which can not be handled)

👨‍💻 The interviewer asked: what's the difference between checked exceptions and unchecked exceptions?

  • Checked exception

    During the compilation of Java code, if the checked exception is not handled by catch/throw, it will not pass the compilation.

    Except RuntimeException and its subclasses, other Exception classes and their subclasses belong to checked exceptions. Common checked exceptions include IO related exceptions, ClassNotFoundException, SQLException

  • Unchecked exception

    During the compilation of Java code, we can compile normally even if we do not handle unchecked exceptions.

    RuntimeException and its subclasses are collectively referred to as unchecked exceptions, such as NullPointerException, NumberFormatException (string converted to number), ArrayIndexOutOfBoundsException (array out of bounds), ClassCastException (type conversion error), ArithmeticException (arithmetic error)

👨‍💻 The interviewer continued to ask: what are the common exceptions you know?

  • NullPointerException: thrown when an application attempts to access an empty object.
  • SQLException: provides exceptions about database access errors or other error information.
  • IndexOutOfBoundsException: thrown when a sort index (such as sorting an array, string, or vector) is out of range.
  • FileNotFoundException: this exception is thrown when an attempt to open the file represented by the specified pathname fails
  • IOException: this exception is thrown when a / O exception occurs. This class is a generic class for exceptions generated by failed or interrupted I/O operations.
  • ClassCastException: thrown when trying to cast an object to a subclass that is not an instance
  • IllegalArgumentException: an exception thrown indicates that an illegal or incorrect parameter was passed to the method.

3. What is the difference between runtime exceptions and non runtime exceptions?

  • Runtime exception

    All of them are RuntimeException class and its subclass exceptions, such as NullPointerException (null pointer exception), indexoutofboundsexception (subscript out of bounds exception), etc. these exceptions are not checked. You can choose to capture and handle them or not in the program. These exceptions are generally caused by program logic errors. The program should avoid such exceptions as much as possible from a logical point of view.

    The characteristic of runtime exception is that the Java compiler will not check it, that is, when such an exception may occur in the program, it will be compiled even if it is not caught with the try catch statement or thrown with the throw clause declaration.

  • Non runtime exception

    Also called compilation Exception. It is an Exception other than RuntimeException. It belongs to Exception class and its subclasses in type.

    From the perspective of program syntax, it is an Exception that must be handled. If it is not handled, the program cannot be compiled. Such as IOException, SQLException, and user-defined Exception exceptions. Generally, exceptions are not checked by users

4. Tell me the difference between throw and throws?

  • throw

    Exception throwing, inside the method body, means that an exception is thrown and handled by the statements inside the method body; throw is a specific action that throws an exception, so it throws an exception instance

    public static double method(int value) {
        if(value == 0) {
            throw new ArithmeticException("Parameter cannot be 0"); //Throw a runtime exception
        }
        return 5.0 / value;
    }
    
  • throws

    The exception declaration, after the method declaration, indicates that if an exception is thrown, the caller of the method will handle the exception; Indicates the possibility of an exception, which does not necessarily occur

    public static void method() throws IOException, FileNotFoundException{
        //something statements
    }
    

5. Do you know try with resources?

If your resource implements the autoclosable interface, you can use this syntax. Most Java standard resources inherit this interface. When you open a resource in the try clause, the resource will be automatically closed after the try code block is executed or after exception handling. Any catch or finally block runs after the declared resource is closed

Resources like InputStream, OutputStream, Scanner and PrintWriter in Java need to be closed manually by calling the close() method. Generally, we implement this requirement through the try catch finally statement, as follows:

//Read the contents of a text file
Scanner scanner = null;
try {
    scanner = new Scanner(new File("D://read.txt"));
    while (scanner.hasNext()) {
        System.out.println(scanner.nextLine());
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} finally {
    if (scanner != null) {
        scanner.close();
    }
}

Use the try with resources statement after Java 7 to transform the above code:

try (Scanner scanner = new Scanner(new File("test.txt"))) {
    while (scanner.hasNext()) {
        System.out.println(scanner.nextLine());
    }
} catch (FileNotFoundException fnfe) {
    fnfe.printStackTrace();
}

Multiple resources can be declared in the try with resources block by using semicolon separation:

try (BufferedInputStream bin = new BufferedInputStream(new FileInputStream(new File("test.txt")));
     BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(new File("out.txt")))) {
    int b;
    while ((b = bin.read()) != -1) {
        bout.write(b);
    }
}
catch (IOException e) {
    e.printStackTrace();
}

6. Tell me about the common methods of Throwable class you know?

  • public String getMessage(): returns a brief description when an exception occurs
  • public String toString(): returns the details of the exception
  • public String getLocalizedMessage(): returns the localization information of the exception object. Override this method with a subclass of Throwable to generate localization information. If the subclass does not override the method, the information returned by the method is the same as that returned by getMessage()
  • public void printStackTrace(): prints exception information encapsulated by Throwable objects on the console

Chestnuts:

/**
 * @author xppll
 * @date 2021/11/30 09:08
 */
public class testThrowable {
    public static void main(String[] args) {
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            System.out.println("getMessage....."+e.getMessage());
            System.out.println("toString....."+e.toString());
            System.out.println("getLocalizedMessage....."+e.getLocalizedMessage());
            System.out.print("printStackTrace.....");
            e.printStackTrace();
        }
    }
}

Output:

getMessage...../ by zero
toString.....java.lang.ArithmeticException: / by zero
getLocalizedMessage...../ by zero
printStackTrace.....java.lang.ArithmeticException: / by zero
	at com.atguigu.mybatisplus.testThrowable.main(testThrowable.java:10)

7. What is serialization? What is deserialization?

If we need to persist Java objects, such as saving Java objects in files or transferring Java objects over the network, serialization is required in these scenarios.

In short:

  • Serialization: the process of converting a data structure or object into a binary byte stream
  • Deserialization: the process of converting a binary byte stream generated during serialization into a data structure or object

Generally speaking, the main purpose of serialization is to transfer objects over the network or store objects in file system, database and memory

👨‍💻 The interviewer asked: what if some fields do not want to be serialized in Java serialization?

Variables that do not want to be serialized are decorated with the transient keyword. It can prevent serialization of variables decorated with this keyword in the instance; When the object is deserialized, the variable value modified by transient will not be persisted and restored

example:

Create a Person class that implements the Serializable interface:

/**
 * @author xppll
 * @date 2021/12/23 22:44
 */
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Serializable {
    private String name;
    private  transient  Integer age;
}

Test class:

/**
 * @author xppll
 * @date 2021/12/23 22:32
 */
public class test {
    public static void main(String[] args) {
        File file = new File("D://out.txt");
        //serialized objects 
        try (ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream(file)
        )) {
            Person person = new Person("dad", 13);
            oos.writeObject(person);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

result:

You can see that there is no age attribute

👨‍💻 The interviewer continued to ask: what should we pay attention to about the use of transient?

  • transient can only modify variables, not classes and methods.
  • For the variable modified by transient, after deserialization, the variable value will be set to the default value of type. For example, if it is a modified int type, the result after the inverse sequence is 0.
  • Because static variables do not belong to any object, they will not be serialized whether they are decorated with the transient keyword or not

Next to the last test case, deserialization:

//Deserialization
try (ObjectInputStream oos = new ObjectInputStream(
        new FileInputStream(file)
)) {
    Person res = (Person) oos.readObject();
    System.out.println(res
    );
} catch (IOException e) {
    e.printStackTrace();
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

Console output:

It can be seen that the variable modified by transient will be set as the default value of type after deserialization

8. What are the IO streams in Java?

  • According to the flow direction, the flow can be divided into input flow and output flow
  • According to the division of operation units, it can be divided into byte stream and character stream
  • According to the role of flow, it is divided into node flow and processing flow

The Java IO stream involves more than 40 classes, which are derived from the following four abstract classes and base classes.

  • InputStream/Reader: the base class of all input streams. The former is byte input stream and the latter is character input stream.
  • OutputStream/Writer: the base class of all output streams. The former is byte output stream and the latter is character output stream.

Structure diagram classified by operation mode:

Classification structure chart by operation object:

👨‍💻 The interviewer asked: since there is a byte stream, why should there be a character stream??

The character stream is converted from bytes by the Java virtual machine. The problem is that this process is very time-consuming, and if we don't know the encoding type, it is easy to cause garbled code. Therefore, I/O flow simply provides an interface for directly operating characters, which is convenient for us to stream characters at ordinary times. If audio files, pictures and other media files use byte stream, it is better to use character stream if characters are involved

9. What is the difference between BIO, NIO and AIO?

Before comparing the differences between the three, let's first understand what are synchronous / asynchronous, blocking / non blocking:

  • Synchronization: no other operations can be done before a task is completed. You must wait (equivalent to making a phone call)
  • Asynchronous: other operations can be performed before a task is completed (equivalent to chatting on QQ)
  • Blocking: compared with the CPU, it suspends the current thread, cannot do other operations, and can only wait
  • Non blocking: you can perform other operations without suspending the current thread
  • BIO: Block IO synchronization blocking is the traditional IO we usually use. It is characterized by simple mode, convenient use and low concurrent processing capacity. Whenever a client sends a request to the server, the server will start a thread. Whether the client responds or not, the thread must wait all the time. As shown in the figure:

  • NIO: New IO is synchronous and non blocking. It is an upgrade of traditional io. The client and server communicate through Channel to realize multiplexing. The server uses one thread to process multiple requests. The requests sent by the client will be registered on the multiplexer (selector), and the client with I/O requests will allocate threads for processing, as shown in the figure:

  • AIO: Asynchronous IO asynchronous non blocking is an upgrade of NIO, also known as NIO2. It realizes asynchronous non blocking IO, that is, it will return directly after application operation and will not be blocked there. When the background processing is completed, the operating system will notify the corresponding thread for subsequent operation.

10. Do you know about deep copy and shallow copy in Java?

  • Shallow copy

    For basic data types: copy data values directly; For reference data type: only the reference address of the object is copied. The old and new objects point to the same memory address. Modify the value of one object and the value of the other object will change.

  • Deep copy

    For basic data types: copy data values directly; As like as two peas, the new data is used to create new memory space. In the new memory space, a new object is copied. The old and new objects do not share memory. Modifying the value of one object will not affect another object.

Here is a detailed example:

Shallow copy

/**
 * @author xppll
 * @date 2021/12/21 12:27
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Address implements Cloneable{
    private String name;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
/**
 * @author xppll
 * @date 2021/12/21 12:29
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Cloneable{
    private Address address;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

Test:

/**
 * @author xppll
 * @date 2021/12/21 12:30
 */
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person(new Address("Changsha"));
        Person person2 = (Person) person1.clone();
        System.out.println(person1.getAddress()==person2.getAddress());//true
    }
}

It can be seen from the results that the clone object of person1 and person1 still use the same Address object.

Deep copy

Here, we simply modify the clone() method of the Person class and copy the Address object inside the Person object.

/**
 * @author xppll
 * @date 2021/12/21 12:29
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Cloneable {
    private Address address;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person person = (Person) super.clone();
        person.setAddress((Address) person.getAddress().clone());
        return person;
    }
}

Retest:

/**
 * @author xppll
 * @date 2021/12/21 12:30
 */
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person(new Address("Changsha"));
        Person person2 = (Person) person1.clone();
        System.out.println(person1.getAddress()==person2.getAddress());//false
    }
}


Last favorite little partner, remember the third company! 😏🍭😘

Keywords: Java Interview

Added by step on Sun, 26 Dec 2021 18:38:34 +0200