15 basic Java programming interview questions-motivation node

In the current Internet environment, the demand for programmers and engineers is increasing, and correspondingly more people who want to invest in the engineering industry are also increasing. Therefore, many large companies are gradually increasing their requirements for interviews for engineer positions. In the java interview, the interviewer focuses on whether the basic knowledge is solid. The basic java programming interview questions are the homework that must be done before the interview. The 15 basic java programming interview questions are compiled below. Friends who want to participate in the java interview can Learn the following.

image.png

1. Where is the security of the java language level?

Answer: Java eliminated the powerful but dangerous pointers and replaced them with references. Since the pointer can be moved, the pointer can point to a memory area, regardless of whether the area is available. This is dangerous, because the memory address may store important data or be occupied by other programs, and use pointers It is also easy for the array to cross the boundary.

Garbage collection mechanism: The programmer does not need to directly control the memory collection, and the garbage collector automatically collects the no longer used memory in the background. Avoid the program forgetting to recycle in time, causing memory leaks. Avoid program error recovery of the memory of the program's core class library, causing system crashes.

Exception handling mechanism: Java exception mechanism mainly relies on five keywords: try, catch, finally, throw, and throws.

Forced type conversion: Only when the forced conversion rules are met can the forced conversion succeed.

2. What is the relationship between JDK, JRE, and JVM?

Answer: jdk is a development kit used when developing JAVA programs, and there is also a JRE operating environment JRE inside. JRE is the operating environment required for the running of Java programs, that is, if you just run Java programs instead of developing, you can run existing Java programs just by installing JRE. Both JDk and JRE contain the Java virtual machine JVM, and the Java virtual machine contains the interpreter and class loader of many application programs, etc.

3. What are the similarities and differences between if multi-branch statements and switch multi-branch statements?

Answer: The same: They are all branch sentences, and judge and deal with more than one case.

Difference: switch is more suitable for multi-branch situations, that is, there are many situations that need to be judged and processed. The judgment condition type is single, and there is only one entry. After the branch is executed (if there is no break out), it is executed without judgment; And if-elseif-else multi-branch is mainly applicable to branch structures with fewer branches. The judgment type is not single. As long as one branch is executed, the following branch will not be executed. Switch is equal value judgment (such as >= <= is not allowed), and if is equal value and interval, it can be used in a large range.

4. The difference and connection between & and &&

Answer: The connection between & and &&: Both & and && can be used as logical AND operators, but it depends on the specific conditions when they are used. Operand 1 & Operand 2, Operand 1 & & Operand 2, Expression 1 & Expression 2, Expression 1 & & Expression 2.

Case 1: When the above operand is a boolean variable, both & and && can be used as logical AND operators.

Case 2: When the result of the above expression is a boolean variable, both & and && can be used as logical AND operators.

Represents logical and (and). When the result of the expression or operand on both sides of the operator is true, the result of the whole operation is true. Otherwise, as long as one of them is false, the result is false.

The difference between & and && (different points): The & logical operator is called the logical AND operator, and the && logical operator is called the short-circuit AND operator, which can also be called the logical AND operator. In any case, the operands or expressions on both sides of & will participate in the calculation. For &&: When the operand on the left of && is false or the result of the expression on the left is false, the operand or expression on the right of && will not participate in the calculation, and the final result will be false.

To sum up, if the first operand of the logical AND operation is false or the result of the first expression is false, whether the second operand or expression is operated has no effect on the final result. It must be false.

5. Basic data type conversion rules?

Answer: Basic type conversion is divided into automatic conversion and forced conversion.

Automatic conversion rules: data types with small capacity can be automatically converted into data types with large capacity, or

In order to say that low-level automatic conversion to high-level. The capacity here does not refer to the number of bytes, but refers to the range of type expression.

Forced conversion rules: high-level to low-level requires forced conversion. How to convert: (1) The conversion on the right side of the assignment operator "=" is automatically converted to the highest-level data type in the expression before performing operations. (2) The conversion on both sides of the assignment operator "=", if the left level> the right level, it will be automatically converted; if the left level == the right level, no conversion is required; if the left level <the right level, a forced conversion is required. (3) Integer constants can be directly assigned to type variables such as byte, short, char, etc., without the need for forced type conversion, provided that it does not exceed the scope of expression, otherwise a forced conversion must be performed.

6. What are the similarities and differences between if multi-branch statements and switch multi-branch statements?

Answer: The same: They are all branch sentences, and judge and deal with more than one case.

Difference: switch is more suitable for multi-branch situations, that is, there are many situations that need to be judged and processed. The judgment condition type is single, and there is only one entry. After the branch is executed (if there is no break out), it is executed without judgment; And if-elseif-else multi-branch is mainly applicable to branch structures with fewer branches. The judgment type is not single. As long as one branch is executed, the following branch will not be executed. Switch is equal value judgment (such as >= <= is not allowed), and if is equal value and interval, it can be used in a large range.

7. What is the difference between while and do-while loops?

