China Electronics Society May 2023 Youth Software Programming Python Level Examination Paper Level 4 Real Questions (Including Answers)

2023-05 Python Level 4 Real Exam Questions

Score: 100

Number of questions: 38

Test duration: 60min

1. Multiple-choice questions (25 questions in total, 50 points in total)

1. What is the result of the following program segment? (A) (2 points)

def s(n):
    if n==0:
        return 1
    else:
        return n +s(n-1)
print(s(7))

A.  29

B.  27

C.  1

D.  0

2. When n is 6, what is the result of running the following Python program? (B) (2 points)

def f(n):
    if n<=2:
        return 1
    else:
        return f(n-1)+f(n-2)
n=int(input("请输入一个正整数:"))
print(f(n))

A.  5

B.  8

C.  11

D.  13

3. There is a cow who gives birth to a heifer at the beginning of each year. Each heifer gives birth to a heifer at the beginning of each year starting from the fourth year. How many cows are there in the nth year? It can be inferred from the recursion method that when the number of years is less than or equal to 4, there are several cows in that year, that is, a[1]=1; a[2]=2; a[3]=3; a[4] =4. When n is greater than 4, the heifer born in the first year can also give birth to a heifer, that is, it is time to consider whether the heifer can give birth, so when n>4, a[n] =? (A) (2 points)

A.  a[n-1]+a[n-3]

B.  a[n-1]+a[n-4]

C.  a[1]+a[3]

D.  a[-1]+a[-3]

Answer analysis: There were four in the first four years. Starting from the fifth year, the heifer born in the second year will also give birth to a cow (and there will be a cow at the beginning of each year from now on), plus the heifers born in the first year. The heifers born to one cow make a total of six cows; in the sixth year, the heifers born to the same cow in the third year must be added, making a total of nine cows; and so on.

4. Binary search is also called half search. Which of the following sequence is suitable for binary search algorithm? (D) (2 points)

A.  11 99 4 25 3 39

B.  43 71 78 81 6 55

C.  67 62 68 4 1 17

D.  85 78 59 53 19 18

Answer analysis: According to the implementation principle of binary search, first the sequence elements must be in order.

5. Among the 32 brand-new gold coins, there is a counterfeit coin that looks exactly like a real gold coin (with a smaller mass). Now there is only one balance. How many times can the counterfeit coin be discovered by using the dichotomy method? (B) (2 points)

A.  4

B.  5

C.  6

D.  7

Answer analysis: Binary search method, each time by comparing with the middle element of the interval, the interval to be searched is reduced to half of the previous one, until the element to be searched for is found, or the interval is reduced to 0.

6. It is known that in a certain program, there is a global variable named a. In a function in the program, a variable a is defined again, and the variable is not a combination type variable.

Which of the following statements is correct? (B) (2 points)

A. If a is declared as global in this function, the operation on a has nothing to do with the global variable a.

B. If a is not declared as global in this function, the operation on a has nothing to do with the global variable a.

C. If a is not declared as global in this function, the operation on a is the operation on the global variable a.

D. Regardless of whether a is declared as global in this function, operations on a are operations on the global variable a.

Answer analysis: If it is not declared as global, it is a local variable, so it has nothing to do with the global variable a.

7. For the list in the program, which option has the parameters filled in on the horizontal line in range() and has the highest algorithm execution efficiency? (C) (2 points)

a=[1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1]
for i in range _____:
    if a[i]==1:
        print(i)

A.  (0,16,1)

B.  (0,16,2)

C.  (0,16,3)

D.  (0,16,4)

8. Which of the following functions cannot be called directly? (B) (2 points)

A.  print( )

B.  sqrt( )

C.  str( )

D. dict( )

Answer analysis: The sqrt() function is a function in the math module and cannot be called directly. The math module should be imported first.

9. Regarding the return statement, which of the following statements is correct? (A) (2 points)

A. The data type of the return value in the return statement can be a list

