跳到主要內容
技藝競賽 / 自編學科模擬練習試題六

Python程式設計學科模擬試題

1.     執行以下 Python 程式片段,其結果為何?

a = 0x5A

b = 0x3C

result = (~(a ^ b)) & 0xFF

print(result % 4)

(A) 0 (B) 1 (C) 2 (D) 3

2.     執行以下 Python 程式片段,其結果為何?

def calc(x, y):

    if x <= 0 or y <= 0:

        return 1

    if (x + y) % 2 == 0:

        return calc(x - 1, y) + 2

    else:

        return calc(x, y - 1) + 1

print(calc(3, 3) % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

3.     執行以下 Python 程式片段,其結果為何?

s = "AABBAABBAA"

cnt = 0

for i in range(len(s) - 2):

    if s[i:i+3] == "AAB" or s[i:i+3] == "BBA":

        cnt += 1

print(cnt % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

4.     關於二維陣列(動態規劃狀態轉移),執行以下程式片段後其結果為何?

dp = [[0] * 4 for _ in range(4)]

for i in range(4):

    dp[i][0] = 1

    dp[0][i] = i

for i in range(1, 4):

    for j in range(1, 4):

        dp[i][j] = (dp[i-1][j] + dp[i][j-1]) * 2

print(dp[3][3] % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

5.     執行以下 Python 程式片段,其結果為何?

# 模擬雙指標 (Two Pointers)

left, right = 0, 9

arr = [1, 3, 5, 7, 9, 10, 8, 6, 4, 2]

ans = 0

while left < right:

    if arr[left] < arr[right]:

        ans += arr[left]

        left += 1

    else:

        ans += arr[right]

        right -= 1

print(ans % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

6.     執行以下 Python 程式片段,其結果為何?(注意運算子優先權)

val = 12 | 6 ^ 3 & 9

print(val % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

7.     執行以下 Python 程式片段,其結果為何?

data = [i for i in range(1, 20) if i % 2 != 0]

# data [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

res = data[1:8:2]

print(sum(res) % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

8.     執行以下 Python 程式片段(記憶化遞迴與動態規劃邏輯),其結果為何?

memo = {}

def f(n):

    if n in memo: return memo[n]

    if n <= 1: return 1

    if n % 2 == 0:

        memo[n] = f(n-1) + 1

    else:

        memo[n] = f(n-2) + 2

    return memo[n]

print(f(7) % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

9.     執行以下 Python 程式片段,其結果為何?

total = 0

for i in range(1, 5):

    for j in range(1, 5):

        if (i + j) % 3 == 0:

            continue

        total += i * j

print(total % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

10. 執行以下 Python 程式片段,其結果為何?

A = {1, 2, 3, 4, 5}

B = {4, 5, 6, 7, 8}

C = {3, 4, 7, 9}

result = (A ^ B) & C

print(sum(result) % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

11. 執行以下 Python 程式片段,其結果為何?

matrix = [[i * j for j in range(3)] for i in range(3)]

ans = 0

for r in range(3):

    ans += matrix[r][2 - r]

print(ans % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

12. 執行以下 Python 程式片段,其結果為何?

def check(n):

    cnt = 0

    while n > 0:

        cnt += (n & 1)

        n >>= 1

    return cnt

# 計算 1 7 所有數字的二進位 1 的總個數

total_ones = sum(check(i) for i in range(1, 8))

print(total_ones % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

13. 執行以下 Python 程式片段,其結果為何?

s = "COMPETITION"

# 取出反轉與間隔切片

sub = s[1:9:2][::-1]

print(len(sub) % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

14. 執行以下 Python 程式片段,其結果為何?

import math

# 模擬對數與無條件進位運算

val = math.ceil(math.log2(20)) + math.floor(math.log10(150))

print(val % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

15. 執行以下 Python 程式片段,其結果為何?

# 字典與 Lambda 排序複合操作

data = {'a': 5, 'b': 2, 'c': 8, 'd': 1}

sorted_keys = sorted(data, key=lambda x: data[x], reverse=True)

# 取出排序後前兩名對應的數值總和

ans = data[sorted_keys[0]] + data[sorted_keys[1]]

print(ans % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

16. 執行以下 Python 程式片段,其結果為何?

# 數字位數乘積與基礎迴圈

num = 1234

prod = 1

while num > 0:

    digit = num % 10

    if digit % 2 == 0:

        prod *= digit

    num //= 10

print(prod % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

17. 執行以下 Python 程式片段,其結果為何?

# 模擬字串格式化與取代

text = "A1B2C3D4"

new_text = "".join([ch for ch in text if ch.isalpha()])

print(len(new_text) % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

18. 執行以下 Python 程式片段,其結果為何?

# 費氏數列變體

a, b, c = 1, 1, 2

for i in range(5):

    a, b, c = b, c, a + b + c

print(c % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

19. 執行以下 Python 程式片段,其結果為何?

# 列表推導式與巢狀條件

res = [x * y for x in range(1, 4) for y in range(1, 4) if (x + y) >= 4]

print(sum(res) % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

20. 執行以下 Python 程式片段,其結果為何?

# 模擬巴斯卡三角形(組合數)

def pascal(row, col):

    if col == 0 or col == row:

        return 1

    return pascal(row - 1, col - 1) + pascal(row - 1, col)

print(pascal(5, 3) % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

21. 執行以下 Python 程式片段,其結果為何?

# 進位制轉換與字串長度

val = bin(43) # 43 轉換為二進位字串

# 註:bin(43) 的格式為 '0b......'

print(len(val) % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

22. 執行以下 Python 程式片段,其結果為何?

# 二分搜尋法的內部變數追蹤

arr = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]

target = 23

low, high = 0, len(arr) - 1

steps = 0

while low <= high:

    steps += 1

    mid = (low + high) // 2

    if arr[mid] == target:

        break

    elif arr[mid] < target:

        low = mid + 1

    else:

        high = mid - 1

print(steps % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

23. 執行以下 Python 程式片段,其結果為何?

# 矩陣相乘與基礎加總

A = [[1, 2], [3, 4]]

B = [[2, 0], [1, 2]]

C = [[0, 0], [0, 0]]

for i in range(2):

    for j in range(2):

        for k in range(2):

            C[i][j] += A[i][k] * B[k][j]

print(C[1][0] % 4)

(A) 0 (B) 1 (C) 2 (D) 3

 

24. 執行以下 Python 程式片段,其結果為何?

# 不確定參數與邏輯短路 (Short-circuit Evaluation)

def check_a():

    return True

def check_b():

    return 1 // 0 == 0 # 這一行若單獨執行會引發 ZeroDivisionError

try:

    ans = check_a() or check_b()

    print(1 if ans else 0)

except ZeroDivisionError:

    print(2)

(A) 0 (B) 1 (C) 2 (D) 3

 

25. 執行以下 Python 程式片段,其結果為何?

# 綜合位元遮罩 (Bit Mask) 運算

mask = 0b1111

val = 27  # 二進位 11011

result = (val >> 2) ^ mask

print(result % 4)

(A) 0 (B) 1 (C) 2 (D) 3

消息公佈欄

時間類別單位標題發佈點閱
跳至網頁頂部