当前位置:网站首页>Pen paper records

Pen paper records

2022-08-10 03:59:00 zhangzhang_one


Record the written test questions you have encountered in the last two days.

1、URL和URI的区别?

URI(Universal Resource Identifier)is the Universal Resource Identifier,Webevery available resource,比如图像、视频、HTMLDocuments etc. are located by a Universal Resource Identifier,即URIis a string that identifies a resource using a specific syntax.
URI通常由3部分构成:访问资源的命名机制;存放资源的主机名;资源名称.比如http://www.blog.cn/text/ziyuan1.html/,其中httpIndicates that this is a passhttp协议访问的资源,主机是www.blog.cn,资源名称/text/ziyuan1.html.

URL(Uniform Resource Locator)统一资源定位符,是URI的一个子集,Can uniquely identify the location of a resource on the network,采用URL可以用一种统一的格式来描述各种信息资源.URL是URI概念的一种实现方式.
URL一般格式为:protocol://hostname[:port]/path/[;parameters][?query]#fragment,第一部分是协议,The second part is the address of the host where the resource is stored(有时也包括端口号),第三部分是主机资源的具体地址.

区别:尽管URI和URL都定义了资源是什么,但URLIt also defines how to access the resource.

2、TCPThe difference between a semi-connected queue and a fully-connected queue?

  • 半连接队列(syn队列):客户端发送SYN包,服务端收到后回复SYN+ACK后,服务端进入了SYN_RCVS状态,At this point the kernel will store the connection to the semi-join queue.
  • 全连接队列(accept队列):当服务器端收到客户端的ACK后,The kernel dequeues the connection from the semi-connection queue,Add to the column of fully connected pairs,服务端进入ESTABLISHED状态.服务器调用accept()The function will return the connection at the head of the queue to the user process.

3、单工、半双工、全双工?

  • Simplex only supports data transfer in one direction.
  • 半双工允许数据在两个方向上传输,But data is only allowed to travel in one direction at a time.
  • Full duplex allows data to be transmitted in both directions simultaneously,能够同时发送和接收数据,就像平常打电话一样,You can hear the other person's voice while speaking.

4、CAS无锁队列的实现原理?

In multi-threaded high-concurrency programming,The most critical issue is to ensure safe access to objects in the critical section,It is usually handled by locking,Shackles essentially transform concurrency into serialization,But this is bound to affect the throughput.对于并发控制而言,Locking is a pessimistic strategy,会阻塞线程执行,And lock-free is an optimistic strategy,It will assume that access to the resource is not conflicting,Since there is no conflict,The thread does not need to block.
A lock-free strategy is adoptedCAS来鉴别线程冲突,一旦检测到冲突,The thread will try the operation multiple times until there is no conflict.

CAS:Compare And Swap,比较和交换,主要是通过处理器的指令来保证操作的原子性,它包含三个操作数:VRepresents the variable memory address,A表示旧的预期值,BIndicates the new value to be set.

CAS的语义是:我认为V中的值是A,如果是,那么将A更新为B,否则不修改,并被告知V的值实际为多少.

当多个线程尝试使用CAS更新一个变量时,只有其中一个线程能更新变量的值,而其他线程都失败,失败的线程并不会挂起,而是被告知这次竞争中失败,并且可以再次尝试.

5、Explain the page storage management method in the next paragraph?

分段管理:很方便按照逻辑模块实现信息的共享和保护,But if the segment is too long,not easy to assign,And will generate external debris.
分页管理:内存利用率高,不会产生外部碎片,There is only a small amount of in-page fragmentation,但是不方便按照逻辑模块实现信息的共享和保护.
段页式管理:程序的地址空间划分成多个拥有独立地址空间的段,每个段上的地址空间划分成大小相同的页.

6、The difference between switching and routing?

  • 交换机工作在数据链路层,用来隔离冲突域,The connected devices belong to the same subnet,Responsible for communication within the subnet.而路由器工作在网络层,Used to isolate subnets,连接的设备分属不同子网,工作范围是多个子网之间,Responsible for communication between networks.
  • The switch forwards data based onMAC地址,The route forwards data based onIP地址.

7、封装的含义?

Encapsulation refers to wrapping up internal implementation details,不允许外部程序直接访问,Instead, the hidden information is manipulated and accessed through the methods provided by the class.

8、The code implements the sorting of the singly linked list?

import java.util.*;
/* * public class ListNode { * int val; * ListNode next = null; * } */
public class Solution {
    
    public ListNode sortInList (ListNode head) {
    
        if(head == null || head.next == null)
            return head;
        ListNode fast = head.next, slow = head;
        while(fast != null && fast.next != null){
    
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode tmp = slow.next;
        slow.next = null;
        ListNode left = sortInList(head);
        ListNode right = sortInList(tmp);
        ListNode h = new ListNode(0);
        ListNode res = h;
        while(left != null && right != null){
    
            if(left.val < right.val){
    
                h.next = left;
                left = left.next;
            }else{
    
                h.next = right;
                right = right.next;
            }
            h = h.next;
        }
        h.next = left!=null?left:right;
        return res.next;
    }
}

9、Finds the first one-occurrence character in a string?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
    
    public static void main(String[] args) throws IOException {
    
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        Map<Character,Integer> map = new LinkedHashMap<Character,Integer>();
        for(int i=0;i<str.length();i++){
    
            char c = str.charAt(i);
            if(map.containsKey(c)){
    
                map.put(c,map.get(c)+1);
            }else{
    
                map.put(c,1);
            }
        }
         for(Character c:map.keySet()){
    
             if(map.get(c) == 1){
    
                 System.out.println(c.charValue());
                 return ;
             }
         } 
        System.out.println(-1);
        return ;
    }
}

10、when there is inheritance,为什么基类的析构函数是虚函数?

If the base class is not set called virtual function,When the derived object pointed to by the base class pointer is deleted,就会调用基类的析构函数,不会调用子类的析构函数,Causes the subclass object to be released incompletely,内存泄漏.
So set the base class destructor to a virtual function,when deleted,The subclass destructor is called first,再调用基类析构函数.

11、什么是线程安全?

当多个线程访问一个对象时,If not consider these threadsScheduling and alternate execution in the runtime environment,也不需要进行额外的同步,或者在调用方法进行任何其他的协调操作,调用这个对象的行为都可以获得正确的结果,那这个对象就是线程安全的.

12、The code implements the thread-safe singleton pattern?

public class Singleton {
    
    private static Singleton instance;
    private Singleton() {
    }
    public static synchronized Singleton getInstance() {
    
        if (instance == null) {
    
            instance = new Singleton();
        }

        return instance;
    }
}

public class Singleton{
    
    private Singleton instance;
    private static final Lock lock = new ReentrantLock();
    private Singleton(){
    }
    public static Singleton getInstance(){
    
        lock.lock();
        try{
    
            if(instance == null)
                instance = new Singleton();
            return instance;
           }finally{
    
               lock.unlock();
           }
    }
}
原网站

版权声明
本文为[zhangzhang_one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208100253372638.html