Niu Ke's Diary (December 6, 2021)

Keywords: Java Back-end

Niu Ke's Diary (December 6, 2021)

Title:

Which of the following modifiers modifies a variable that is shared by all objects generated by the same class ()

public
private
static
final

Resolution:

Correct answer: C

When static modifies a field, it will certainly change the way the field is created (each static modified field has only one storage space for each class, while non static modified fields have one storage space for each object)
The static attribute belongs to a class, so the objects share it, so you can operate through both class name and variable name, as well as object name and variable name

Title:

about java What is the incorrect description of compiling and running commands?  ( )

Run“ java Scut.class"
Run“ java Scut"
compile Scut.java Files, using“ javac Scut.java"The output file of the command is Scut.class
java The object of this command is Scut.class

Resolution:

B correct

A. the wrong running command is java + the name of your Java program, but it does not add a suffix, so this problem is more wrong with the suffix. class
B correct
C javac is a compilation command, followed by your Java program name with a suffix, that is, YourClassName.java, so the answer is correct
The D JVM (Java virtual machine) runs a compiled bytecode file (a file with. Class suffix), that is, YourClassName.class, so the answer is correct

Title:

Given the following code:
public class Test {
    private static int j = 0;

    private static Boolean methodB(int k) {
        j += k;
        return true;
    }

    public static void methodA(int i) {
        boolean b;
        b = i < 10 | methodB(4);
        b = i < 10 || methodB(8);

    }

    public static void main(String args[]) {
        methodA(0);
        System.out.println(j);
    }
}

The program prints"0"
The program prints"4"
The program prints"8"
The program prints"12"
The code does not complete.

Resolution:

Choose B
Call methodA (0) in the main function first
In method a, the second line

b = i < 10 | methodB(4); //The middle is the and operator. After executing methodB(4), j = 4

In methodA, the third line

b = i < 10 || methodB(8);//The middle is the or operator, because I < 10 is established and subsequent calculation is not required

So the final output is 4

Title:

The output after the execution of the following code segment is
public class Test {
    public static void main(String args[]) {
        int i = -5;
        i =  ++(i++);
        System.out.println(i);
    }
}

-7
-3
 Compilation error
-5

Resolution:

Correct answer: C

The answer is a compilation error.
The compilation error lies in this sentence: i = ++(i + +);
++() there must be a variable in parentheses, and i + + is a literal.

Title:

Which of the following descriptions are correct: ()
public class Test {
    public static class A {
        private B ref;
        public void setB(B b) {
            ref = b;
        }
    }
    public static Class B {
        private A ref;
        public void setA(A a) {
            ref = a;
        }
    }
    public static void main(String args[]) {
    ...
        start();
    ....
    }
    public static void start() { A a = new A();
        B b = new B();
        a.setB(b);
        b = null; //
        a = null;
    ...
    }
}

b = null After execution b Can be recycled
a = null After execution b Can be recycled
a = null After execution a Can be recycled
a,b It can only be garbage collected after the end of the whole program
 class A And class B Circular references in the design will lead to memory leakage
a, b Must be start Method cannot be garbage collected until it is executed
	


Resolution:

Correct answer: B C

Title:

about Java Medium ClassLoader Which of the following descriptions are wrong:(    )

By default, Java The application startup process involves three steps ClassLoader: Boostrap, Extension, System
 The general situation is different ClassLoader The classes loaded are different, with the exception of interface classes. For the same interface, the classes loaded by all class loaders are the same
 Class loaders need to ensure thread safety during class loading
ClassLoader of loadClass When loading a class, it returns if the class does not exist null
ClassLoader In the parent-child structure of, the default loading adopts parent priority
 All ClassLoader The loaded classes are from CLASSPATH Environment specified path

Resolution:

Correct answer: B D F

A. The Java system provides three kinds of loaders: Bootstrap ClassLoader, Extension ClassLoader and Application ClassLoader. A is correct
B. In depth understanding of the Java virtual machine P228: for any Class, the Class loader that loads it and the Class itself need to establish its uniqueness in the Java virtual machine. Each Class loader has an independent Class namespace. This sentence can be expressed more commonly: compare whether two classes are "equal" , it only makes sense if the two classes are loaded by the same Class loader. Otherwise, even if the two classes come from the same Class file and are loaded by the same virtual machine, the two classes must not be equal as long as the Class loaders that load them are different. Interface Class is a special Class, so the classes obtained by loading different Class loaders on the same interface Is different. B error
C. The class only needs to be loaded once, so it is necessary to ensure the thread safety of the class loading process and prevent the class from being loaded multiple times. C is correct
D. The class loader of Java program adopts the two parent delegation model. The code to realize the two parent delegation is concentrated in the loadClass() method of java.lang.ClassLoader. The general logic of this method is to check whether it has been loaded first. If not, call the loadClass() of the parent class loader Method. If the parent class loader is empty, the startup class loader is used as the parent class loader by default. If the parent class fails to load, a ClassNotFoundException exception is thrown. D error
E. Working process of parent delegation model: if a class loader receives a class loading request, it will not try to load the class itself, but delegate the request to the parent class loader to complete it. This is true for class loaders at each level. Therefore, all loading requests should eventually be transmitted to the top-level startup class loader, only when the parent class loader The child loader will try to load itself only when it is reported that it is unable to complete the load request. E is correct
F. The Application ClassLoader is responsible for loading the class library specified on the user ClassPath. Not all classloaders load this path. F error

Title:

about Java Memory area which of the following statements are incorrect

The program counter is a small memory space. Its function can be regarded as a signal indicator of the bytecode executed by the current thread. Each thread needs an independent program counter.
Java The virtual machine stack describes java The memory model of method execution. When each method is executed, a stack frame will be created to store local variable table, class information, dynamic link and other information
Java Heap is java The largest piece of memory managed by the virtual machine. Each thread has a memory area where all object instances and arrays allocate memory.
The method area is a memory area shared by each thread. It is used to store constants loaded by the virtual machine, code compiled by the real-time compiler, static variables and other data.

Resolution:

Correct answer: B C

A. Program counter is a small memory space. Its function can be regarded as a signal indicator (offset address) of bytecode executed by the current thread , the bytecode generated during Java compilation is somewhat similar to the instruction of compilation principle. The memory space of the program counter stores the offset address of the bytecode currently executed. Each thread has an independent program counter (the memory space of the program counter is thread private) , because the memory space of the program counter is changed when executing the statement, it will not overflow memory, and the program counter is the only area in the jvm virtual machine specification that does not specify OutOfMemoryError exception;

B.java virtual machine Stack: thread is private, and its life cycle is consistent with that of thread. It describes the memory model of Java method execution: each method creates a Stack frame when executing
Frame) is used to store local variable table, operand stack, dynamic link, method exit and other information. Each method from the call to the end of execution corresponds to a stack frame from the virtual machine stack to the stack.
There is no class information. The class information is in the method area

C.java heap: for most applications, this area is the largest piece of memory managed by the JVM. Thread sharing is mainly used to store object instances and arrays

D. Method area: it is a shared memory area that stores class information, constants, static variables, code compiled by the real-time compiler and other data that have been loaded by the virtual machine.

Title:

What modifiers can local inner classes be decorated with?
public
private
abstract
final

Resolution:

Correct answer: C D

Posted by fry2010 on Mon, 06 Dec 2021 13:50:58 -0800