Five Questions of Daily Java Written Exam-2020-9-22

Five Questions of Daily Java Written Exam-2020-9-22

1... The keyword used to create an object in the Java language is ()

	正确答案: C  你的答案: C (正确)
class
interface
new
create
  1. Which of the following statements about java hashmap is wrong?

    Correct answer: C Your answer: C (correct)

    HashMap 的实例有两个参数影响其性能:“初始容量” 和 “加载因子”。
    HashMap 的实现不是同步的,意味着它不是线程安全的
    HashMap通过开放地址法解决哈希冲突
    HashMap中的key-value都是存储在Entry数组中的
    

Analysis:

Hashmap uses the zipper method to resolve conflicts

Here to help you summarize the knowledge points of hashMap and hashtable: 1. Some statements about HashMap: a) HashMap is actually a "linked list hash" data structure, that is, a combination of array and linked list. The underlying structure of HashMap is an array, and each item in the array is a linked list. b) HashMap instance has two parameters that affect its performance: "initial capacity" and filling factor. c) The implementation of HashMap is not synchronized and the thread is not safe. HashTable thread safety d) The key-value in HashMap is stored in Entry. e) HashMap can store null keys and null values, and does not guarantee that the order of elements will remain unchanged forever. Its bottom layer uses arrays and linked lists, and the uniqueness of keys is guaranteed through the hashCode() method and the equals method

  1. Which of the following statements is correct:

​ Correct answer: D Your answer: C (wrong)

形式参数可被字段修饰符修饰
形式参数不可以是对象
形式参数为方法被调用时真正被传递的参数
形式参数可被视为local variable

A: Formal parameters can only be modified by final

B: Formal parameters can be objects

C: The copy of the actual parameter is passed when the formal parameter is called

D: local variable: local variable

  1. What will happen when you attempt to compile and run the following code?
public class Test{
    
    
static{
    
    
   int x=5;
}
static int x,y;
public static void main(String args[]){
    
    
   x--;
   myMethod( );
   System.out.println(x+y+ ++x);
}
public static void myMethod( ){
    
    
  y=x++ + ++x;
 }
}

​ Correct answer: D Your answer: B (wrong)

compiletime error
prints:1
prints:2
prints:3
prints:7
prints:8
  1. Regarding the following Java program, which description is correct: ()

    public class ThreadTest extends Thread {
    public void run() {
    System.out.println("In run");
    yield();
    System.out.println("Leaving run");
    }
    public static void main(String []argv) {
    (new ThreadTest()).start();
    }
    }
    

    Correct answer: C Your answer: A (wrong)

程序运行输出只有In run
程序运行输出只有Leaving run
程序运行输出先有In run后有Leaving run
程序运行输出先有Leaving run后有In run
程序没有任何输出就退出了
程序将被挂起,只能强制退出

Analysis:

The function of the Thread.yield() method is to suspend the currently executing thread object and execute other threads.

What yield() should do is to return the currently running thread to a runnable state to allow other threads with the same priority to get a chance to run. Therefore, the purpose of using yield() is to allow threads of the same priority to rotate appropriately. However, in practice, there is no guarantee that yield() will achieve the concession purpose, because the concession thread may be selected again by the thread scheduler .

Conclusion: yield() never caused the thread to go to the waiting/sleeping/blocking state. In most cases, yield() will cause the thread to transition from the running state to the runnable state, but it may have no effect.

Guess you like

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