Answer: while first judges and then executes, the first judgement is false, the loop body is not executed at a time; do while executes first and then judges, at least one execution; if the while loop judges to be true for the first time, there is no difference between the two loops .

image.png

8. The role of break and continue?

Answer: break: end the current loop and exit the current loop body. Break can also exit the switch statement.

continue: The subsequent statements in the loop body are not executed, but the loop is not over, continue to judge the loop condition (for loop will also i++). continue just ends the loop.

9.What are the characteristics of the array?

Answer: The array is (ordered) (collection) of (same type data);

The array will open up a continuous space in the memory, each space is equivalent to a variable before, called the element of the array;

Each array element has a default value double 0.0 boolean false int 0;

The array elements are ordered, not in size order, but in index order;

Basic data types and reference data types can be stored in an array; but for an array, the type of the array is fixed and can only be one;

length: The length of the array. The length of the array is fixed. Once defined, it cannot be changed (expansion of the array).

10. If a number is exactly equal to the sum of its factors, this number is called "final number", for example, 6 = 1+2+3. Program to find all the numbers within 0-1000

Answer: public class WanShu {

private static boolean isWanShu(int shu){

    int sum = 0;

    for (int i = 1; i < shu; i++) {

        if(shu%i==0){

            sum+= i;

        }

    }

    if(sum==shu){

        return true;

    }else{          

        return false;

    }   

}

public static void main(String[] args) {

    for (int i = 1; i < 1000; i++) {

        if(isWanShu(i)){

            System.out.println(i);

        };

    }

}

}

11. The output of the following program is (A)

static boolean foo(char c) {

    System.out.print(c);

    return true;

}

public static void main(String[] args) {

    int i = 0;

    for (foo('A'); foo('B') && (i < 2); foo('C')) {

        i++;

        foo('D');

    }

}

A. ABDCBDCB B. ABDCDBCB C. ABDBCDCB D. ABDBCDCB

Analysis: First, it must start with ABDC, exclude C and D, and then execute foo('B') && (i <2). The result is B, and the result is A. Execution order--foo('A')--foo('B')&&true--foo('D')--foo('C')--foo('B')&&true--foo('D')--foo(' C')–foo('B')&&false–end

12. When n = 5, the return value of the following function is: (A)

int foo(int n)

{

    if(n<2)return n;

    return foo(n-1)+foo(n-2);

}

A.5 B.7 C.8 D.1

Analysis: Result=foo(4)+foo(3)

foo(4)=foo(3)+foo(2)

foo(3)=foo(2)+foo(1)

foo(2)=foo(1)+foo(0)

foo(1)=1

foo(0)=0

So foo(2)=1, foo(3)=2, foo(4)=3

13. Write the code for insertion sort

Answer: package com.bjsxt;

public class TestInsertSort {

public static void sort(int arr[]) {

int i, j;

for (i = 1; i < arr.length; i++) {

int temp = arr[i];

for (j = i; j > 0 && temp < arr[j - 1]; j–) {

arr[j] = arr[j - 1];

}

arr[j] = temp;

}

}

}

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

Answer: Final modifier (keyword) If a class is declared as final, it means that it can no longer derive new subclasses, and cannot be inherited as a parent class, for example: String class, Math class, etc. Declaring variables or methods as final ensures that they will not be changed during use. The variable declared as final must be given an initial value at the time of declaration, and can only be read in future references and cannot be modified. The method declared as final can also only be used, cannot be overridden, but can be overloaded. Using final modified objects, the reference address of the object cannot be changed, but the value of the object can be changed.

Finally, a finally block is provided during exception handling to perform any cleanup operations. If there is finally, the finally statement will be executed regardless of whether an exception occurs. Under normal circumstances, all related operations such as closing the physical connection (IO stream, database connection, Socket connection) are put into this code block.

Finalize method name. Java technology allows the use of the finalize() method to do the necessary cleanup work before the garbage collector clears the object from memory. The finalize() method is called before the object is deleted by the garbage collector. It is defined in the Object class, so all classes inherit it. Subclasses override the finalize() method to organize system resources or perform other cleanup tasks. Under normal circumstances, this method is called by the JVM, and programmers should not call it.

15. What is the difference between character constants and string constants?

Answer: Formally: a character constant is a character surrounded by single quotes; a string constant is a number of characters surrounded by double quotes. Meaning: a character constant is equivalent to an integer value (ASCII value) and can participate in expression operations; a character string constant represents an address value (the string is stored in the memory location). Occupied memory size: character constants only take up one byte; string constants take up several bytes (at least one character end flag).

The above are the basic java programming interview questions compiled for my friends today. These 15 questions certainly cannot be said to have the most comprehensive knowledge of java programming, but they are definitely the content of the basic common exams, and there will be a lot of them in the java interview Therefore, I hope everyone can learn the above content carefully, and then learn more java knowledge through the java basic tutorial in time.

Guess you like

Origin blog.csdn.net/weixin_49543720/article/details/110441494