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

2023-05 Python Level 2 Real Exam
Questions: 37
Score: 100
Test Duration: 60min

1. Multiple-choice questions (25 questions in total, 50 points in total)
1. Run the following program. If the numbers entered successively through the keyboard are 1 and 3, what is the output result? (D) (2 points)

a=int(input())
b=int(input())
if a < b:
    a=b
print(a)


A.    3 1
B.    1 3
C.    1
D.    3

2. Run the following program. What is the output result? (C) (2 points)

n=10
s=0
m=1
while m<=n:
    s=s+m
    m=m+1
print(s)

A. 45
B. 50
C. 55
D. 60
Answer analysis: This question tests the while loop. The program's consciousness is to calculate 1+2+3+......+10. The result is 55. The answer is C.

3. What is the output of the following code? (A) (2 points)

vlist = list(range(5))
for e in vlist:
    print(e,end=",")

A. 0,1,2,3,4,
B. [0, 1, 2, 3, 4]
C. 0 1 2 3 4
D. 0;1;2;3;4;
Answer analysis: list(range The value of (5)) is [0,1,2,3,4]; the following program takes out the values ​​in the list one by one, so choose A.

4. What is the output of the following program? (B) (2 points)

n=0
while n<10:
    n=n+2
    if n==6:
        continue
print(n)

A. 5
B. 10
C. 6
D. 8
Answer analysis: At the beginning of the last loop, the value of n was 8, but after the n=n+2 operation, n became 10, so the final result is 10 .

5. Run the following program, what is the output result? (B) (2 points)

numbers=[1,2,3,4]
numbers.append([5,6,7,8])
print(len(numbers))

A. 4
B. 5
C. 8
D. 12
Answer analysis: According to the meaning of the question, [5,6,7,8] is added to the original list as a whole element, so the original list length will become 5.

6. What is the output of the following code? (D) (2 points)

s=[4,2,9,1]
s.insert(3,3)
print(s)

A. [4,2,9,1,2,3]
B. [4,3,2,9,1]
C. [4,2,9,2,1]
D. [4,2,9, 3,1]
Answer analysis: According to the meaning of the question, insert 3 into the element position with index 3 in the list (the fourth element), so choose D.

7. Regarding the following procedures, which statement is correct? (C) (2 points)

num=0
while num<10:
    print("=")

A. No matter what is added to the while code block, the infinite loop cannot be changed.
B. Adding a line of code num-=1 to the while code block can change the infinite loop
. C. This while loop is an infinite loop and will always print "="
D . The running result prints out 10 "=" statements.
Answer analysis: This question tests the while conditional loop. Adding num += 1 in the loop can change the infinite loop and exclude A; add a sentence num -= 1, and num starts from 0. Self-decrementing, num<10 is always true, it is still an infinite loop, exclude B; num value has not changed, num<10 is always true, it will always print "=", D is excluded, so choose C.

8. Run the following program, what is the output result? (D) (2 points)

list5=['1','2','4','6','9']
print(list5[2])

A.    1
B.    2
C.    3
D.    4

9. Which of the following statements is correct? (D) (2 points)
A. The element values ​​of the tuple can be modified at will
B. You can use the del statement to delete an element in the tuple
C. tup1=(5) is a legal tuple
D. tup1=(' turtle','fish',65536) is a legal tuple
Answer analysis: The element value of the tuple cannot be modified; the del statement can only delete the entire tuple, not a certain element; when the tuple contains only one element ,

10. In Python language, what is the value of the expression [1,2]*2? (C) (2 points)
A. [2,4]
B. 6
C. [1,2,1,2]
D. [1,2,2]
Answer analysis: The multiplication operation of a list is to multiply the elements in the list Repeat N times (N is the multiplier); so choose C.

11. In Python language, what is the value of expression [2] in [1,2,3,4,5]? (D) (2 points)
A. 0
B. 1
C. True
D. False
Answer analysis: [2] is a list. Although there is 2 in [1,2,3,4,5], this 2 is not List, but integer data, so the return value is False.

12. d={"Wang Ming":178,"Zhang Hua":158,"Yu Fei":189,"Liu Ying":164}, what is the value of d["Yu Fei"]? (D) (2 points)
A. -2
B. 2
C. 'Yu Fei'
D. 189
Answer analysis: d["Yu Fei"] represents the value with the key "Yu Fei" in the dictionary, so choose D .