B. There cannot be expressions in the return statement

C. A function has at least one return statement

D. return can only return one value

Answer analysis: The return statement can contain expressions and can also return multiple values. A function can have a return value or no return value.

10. Regarding the description of functions, which one is incorrect? (C) (2 points)

A. Return the result through the return statement in the function

B. Functions can improve code reuse

C. Global variables cannot be used inside a function

D. The definition of the function must be before the main program function call statement

11. Xiaofang writes an anonymous function to calculate the area of ​​a rectangle. Which of the following statements is correct? (A) (2 points)

A.  rst = lambda a,b : a * b

B.  lambda a,b:a*b

C.  lambda a,b,a*b

D.  rst = lambda a,b,a*b

Answer analysis: The keyword lambda represents an anonymous function. The parameters before the colon represent the parameters of the function, and the return value after the colon represents the return value. When defining an anonymous function, you need to assign it to a variable.

12. Run the following program, what is the output result? (A) (2 points)

s = 1
def sums(n):
    global s
    s = 0
    s = s + n
    print(s)
sums(5)
print(s)

A.  5 5

B.  5 1

C.  1 1

D.  1 5

Answer analysis: If you want to operate variables outside the function inside the function, you need to declare it as global inside the function. In this question, s is declared as global inside the function, so changes inside the function will affect outside the function, so the output results are 5.

13. Xiao Ming helps the teacher count the number of students aged 10 years old. The teacher has the ID number and other information of every student in the grade. He wrote the program as follows.

#列表stud存储每位学生的身份证号码,如
stud = ['110726201205261117','120718201209011101']
def cj(xs):
    c = 0
    for s in xs:
        age = int(   ①   )
        if 2023 - age == 10:
            c += 1
    return c
print(cj(stud) )

Among the following codes, which code underlined ① cannot be selected to implement the statistical function? (D) (2 points)

A.  s[6:10]

B.  s[6:-8]

C.  s[-12:-8]

D.  s[-12:11]

Answer analysis: Slice the code of the year of birth from the ID number. The codes of the three options A, B, and C are all correct.

14. Which of the following descriptions of recursion is correct? (C) (2 points)

A. There must be a loop structure in the recursive function

B. Recursion does not reflect the idea of ​​"reducing big things into small things"

C. Recursion has clear boundary conditions to end the recursion

D. Recursive execution is more efficient

Answer analysis: Recursion has clear boundary conditions for ending the recursion and the boundary value at the end. Recursion embodies the idea of ​​"reducing big things into small things".

15. Xiaofang’s mother divides apples as follows: divide half of them and add an apple on the first day, divide the remaining half and add an apple on the second day, and divide the remaining half and add an apple every day thereafter. On the 8th day, my mother found that there was only one apple left. How many apples did Xiaofang’s mother have at the beginning? Which of the following algorithms can we use to solve this problem? (B) (2 points)

A. Find

B. Recursion

C. Enumeration

D. Sort

Answer analysis: Calculate the number of remaining apples on the 7th day from the 8th day forward, and then calculate the number of apples on the 6th day, and solve the problem in sequence. This is a recursion problem

16. The laws of nature: the number of petals

The number of petals in most petals is 1, 1, 2, 3, 5, 8, 13, 21,... If you carefully observe the order of these numbers, you will find that the arrangement of these numbers is regular, that is, Bonacci Sequence. Student Xiao Li writes a program to find the value of the nth item of the sequence.

 

The code in the box consists of the following three parts: ① a=b ② b=c ③ c=a+b. Which of the following options has the correct order of codes? (D) (2 points)

A.  ①②③

B.  ①③②

C.  ③②①

D.  ③①②

17. A student uses bisection search and sequential search to find the number 15 in the number sequence "1, 3, 5, 8, 15, 21, 35, 65". Which of the following numbers can be accessed by both methods? (C) (2 points)

A.  3

B.  5

C.  8

D.  34

