A) Syntax error. There is no such thing as @ in Python
B) this causes testing() to automatically print 1000 when we call it
C) this prints <function testing at 0x1023e2340>
D) this prints 1000 automatically without us calling testing()
E) testing()'s metadata will automatically print whenever it is called
Answer
Answer: C
here, we are decorating testing() with print(). This is the same as:
A) No error
B) ZeroDivisionError
C) IndexError
D) KeyError
E) All of the above
Answer
Answer: D
*args allows our function to take in any number of positional arguments, while **kwargs allow our function to take in any number of keyword arguments.
Dog('rocky', 5) passes 2 positional arguments to __init__, and thus args=('rocky', 5) while kwargs={} (no keyword arguments here)
args, kwargs = kwargs, args switches args and kwargs. So now, args={} and kwargs=('rocky', 5)
When we attempt to call args['name'], we get a KeyError as at this point, args is an empty dictionary.
5、GIL
What is the Python Global Interpreter Lock (GIL)?
A) a physical lock that secures Python servers from intruders
B) a thing that prevents the Python interpreter from leaking data to other processes on your computer
C) a programming paradigm that allows us to run multiple Python processes concurrently
D) a thing that allows one thread to run per interpreter at one time
E) a thing that enables our Python interpreter to run code faster
Answer
Answer: D
The Python GIL makes is such that only 1 thread is able to run in the Python interpreter at one time. (tho we can use multiprocessing to circumvent this)
6、True = False
True = False
False = True
print(not True, not False)
what does this print?
A) SyntaxError
B) False True
C) True False
D) True True
E) False False
Answer
Answer: A
We cannot assign anything to True as True is a reserved Python keyword. I hope you answered this correctly.
7、Context manager
What is a context manager used for?
A) to manage contexts
B) to properly handle resources eg file operations or databases
C) to ensure that type hints in Python are enforced
D) to make sure that any exceptions do not affect other contexts
E) to ensure that the Python interpreter does not taking up an excessive amount of RAM
Answer
Answer: B
# let's say we need to read a file
file = open('hi.txt')
print(file.read())
file.close()
if we do this, there’s a chance that file.close() might not executed due to some error. Not closing the file might lead to the following problems:
the file might take up RAM and slow down your program
changes made to the file might not stick, as many files only save properly when they are closed
in Windows, a file is treated as locked when it is open, so other scanners or programs might not be able to read it
and other weird issues
# reading file using context manager
with open('hi.txt') as file:
print(file.read())
上周我有过这样的经历。目前,我的研究涉及大量特定于 x86 的编程和虚拟机自省 (VMI)。 为了测试我正在开发的概念验证虚拟机管理程序之一,我需要一种方法来快速将 Linux PID 值转换为相应的 在 CPU 上执行该进程时加载到 CR3 寄存器中的值。对于那些不熟悉 x86 CPU 架构的人, 我建议在Linux x86页面表管理上阅读此页面。 简而言之,当一个进程在 x86 CPU 上执行时,CR3 寄存器会加载该进程的页面全局目录 (PGD) 的物理地址。 这是必需的,以便 CPU 可以执行从虚拟内存地址到物理内存地址的转换。 由于每个进程都需要自己的 PGD,因此 CR3 寄存器中的值对于系统中的每个计划进程都是唯一的。 这对 VMI 来说非常方便,因为这意味着我们不需要不断扫描客户机内核的内存来跟踪哪个进程 正在执行。相反,我们可以只监控对 CR3 寄存器的写入。
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/pid.h>
#include <asm/io.h>
unsigned long pid_to_cr3(int pid)
{
struct task_struct *task;
struct mm_struct *mm;
void *cr3_virt;
unsigned long cr3_phys;
task = pid_task(find_vpid(pid), PIDTYPE_PID);
if (task == NULL)
return 0; // pid has no task_struct
mm = task->mm;
// mm can be NULL in some rare cases (e.g. kthreads)
// when this happens, we should check active_mm
if (mm == NULL) {
mm = task->active_mm;
}
if (mm == NULL)
return 0; // this shouldn't happen, but just in case
cr3_virt = (void *) mm->pgd;
cr3_phys = virt_to_phys(cr3_virt);
return cr3_phys;
}