13. Which of the following statements is correct? (C) (2 points)
A. Dictionaries can only store strings, not other types of objects
B. dict11={:} can create an empty dictionary
C. {123:456} is a legal dictionary
D. Dictionary The value must be unique, and the key does not have to be unique.
Answer analysis: A dictionary can store any type of object; the keys and values ​​in the dictionary must be separated by colons, but an empty dictionary does not need to be separated by colons. {} can create a Empty dictionary; dictionary keys must be unique, values ​​do not have to be unique

14. Run the following program. How many hellos are output in total? (C) (2 points)

for i in range(3): 
  print("hello")

A. 1
B. 2
C. 3
D. 4
Answer analysis: The loop is executed 3 times, so 3 hellos are output.
    
15. What is the result of the following program? (C) (2 points)

lis1=["cat","tomato","dog","apple","dog","dog"]
print(lis1.index("dog"))

A. 0
B. 1
C. 2
D. 3
Answer analysis: Examine and obtain the subscript of the first occurrence of an element in the list. The index of the first occurrence of dog is 2.

16. Run the following program. When 1 is entered from the keyboard, what is the result of the program? (A) (2 points)

str1='一二三四五六日'
strid=int(input('请输入1-7的数字:'))
print('星期'+str1[strid-1])


A. Monday
B. Monday + Monday
C. Tuesday
D. Monday + Tuesday
Answer analysis: Examine the connection of strings. The subscript in the question is subtracted by one, so if you enter 1, the returned value is Monday.

17. What is the result of running the following program? (A) (2 points)

zd1={'name':'Tom','color':'blue'}
zd1['name']='Mike'
print(zd1)

