Five daily written test questions-2020-9-15

Five daily written test questions-2020-9-15

public` `interface` `IService {String NAME=``"default"``;}

Which item is the default type equivalent:

Correct answer: C Your answer: C (correct)

public String NAME="default";
public static String NAME="default";
public static final String NAME="default";
private String NAME="default";
  1. After executing the following code segment, the string value referenced by the variable s1 is (). *
String s1 =   "ABCD"; 
   String s2 =   "1234"; 
System.out.println(s1   + s2);         

Correct answer: A Your answer: A (correct)

ABCD
1234
ABCD1234
1234ABCD

Analysis:

Because String is an immutable type and is stored in the constant character area, the answer is ABCD.

static String str0="0123456789";
static String str1="0123456789";
String str2=str1.substring(5);
String str3=new String(str2);
String str4=new String(str3.toCharArray());
str0=null;   

Assume that str0,..., str4 are read-only references.

In Java 7, based on the above code, after a FullGC has occurred, the number of characters reserved by the above code in the Heap space (not including PermGen) is ()

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

5
10
15
20

Analysis 1:

Should be C

substring is actually new, 5 characters

str3 and 4 are also new, each with 5 characters

Each will create new objects

The constant pool is PermGen

So it should be a total of 15 characters

Analysis 2:

This is a question about the garbage collection mechanism of java. Garbage collection is mainly for the recovery of the heap area, because the memory in the stack area is released with threads. The heap is divided into three areas: Young Generation (Young Generation), Old Generation (Old Generation), and Permanent Generation (Permanent Generation, that is, method area).

Young generation: When the object is created (new), the object is usually placed in Young (except for some objects that occupy a large amount of memory). After a certain Minor GC (memory collection for the young generation), objects that are still alive will be moved to the year. Old generation (some specific mobile details omitted).

Old generation: These are the larger objects moved by the younger generation. Minor GC (FullGC) is for the collection of the old generation

Permanent generation: final constants, static variables, and constant pools are stored.

Both str3 and str4 are directly new objects, and the source code of substring is actually a new string object returned, as shown below:

After fullgc, the memory recovery in the old area will account for 15 in the young area, not counting PermGen. So the answer is C

In Java, regarding the description of the HashMap class, the following is correct ()

​ Correct answer: ACD Your answer: AD (wrong)

HashMap使用键/值得形式保存数据
HashMap 能够保证其中元素的顺序
HashMap允许将null用作键
HashMap允许将null用作值
Map collection class key value
HashMap Allowed to be null Allowed to be null
TreeMap Not allowed to be null Allowed to be null
ConcurrentMap Not allowed to be null Not allowed to be null
HashTable Not allowed to be null Not allowed to be null

What are the synchronizers provided by the JDK for concurrent programming?

​ Correct answer: ABC Your answer: BC (wrong)

Semaphore
CyclicBarrier
CountDownLatch
Counter

Analysis:

Answer: ABC
A, Semaphore of the Java concurrency library can easily control the semaphore. Semaphore can control the number of resources that can be accessed at the same time. Acquire a permission through acquire(). If not, wait, and release() releases A license.
B. The main method of CyclicBarrier is one: await(). The await() method is not called once, the count will decrease by 1, and the current thread will be blocked. When the count decreases to 0, the blocking is released, and all threads blocked on this CyclicBarrier start to run.
C, literally translated is CountDown latch (Latch). Needless to say, the countdown, the latch means, as the name suggests, to prevent progress. This means that the CountDownLatch.await() method will block the current thread before the countdown reaches 0.
D, Counter is not a synchronizer for concurrent programming

Guess you like

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