Five Written Exam Questions Daily-2020-9-13

Five Written Exam Questions Daily-2020-9-13

  1. The modification that cannot be used when defining member variables in a class is ()

    Correct answer: B Your answer: B (correct)

final
void
protected
static

Analysis:

void can only be used to modify methods

final can be used to modify methods and variables, modified variables become constants

protected is protected

static is static

  1. There is one of the following objects:
public class DataObject implements Serializable{
    
    
    private static int i=0;
    private String word=" ";
    public void setWord(String word){
    
    
        this.word=word;
    }
    public void setI(int i){
    
    
        Data0bject. i=I;
     }
}

Create a DataObject in the following way:

DataObject object=new Data0bject ( );
object. setWord("123");
object. setI(2); 

Serialize this object into a file, and read the file in another JVM for deserialization. May I ask the values ​​of word and i in the DataObject object read out at this time:

Correct answer: D Your answer: C (wrong)

"", 0
"", 2
"123", 2
"123", 0

Analysis:

Objects are serialized, not classes, class variables will not be serialized

  1. Java does not instantiate static variables and transient modified variables during serialization, because static represents a member of the class, and transient represents the temporary data of the object. It is declared that these two types of data members cannot be serialized

The following statement about program compilation is correct ()

Correct answer: C Your answer: D (wrong)

java语言是编译型语言,会把java程序编译成二进制机器指令直接运行
java编译出来的目标文件与具体操作系统有关
java在运行时才进行翻译指令
java编译出来的目标文件,可以运行在任意jvm上

Analysis:

A: Java is compiled into bytecode, and then translated into machine code that can be recognized by the JVM of each system. This is the cross-platform nature of java one-time programming multi-platform applications

B: Java source files generate class files, which have nothing to do with the system

C: Note that bytecode and machine code are not the same thing. When a java program is running, bytecode will be translated into machine code by jvm, so java is an interpreted language

D: Pay attention to the version of JVM, like a person wearing pants, can a pair of pants be worn by anyone?

  1. Which of the following statements about JAVA exception handling is correct ()

Correct answer: ABD Your answer: AC (wrong)

finally是为确保一段代码不管是否捕获异常都会被执行的一段代码
throws是用来声明一个成员方法可能抛出的各种非运行异常情况
final用于可以声明属性和方法,分别表示属性的不可变及方法的不可继承
throw是用来明确地抛出一个异常情况

Analysis:

throws is used to declare the exception types that the method does not need to handle on the method, and it can be multiple exception classes followed by the exception class name on the method

Throw is used to throw an object of a specific exception class. The exception object used in the method can only be an exception type entity. The try block must be the same as the catch block or finally, and cannot exist alone. The two must appear one.

The finally block will always be executed, regardless of whether there is an error. But if the try statement block or the executed catch statement block uses the JVM system exit statement, the finally block will not be executed. Generally, we put the code to close the resource in the finally It guarantees that resources can always be closed

Then look at this question. A is definitely right. C has nothing to do with exception handling at all, so I don't choose it. Even if it matters, it's wrong

final is used to declare properties, methods, and classes. Respectively indicate that the property cannot be changed, the method cannot be overridden, and the class cannot be inherited.

So the C option says that the method cannot be inherited. It cannot be overridden.

  1. Regarding the volatile keyword, which of the following descriptions is incorrect?

    Correct answer: BD Your answer: AC (wrong)

    用volatile修饰的变量,每次更新对其他线程都是立即可见的。
    对volatile变量的操作是原子性的。
    对volatile变量的操作不会造成阻塞。
    不依赖其他锁机制,多线程环境下的计数器可用volatile实现。
    

Analysis:

Once a shared variable (class member variable, class static member variable) is modified by volatile, then it has two semantics:

  1. This guarantees the visibility of different threads operating on this variable, that is, if a thread modifies the value of a variable, the new value is immediately visible to other threads.
  2. Reordering instructions is prohibited.

Volatile only provides a guarantee that when the variable is accessed, the latest value is read from the memory every time, and the value is not cached in the register-it will be read from the memory every time.

And the modification of this variable, volatile does not provide atomic guarantee.

Due to timely updates, it is likely that another thread can access the latest variable value and cannot break out of the loop

The counter must use lock protection under multithreading.

Guess you like

Origin blog.csdn.net/qq_37924905/article/details/108570376