A.    {'name': 'Mike', 'color': 'blue'}
B.    {'name': 'Mike', 'color': 'blue', name': 'Tom'}
C.    {'name': 'Tom', 'color': 'blue'}
D.    {'name': Tom','name': 'Mike', 'color': 'blue', }

18. If a= (1, 2, 3), which of the following commands will result in an error? (C) (2 points)
A. a[1]
B. list(a)
C. a[1] = 4
D. a*5
Answer analysis: The elements in the tuple cannot be modified, so option C is wrong .

19.  

s='happy birthday'
print(s[13:-15:-2])

What is the result of running the above code? (B) (2 points)
A. An error will be reported when running
B. ydti pa
C. ydtipa
D. yadhtrib yppa

20. What is the result of executing the following program? (D) (2 points)

yz=(1,5,7,3,9)
list1=sorted(yz)
print(list1)

A. Error
B. 1, 3, 5, 7, 9
C. (1, 3, 5, 7, 9)
D. [1, 3, 5, 7, 9]
Answer analysis: Tuples can be sorted() Sort, but the output result is a list.

21. Which of the following functions can replace the content in a string (note: not formatted output)? (A) (2 points)
A. replace( )
B. format( )
C. split( )
D. join( )

Answer analysis: The replace() function is used for string replacement; split() is used for string splitting; join() is used for concatenation; format() is used for formatted output.
    
22. Run the following program, what is the output result? (C) (2 points)

list3=['11','4',5,1,4]
print(len(list3))

A. 1
B. 4
C. 5
D. 8
Answer analysis: The list3 list has five elements, so the return value of the len() method is 5
    
23. Which of the following statements is wrong? (A) (2 points)
A. Once a dictionary is created, it cannot be modified
B. a={} is an empty dictionary
C. {123:'123'} is a legal dictionary
D. In the same dictionary, the dictionary Keys are unique.
Answer analysis: The dictionary can be modified; {} can create an empty dictionary; the keys of the dictionary must be unique.

24. List ls=['H', 'a', 'p', 'p', 'y', '!'], which of the following statements is wrong? (C) (2 points)
A. The value of ls[:6] is ['H', 'a', 'p', 'p', 'y', '!'] B. ls[:-6
] The value of is []
C. The value of ls[6] is '!'
D. The value of ls[2:-2] is ['p', 'p']
Answer analysis: The maximum value of the ls list subscript is 5, ls[6] will refer to the element with index 6 in the ls list, and an exception will be thrown (list index out of range)

25. What is wrong with using the tuple function to create tuples? (A) (2 points)
A. tuple(20,30)
B. tuple('Hello')
C. tuple([2,0,1,3])
D. tuple('12345')
Answer analysis: tuple () can convert iterable objects such as lists, strings, and dictionaries into tuples. Integers are not iterable objects.

2. True or False Questions (10 questions in total, 20 points in total)
26. The append() method can add an element to the end of the list. ( right)

27. The following program is written correctly. (wrong)

score=50
if score>=60:
    print("合格")
else:
    print("不合格")

28. Decide whether the following statements are correct. (Yes)
 >>>book={'Grimm's Fairy Tales':1001,'Andersen's Fairy Tales':1002}

29. Decide whether the following statements are correct. (wrong) 

if 5>3 
  print("5大于3")

30. In a while loop, if you want to return to the beginning of the loop, you can use the break statement. (Wrong)
Answer analysis: Use the continue statement to return to the beginning of the loop

31. Execute the following program and the result is: (wrong)

3
3
3
for i in range(1,5,2): 
    print(3)

32. The output result of executing print("e" in "hello") is True. (Correct)
Answer analysis: in is a member operator. If the string contains the given character, it returns True
    
33. (3) The result of in (1, 2, 3) is True. (Correct)
Answer analysis: (3) There is no comma, it does not represent a tuple, it represents 3 of the integer type.
    
34. The code print('{}-{}*{}={}'.format(20,3,4,8)) prints the result 20-3*4=8. (Correct)
Answer analysis: According to the rules of format operation: print('{}-{}*{}={}'.format(20,3,4,8)) is the following (20,3,4, 8) Fill in the numbers in the previous braces in sequence.
    
35. Tuples are immutable sequences, lists are mutable sequences. (right)

3. Programming questions (2 questions in total, 30 points in total)
36. Weight comparator 
requirements: Please program to implement the following functions:
(1) When the program starts running, remind the user to enter the names and weights of three people (can be entered separately, each time Enter the name or weight);
(2) The program automatically compares to find the name and weight of the heaviest person;
(3) The output format is not limited, but the name and weight information of the heaviest person must be included.

Reference procedure 1:

w1 = int(input("请输入第一个人的体重:"))
n1 = input("请输入第一个人的名字:")

w2 = int(input("请输入第二个人的体重:"))
n2 = input("请输入第二个人的名字:")

w3 = int(input("请输入第三个人的体重:"))
n3 = input("请输入第三个人的名字:")
maximum = w1
m_name = n1

if w2 > maximum:
    maximum = w2
    m_name = n2

if w3 > maximum:
    maximum = w3
    m_name = n3

print("体重最重的人是:",m_name)
print("他的体重是:",maximum)

Reference procedure 2:

name=[]
weight=[]
for i in range(3):
    a = input('请输入姓名:')
    b = int(input('请输入体重:'))
    name.append(a)
    weight.append(b)
c = max(weight)
d = weight.index(c)

print("体重最重的人是:",name[d])
print("他的体重是:",c)

Scoring criteria:
(1) According to the meaning of the question, enter the names and weights of three people respectively; (0.5 points for each item, 3 points in total) ( 
2) Conditional statements either use sorting or maximum value calculation; (4 points) 
( 3) Use variables to store weight and name; (4 points)
(4) There is data type conversion; (1 point for each conversion, 3 points in total)
(5) The results are output correctly. (1 point)

37. Count the number of specified characters in the statement.
Requirements:
(1) The statement to be counted is: Were you born on August 21, 1994
(2) Conditional statements are required to count the sum of the numbers of all English letters and numbers in the sentence. (Excluding spaces, commas and other punctuation marks);
(3) Output an integer representing the sum of all English letters and numbers.

Reference procedure: 

str = 'Were you born on August 21, 1994' 
n = 0 
for i in str: 
    if i==' ' or i==',': 
        continue 
    else:
        n+=1
print(n)

Scoring criteria: 
(1) Can store strings correctly; (2 points) 
(2) Create counting variables; (2 points)
(3) Have a loop to traverse strings (or iterate objects); (2 points)
(4) Judge characters value; (2 points)
(5) Counting is performed correctly in the loop; (2 points)
(6) The output format is correct; (2 points)
(7) The code execution is completely correct. (3 points)
 

Guess you like

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