Answer analysis: To search for the number 15 sequentially, the numbers accessed are 1, 3, 5, 8, and 15; to use binary search for the number 15, the numbers accessed are 8, 21, and 15 in order; the number 8 is accessed by both methods. Therefore choose C.

18. Xiao Li checks the Python library installed on the computer. Which of the following commands is feasible? (B) (2 points)

A.  pip install

B.  pip list

C.  pip show

D.  pip help

19. Run the following program, what is the output result? (A) (2 points)

a = [0] * 10
def tj(n):
    while n>0:
        a[n%10] += 1
        n //= 10        
    return sum(a)
print(tj(20230113))

A.  8

B.  12

C.  4

D.  6

Answer analysis: The function tj counts the number of times each digit appears in the integer n. When n=20230113, 0 appears 2 times, 1 appears 2 times, 2 appears 2 times, and 3 appears 2 times, so the answer is A.

20. Usually, when defining a function, how many parameters does it contain at most? (D) (2 points)

A.  3

B.  4

C.  5

D. as many as you want

21. Which of the following functions can be called to view documentation? (A) (2 points)

A.  help()

B.  range()

C. only()

D.  print()

Answer analysis: The help() function can view the documentation.

22. What is the output result of the following code? (C) (2 points)

def sum(a):
    a+=1
a=6
sum(a)
print(a)

A.  8

B.  7

C.  6

D.  2

23. What is the output of the following code? (B) (2 points)

def py(a,b=2):
  a=a%b
  return(a)
print(py(7))

A.  1,2

B.  1

C.  3,2

D.  3

24. What is the output of the following code? (B) (2 points)

def py(b):
    b+=3
    return(b)
print(py(3))

A.  3

B.  6

C.  0

D.  9

25. Which of the following is a third-party library for Chinese word segmentation in Python? (D) (2 points)

A. pandas

B.  beautifulsoup4

C.  python-docx

D. jieba

Answer analysis: jieba belongs to the third-party library of python Chinese word segmentation direction

2. True or False Questions (10 questions in total, 20 points in total)

26. Each time you enter a deeper level of recursion, the problem size should increase compared to the previous recursion. ( wrong)

Answer analysis: Each time you enter a deeper level of recursion, the problem size should be reduced compared to the last recursion.

27. Positional parameters and keyword parameters are concepts when calling functions. When they coexist, keyword parameters must be written before unknown parameters, otherwise syntax errors will occur. (wrong)

correct incorrect

Answer analysis: When the two coexist, the keyword parameters must be written after the positional parameters, otherwise a syntax error will occur.

28. A function can have no parameters or multiple parameters, but the number of parameters must be determined. (wrong)

Answer analysis: The number of parameters of a function can be uncertain.

29. There is no way to use this function without understanding its internal implementation details. ( wrong)

Answer analysis: When calling a function, you only need to know the function name and its input and output. You do not need to know the implementation details of the function.

30. Xiao Ming writes the following function:

def jiafa(x,y=0): 
    return x+y 

He calls the function using the statement jiafa(10), and the program will prompt a running error. (wrong)

Answer analysis: When the function is called, you do not need to pass a value to the default parameter in the function parameter. Therefore, when the function is called, the result 10 is returned and no running error is prompted.

31. Run the following program, the output result is 4. (wrong)

def fun(n):
    if n == 1:
        return 1
    else:
        fun(n-1) * 2
print(fun(3))

Answer analysis: Recursive functions need to have a return statement to return, that is, return fun(n-1)*2.

32. Space complexity refers to the measurement of the storage space required when an algorithm is written into a program and is run on a computer. It is recorded as S(n), where n is the scale or size of the problem. ( right)

Answer analysis: Space complexity refers to the measurement of the storage space required when an algorithm is written into a program and run on a computer, recorded as S(n), where n is the scale or size of the problem.

33. Custom functions can have no parameters. ( right)

34. The divide-and-conquer algorithm will definitely use recursion. ( wrong)

