Uploaded by martiny11211

期中考的程式考題 選擇題答案

advertisement
選擇題
一、 你要設計一個 Python 程式來檢查使用者輸入的數字是 1 位數、2 位數還
是 2 位數以上,其中規定輸入的值必須是正整數。你要如何完成這段程式碼?
num=int(input(“Enter a number with 1 or 2 digits:”))
digits=”0”
if num >0:
(1)
digits=’1’
(2)
digits=’2’
(3)
digits=’>2’
print(digits + “digits.”)
else:
print(“The number is not a positive number”)
(1) A. if num<10: B. if num<10 C. if num>-10 and num<10: D. if num>-100
and num <100:
(2) A. if num<100: B. if num <100 C. elif num<100: D. elif num>-100 and
num<100:
(3) A. elif num >100 B. if: C: else: D: elif:
二、同事開發一個程式將產品名稱輸入到資料庫的程式,但其中發生錯誤,讓
每個產品存入的名稱都顛倒字母順序,請你開發一個 Python 程式,將每個產品
名稱以正常的順序輸出
backwards_pname=input(“請輸入顛倒字母順序的產品名稱:”)
forwards_pname=””
for i in
(4) :
forwards_pname +=
(5)
print(forwards_pname)
(4) A. backwards_pname B. len(backwards_pname)
C. range(0,len(backwards_pname),-1)
D. range(len(backwards_pname)-1,-1,-1)
(5) A. backwards_pname[i-1] B. backwards_pname[len(forward_pname)-1]
C. backwards_pname[len(backwards_pname)-len(forwards_pname)]
D. backwards_pname[i]
二、你要設計一個 Python 程式來驗證產品編號,產品編號的格式必須是 dddddd,並且只含數字和破折號,如果格式正確,則程式必須列印 True,如果格
式不正確,則須列印 False,你要如何完成下面的程式碼?
product_no=””
(6)
(7)
product_no=input(“Enter product number(dd-dddd):”)
parts=product_no.split(‘-‘)
if len(parts)==2:
if len(parts[0])==2 and len(parts[1])==4:
if parts[0].isdigit() and parts[1].isdigit():
(8)
print(valid)
(6) A. while product_no ==””: B. while product_no != “”:
(7) A. valid=False B. valid=True
(8) A. valid=False B. valid=True
(9) print(list(range(2,-11,-3))) 結果為 A. [2, -1, -4, -7]
1, -4, -7, -10] D. [2, -1, -7, -10]
(10) for i in range(1,11):
if i==6:
break
print(i,end=’ ’)
請問印出? A. 1 2 3 4 5 7 8 9 10
B. 1 2 3 4 5
B. [2, -1, -4, -7, -10]
C. [-
C. 1 3 5 7 9 D. 2 4 6 8 10
(11)你為學校設計一個 python 應用程式,在 classroom 的清單中包含 60 個同學
的姓名,最後 3 名是班上幹部,你需要分割清單內容顯示除了幹部以外的所有
同學,你可以利用下面哪一個程式碼達成?
A. classroom[0:-2]
B. classroom[1:-3]
C. classroom[0:-3]
D. classroom[0:-4]
(12) 當我們要使用 math 模組前須使用下列哪個指令?
A. input math B. print math C. type math
D. import math
(13) list1=[1,2,3]
list2=[4,5,6]
list3=list2+list1
list4=list3*2
print(list4)
A. [[4,5,6],[4,5,6],[1,2,3],[1,2,3]]
B. [1,2,3,4,5,6,1,2,3,4,5,6]
C.[4,5,6,1,2,3,4,5,6,1,2,3] D.[[4,5,6,1,2,3],[4,5,6,1,2,3]]
(14) listA=[1,4,12,3,24]
print(‘12’ in listA)
A. True B. False
Ans:
1.A 2.C 3.C 4. D 5.D 6.A 7.A
8.B 9.B 10.B 11.C 12.D 13.C 14.B
Download