【選擇題】
【D】01.執行下列Python程式,螢幕上輸出的數字依序為何? (A)0,0,8.0,4.0 (B)0,0,8.0,8.0,4.0,4.0 (C)0,8.0,4.0 (D)0,8.0,8.0,4.0,4.0。[109地方四等資處]
list = [0, 1, 2]
result, num = 0, 8
for item in list: |
8, 0 |
8, 1 |
8, 2 |
try: |
|
|
|
result =
num / item |
8 / 0 → F |
8 / 1 → T |
8 / 2 → T |
print(result) |
|
8.0 |
4.0 |
except: |
|
|
|
print(0) |
0 |
|
|
else: |
|
|
|
print(result) |
|
8.0 |
4.0 |
【B】02.若執行以下的Python程式碼,則螢幕上輸出的數字依序為何?
for a in range(3, 9):
for b in range(2, a):
if a % b == 0:
print(b)
(A)2,2,3 (B)2,2,3,2,4
(C)3,2,4,5,2,3,6,7,2,4,8
(D)3,2,4,5,2,3,6,7,2,4,8,3,9。[109普考資處]
a |
b(2, a) |
a % b == 0 |
Print(b) |
a |
b(2, a) |
a % b == 0 |
Print(b) |
3 |
2 |
F |
|
7 |
2 |
F |
|
4 |
2 |
T |
2 |
|
3 |
F |
|
|
3 |
F |
|
|
4 |
F |
|
5 |
2 |
F |
|
|
5 |
F |
|
|
3 |
F |
|
|
6 |
F |
|
|
4 |
F |
|
8 |
2 |
T |
2 |
6 |
2 |
T |
2 |
|
3 |
F |
|
|
3 |
T |
3 |
|
4 |
T |
4 |
|
4 |
F |
|
|
5 |
F |
|
|
5 |
F |
|
|
6 |
F |
|
|
|
|
|
|
7 |
F |
|
【B】03.若執行以下的Python程式碼,則螢幕上輸出的數字依序為何? (A)3,3,7,3 (B)3,3,7,7 (C)3,7,3,3 (D)3,7,7,7。[110地方四等資處]
list = [1, 2, 3, 4]
def function(list): |
|
|
|
print(list[2]) |
|
3 |
|
list[2] = 7 |
|
|
list = [1, 2, 7, 4] |
print(list[2]) |
|
7 |
|
print(list[2]) |
3 |
|
|
function(list) |
|
|
|
print(list[2]) |
|
7 |
輸出3, 3, 7, 7 |
【D】04.執行下列Python程式後,產生的輸出為何? (A)10 20 (B)20 30 (C)[10, 20, 30] (D)[20, 10, 30]。[110地方四等電子]
def unknown(input, p1, p2): → 定義input[p1]和input[p2]互換
input[p1],
input[p2] = input[p2], input[p1]
return input
input = [10, 20, 30]
print(unknown(input, 0, 1)) → 將input[0]和input[1]互換,10和20互換
【B】05.執行下列Python語言程式碼片段後,產生的輸出為何? (A)0 (B)10 (C)20
(D)40。[110鐵路員級]
a = 40
y = 10
if (a < 50): → 40 < 50 = True
if (a <
30): → 40 < 30 = False
y = y + 10
else:
y = y - 10
print(y) → 輸出10
【B】06.若執行以下的Python程式碼,則func()方法會被呼叫幾次? (A)6 (B)7 (C)8
(D)9。[111地方四等資處]
number = 5
def func(var):
if var>=0:
return var * func(var-1) → |
第2次 5
* func(4) 第3次 4
* func(3) 第4次 3
* func(2) 第5次 2
* func(1) 第6次 1
* func(0) 第7次 0
* func(-1) |
else:
return 1
func(number) → 第1次 func(5)
【B】07.若執行以下的Python程式碼,螢幕上的輸出結果依序為何? (A)11 11 (B)11 10
9 (C)11 10 9 12 11 (D)11 10 9 12 11 13。[111普考資處]
var1 = 4
var2 = 7
while var1 < 8: |
4 < 8 True |
|
|
|
while var2
> var1: |
7 > 4 True |
6 > 4 True |
5 > 4 True |
4 > 5 False |
print(var1
+ var2) |
4 + 7 = 11 |
4 + 6 = 10 |
4 + 5 = 9 |
|
var2 =
var2 - 1 |
7 - 1 = 6 |
6 - 1 = 5 |
5 - 1 = 4 |
|
var1 = var1
+ 1 |
|
|
4 + 1 = 5 |
|
【B】08.若執行以下的Python程式碼,螢幕上的輸出結果依序為何? (A)True True
(B)True False (C)False True (D)False False。[111普考資處]
var1 = 4
var2 = 30
var3 = 60
var4 = var1 == 4 and var2 >30
or var3 > 50
var5 = var1 != 2 and not var2
> 26 and var3 >= 80
print(var4, var5)
var4 = var1 == 4 and var2
>30 or var3 > 50
→ var4 = T and F or T
→ var4 = F or T
→ var4 = T
var5 = var1 != 2 and not var2 > 26
and var3 >= 80
→ var5 = T and not T and F
→ var5 = T and F and F
→ var5 = F and F
→ var5 = F
【C】09.執行下列的Python程式,產生的輸出為何? (A)0 (B)4 (C)10
(D)24。[111普考電子]
Def gg(x):
if(x==0):
return 0
else:
return
x+gg(x-1)
→ 4+3+2+1=10
print(gg(4))
【A】10.若執行以下的Python程式碼
var1 = 10
var2 = 5
var3 = 7
var4 = 12
var5 = var1 >= var2 and var3 != var4 → True and
True → True
var6 = var1 == var2 or var3 < var4 → False or
True → True
print(var5, var6)
螢幕上的輸出結果依序為何? (A)True True
(B)True False (C)False True (D)False False。[112普考資處]
【A】11.下列Python程式的輸出為何? (A)[3,5,6,9,10,12,15,18]
(B)[3,5,6,9,10,12,18,20] (C)[3,5,6,9,10,12,15,18,20] (D)[3,5,6,9,10,12,15,15,18,20]。[112關務四等]
answer = []
for value in range(3,20): → 介於3≦value<20之間
if (value % 3
== 0) or (value % 5 == 0): → 3或5的倍數
answer.append(value)
print(answer) → 3,5,6,9,10,12,15,18
【B】12.若執行以下的Python程式碼,則螢幕上的輸出結果為何? (A)5 (B)9 (C)13 (D)17。[112關務四等]
var1 = 10
var2 = 6/2*3 → 3*3 → 9
var3 = 16**1/2 → 16/2 → 8
print (var1-var2+var3) → 10-9+8 → 9