Answer analysis: The divide-and-conquer algorithm does not necessarily use recursion. For example, bifurcated search also belongs to divide and conquer, but does not use recursion.

35. To facilitate subsequent code writing when calling the library, you can give it an alias, such as naming matplotlib plt. ( right)

3. Programming questions (3 questions in total, 30 points in total)

36. It is known that the calculation steps for the nth term of a certain c are as follows:

 

Xiao Ming writes a program to calculate the result of the nth item. First enter n from the keyboard, and then use the above recursion relationship to calculate the result. Please improve the code at the underline.

def fc(n):
    if n ==1:
                 ①        
    else:
        a=6*n-1
        b=8*n+3
        return          ②        
m=int(input("请输入一个整数:"))
if not isinstance(m,int):     #判断输入的m是否为整数
    print('请输入一个整数')
else:
    print(        ③        )

Reference procedure:

def fc(n):
    if n ==1:
        return 1 
    else:
        a=6*n-1
        b=8*n+3
        return  a*fc(n-1)/b
m=int(input("请输入一个整数:"))
if not isinstance(m,int):     #判断输入的m是否为整数
    print('请输入一个整数')
else:
    print(fc(m))

Note: In this question, the calculation formula for the nth item of c is known. The value of c is 1 for the first item. The second item can be calculated based on the formula for the first item. The function fc is written in the program to realize the calculation result of c. , in function fc, the 1st item returns 1, so the code at ① is return 1; the code at ② in function fc is to calculate the nth data. From the recursion formula, we can know that the code is a*fc(n-1 )/b, the code at ③ is to call the function fc to calculate the mth input item, so the code there is fc(m).

Grading:

(1) return 1; (3 points)

(2)a*fc(n-1)/b;(3分)

(3) fc(m). (2 minutes)

37. Please complete the following code and program to output the multiplication table recursively.

  

 

def get_result(num):
    if num == 1: 
        print("        ①        ") 
     else: 
        get_result(        ②        ) 
        for i in range(1,        ③        ): 
            print("%d * %d = %d" % (i, num, i * num), end="  ") 
        print()
get_result(        ④        )

Reference procedure:

def get_result(num): 
    if num == 1:
        print("1 * 1 = 1") 
    else:
        get_result(num - 1)
        for i in range(1,num + 1): 
            print("%d * %d = %d" % (i, num, i * num), end="  ")
        print()
get_result(9)

Grading:

(1)1*1=1; (3 points)

(2)num-1; (3 points)

(3) num+1; (2 points)

(4)9. (2 minutes)

   

38. There is a snail at the bottom of a well, which is n meters deep. The snail climbs up a meter every day and slides back b meters.

Please receive input integers n, a, b from the keyboard, separated by spaces.

And complete the following procedure to calculate how many days it will take the snail to climb out of the well.

n,a,b=input("请输入井深,蜗牛每天往上爬几米,蜗牛滑下几米:").        ①        
n,a,b=        ②        
pos=0
i=0
while        ③        :
             ④        
             ⑤        
print("第%d天,蜗牛距离井口%d米;"%(i,n-pos))
print("第%d天,蜗牛成功离开了深井!!"%(        ⑥        ))

Reference procedure:

n,a,b=input("请输入井深,蜗牛每天往上爬几米,蜗牛滑下几米:").split()
n,a,b=int(n),int(a),int(b)
pos=0
i=0
while pos+a<n:
    i+=1
    pos+=(a-b)
print("第%d天,蜗牛距离井口%d米;"%(i,n-pos))
print("第%d天,蜗牛成功离开了深井!!"%(i+1))

Grading:

(1) split(); (2 minutes)

(2)int(n),int(a),int(b);(2分)

(3)pos+a<n;(2分)

(4) i+=1; (2 minutes)

(5) pos+=(ab); (2 points)

(6) i+1. (2 minutes)

   

Guess you like

Origin blog.csdn.net/m0_46227121/article/details/131207814