Five Questions of Daily Java Written Test-2020-9-21

Five Questions of Daily Java Written Test-2020-9-21

  1. The following descriptions about List interface, Set interface and Map interface are wrong?

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

他们都继承自Collection接口
List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置
Set是一种不包含重复的元素的Collection
Map提供key到value的映射。一个Map中不能包含相同的key,每个key只能映射一个value

Analysis:

Map interface and Collection interface are of the same level

Collection

​ -----List

​ -----LinkedList asynchronous

​ ----ArrayList is not synchronized, which implements a variable-size element array

​ ----Vector synchronization

​ ------Stack

​ -----Set does not allow the same elements

Map

​ -----HashTable synchronization to implement a key-value mapping hash table

​ -----HashMap is asynchronous,

​ -----WeakHashMap The improved HashMap implements "weak reference". If a key is not referenced, it will be recycled by the GC

  1. Off-heap refers to the kind of memory ()

    Correct answer: B Your answer: C (wrong)

JVM GC能管理的内存
JVM进程管理的内存
在JVM老年代内存区
在JVM新生代内存

Analysis:

Off-heap memory means that memory objects are allocated in memory outside the heap of the Java virtual machine, which is directly managed by the operating system (not the virtual machine). It does not belong to the old and new generations.

JVM GC reclaims heap and method area, select B for elimination method

  1. List is an ArrayList object. Which option code is filled in //todo delete, which can correctly and safely delete the objects saved in a list during the Iterator traversal process? ()
Iterator it = list.iterator();
int index = 0;
while (it.hasNext())
{
 Object obj = it.next();
    if (needDelete(obj))  //needDelete返回boolean,决定是否要删除
 {
        //todo delete
    }
    index ++;
}

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

it.remove();
list.remove(obj);
list.remove(index);
list.remove(obj,index);

Analysis:

The terator supports the safe removal of objects from the source collection, just call remove() on the Iterator. The advantage of this is that you can avoid ConcurrentModifiedException, when the Iterator is opened to iterate the collection, and the collection is modified at the same time. Some collections do not allow deleting or adding elements during iteration, but it is safe to call the remove() method of Iterator.

Integer i = 42; 
Long l = 42l; 
Double d = 42.0; 

The following is true

​ Correct answer: G Your answer: F (wrong)

(i == l)
(i == d)
(l == d)
i.equals(d)
d.equals(l)
i.equals(l)
l.equals(42L)

Analysis:

The "==" operation of the packaging class will not be automatically unboxed without encountering arithmetic operations

The equals() method of the packaging class does not handle data transformation

Expand knowledge:

1, basic and basic molded a "==" comparison operator, basic package type will be automatically changed to basic after unpacking compared, so Integer (0) automatically unpacking to an int Comparing again, it obviously returns true;

​ int a = 220;

​ Integer b = 220;

​ System.out.println(ab);//true
​ **2, two Integer types for "
"Compare, if its value is between -128 and 127, then return true, otherwise return false, which is related to the buffer object of Integer.valueOf(), so I won’t repeat it here.**

​ Integer c=3;

​ Integer h=3;

Integer e = 321;

​ Integer f=321;

​ System.out.println(c==h);//true

​ System.out.println(e==f);//false

3, the basic two-package type for equals () comparing first equals () would be more types, if the same type, the continued comparison value, if the values are identical, returns true.

​ Integer a=1;

​ Integer b=2;

​ Integer c=3;

​ System.out.println(c.equals(a+b));//true

4, the equals basic package type call (), but the parameters are the basic type, this time, the first packing takes place automatically, into its basic package type, then comparing 3.

​ int i=1;

​ int j = 2;

​ Integer c=3;

​ System.out.println(c.equals(i+j));//true

  1. The variables a, b, and c in the following Java code are stored in the memory storage area.
class A {
    
    
    private String a = “aa”;
    public boolean methodB() {
    
    
        String b = “bb”;
     final String c = “cc”;
    }
}
正确答案: C  你的答案: C (正确)
堆区、堆区、堆区
堆区、栈区、堆区
堆区、栈区、栈区
堆区、堆区、栈区
静态区、栈区、堆区
静态区、栈区、栈区

Analysis:

The answer is C

a is a member variable in the class, stored in the heap area

b, c are local variables in the method, stored in the stack area

Guess you like

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