113 年 國立政治大學資訊管理學系碩士班科技組《計算機概論》
第 1 題4 分
The CAP theorem explains some of the competing requirements in a distributed system with replication. Which statements are accurate regarding this theorem?
A) C means "Consistency", which guarantees that the nodes will have the same copies of a replicated data item in various transactions.
B) A means "Atomicity", which guarantees that each transaction is a single unit that either succeeds completely or fails completely.
C) P means "Partition tolerance", which guarantees that the system can continue operating even if the network fails and the nodes form disconnected partitions.
D) Distributed databases like MongoDB and Cassandra tend to prioritize "C" and "P" at the cost of sacrificing "A".
E) A MySQL database configured in the Master-Slave setting satisfies "C" and "A" but compromises "P".
(1) AB
(2) AC
(3) CD
(4) DE
(5) BD
登入後即可作答並保存紀錄。
核心觀念
CAP 定理指出,在必須容忍網路分割 (Partition tolerance, P) 的情況下,分散式系統只能同時保證 一致性 (Consistency, C) 或 可用性 (Availability, A) 其中之一,三者不可兼得。理解每個選項所描述的概念是否符合 CAP 的定義,即可判斷正確答案。
(a) 各敘述的正誤判斷
| 選項 | 內容 | 判斷 | 理由 |
|---|---|---|---|
| A | C = “Consistency”,保證所有節點在多筆交易中持有相同的資料副本。 | ✅ 正確 | 一致性要求在寫入完成後,所有節點對同一資料項的讀取結果相同,等同於「所有副本保持相同」的概念。 |
| B | A = “Atomicity”,保證交易要麼全部成功要麼全部失敗。 | ❌ 錯誤 | CAP 中的 A 代表 Availability(系統能對每個請求回應成功或失敗),而非資料庫的 Atomicity(原子性)。 |
| C | P = “Partition tolerance”,系統即使在網路分割、節點斷開時仍能持續運作。 | ✅ 正確 | 分割容忍正是 CAP 中 P 的定義。 |
第 2 題4 分
Which statements about SQL and NoSQL databases are most accurate?
A) SQL is based on a structured query language with a fixed schema, while NoSQL is schema-less and can store unstructured data.
B) NoSQL is designed to handle distributed data stores, making them a common choice for cloud storage and big data applications.
C) Database normalization (i.e. 1NF, 2NF, 3NF, and 4NF) does not apply to NoSQL databases.
D) NoSQL databases often sacrifice some of the ACID (Atomicity, Consistency, Isolation, Durability) properties for better scalability and performance.
E) SQL is predominantly better at handling complex queries due to its rigid and well-defined schema.
(1) ABD
(2) BCD
(3) ABC
(4) ABDE
(5) ABCDE
登入後即可作答並保存紀錄。
核心觀念
本題測驗的是 SQL(關聯式)與 NoSQL(非關聯式)資料庫的基本特性與設計取捨,涵蓋 schema、分散式架構、正規化與 ACID 等概念。掌握這些概念即可判斷每個敘述的正確性。
(a) 各選項的判斷與說明
| 選項 | 內容 | 判斷 | 為什麼 |
|---|---|---|---|
| A | SQL is based on a structured query language with a fixed schema, while NoSQL is schema‑less and can store unstructured data. | 正確 | 關聯式資料庫必須先定義表格結構(欄位、資料型別),且以 SQL 作為查詢語言。NoSQL 多採用 schema‑less(或靈活 schema)設計,允許儲存 JSON、BLOB 等非結構化資料。 |
| B | NoSQL is designed to handle distributed data stores, making them a common choice for cloud storage and big data applications. | 正確 | 許多 NoSQL(如 Cassandra、MongoDB、DynamoDB)天生支援水平擴充與多節點分散,適合雲端與大數據情境。 |
| C | Database normalization (i.e. 1NF, 2NF, 3NF, and 4NF) does not apply to NoSQL databases. | 錯誤 | 正規化是關聯式資料庫設計的理論基礎,NoSQL 可以 參考正規化原則,但通常會 有意 反正規化以提升效能。說「不適用」過於絕對,並非事實。 |
第 3 題4 分
Which statements are most accurate regarding network protocol behavior and standards?
A) OSPF is a routing protocol that calculates the shortest path for data packets to travel within an IP network using a path cost metric.
B) ARP operates at the Internet layer to translate network addresses such as IP addresses into physical MAC (Media Access Control) addresses.
C) ICMP is used for establishing and managing session states, often implemented at the Transport layer, alongside TCP and UDP.
D) HTTP/2 introduces multiplexing of requests over a single TCP connection to reduce the amount of required connections.
E) SSL/TLS protocols work at the Network layer to provide secure encryption capabilities for data packets transmitted across networks.
(1) AB
(2) BC
(3) AD
(4) CD
(5) DE
登入後即可作答並保存紀錄。
核心概念
本題測驗 OSI/Internet 模型各層的協定功能與定位,必須知道以下要點:
- OSPF 為 Link‑State 路由協定,屬於 Network (Internet) 層,以「成本」(cost) 計算最短路徑。
- ARP 用於把 IP 位址 ↔ MAC 位址 的映射,屬於 Data Link (Network Access) 層,不是 Internet 層。
- ICMP 只負責 錯誤回報、診斷訊息(如 ping),屬於 Network (Internet) 層,不參與會話管理。
- HTTP/2 在 Application 層,引入 單一 TCP 連線的多路復用,減少連線數。
- SSL/TLS 提供 端點到端點的加密,位於 Transport 層之上(或視為 Session/Application 層),而非 Network 層。
(a) 各選項的正確性分析
| 選項 | 內容 | 判斷 | 理由 |
|---|---|---|---|
| A | OSPF 是一種路由協定,使用路徑成本 metric 計算 IP 網路內的最短路徑。 |
第 4 題4 分
What are the pros and cons of using threads compared to processes in parallel programming?
A) Threads lead to more efficient system resource utilization than separate processes.
B) Threads have lower context-switch overhead because they do not need to switch the memory space.
C) Processes are more resilient to errors since one process crashing does not affect other processes. In contrast, thread errors can cause the entire process to crash.
D) Threads need more complex synchronization mechanisms to manage shared resources and to avoid concurrency issues.
E) Processes are generally easier to implement and debug, while threads can introduce complicated debugging scenarios because of shared state and asynchronous execution.
(1) ABE
(2) BCD
(3) ACE
(4) ABD
(5) ABCDE
登入後即可作答並保存紀錄。
核心觀念
本題在測試考生對 執行緒 (thread) 與行程 (process) 在平行程式設計上的優劣特性 的理解。必須比較兩者在資源利用、切換開銷、錯誤隔離、同步需求與除錯難易度等面向,判斷每個敘述是否正確,最後選出完整符合的選項組合。
解題過程
| 項目 | 敘述內容 | 判斷理由 |
|---|---|---|
| A | Threads lead to more efficient system resource utilization than separate processes. | 執行緒共享同一個程式碼段、堆疊、全域變數與檔案描述子,省去為每個行程額外配置的記憶體與系統資源,因而 資源利用率較高。 |
| B | Threads have lower context‑switch overhead because they do not need to switch the memory space. | 切換執行緒只需保存/還原 CPU 暫存器與少量執行緒本地狀態;不必改變 MMU 的頁表或重新載入虛擬位址空間,故 上下文切換成本較低。 |
第 5 題4 分
Which statements about the Unified Modeling Language (UML) are most accurate?
A) Sequence diagram shows the interaction between objects in a system by modeling messages exchanged between them over time.
B) Activity diagram shows the workflow of a system by modeling activities, actions, and control flows.
C) Use case diagram shows the functionality of a system by modeling actors, use cases, and their relationships.
D) Composition represents a "has-a" relationship, while Aggregation represents an "is-part-of" relationship between the aggregated object and the aggregate object.
E) A UML use case describes the overall behavior of the system from the perspective of the system.
(1) AED
(2) ABC
(3) BCD
(4) BDE
(5) ABCDE
登入後即可作答並保存紀錄。
核心觀念
本題測驗的是 UML(Unified Modeling Language)各圖式的語意與關係概念,重點在於辨識每個圖式的正確描述以及組合(Composition)與聚合(Aggregation)之間的差異。只要能正確判斷各敘述是否符合 UML 標準,即可選出正確的選項集合。
解題過程
| 項目 | 敘述 | 判斷依據 | 正確性 |
|---|---|---|---|
| (a) A | Sequence diagram shows the interaction between objects in a system by modeling messages exchanged between them over time. | Sequence Diagram 以時間軸(垂直)展示物件(橫向)之間的訊息傳遞,正是 UML 官方說明。 | ✅ 正確 |
| (b) B | Activity diagram shows the workflow of a system by modeling activities, actions, and control flows. | Activity Diagram 與流程圖相似,用 Activity、Action、Control Flow 來描述系統工作流程,符合 UML 定義。 | ✅ 正確 |
第 6 題4 分
Which statements are most accurate regarding cybersecurity?
A) A rootkit is a set of specialized tools for system administrators to manage and monitor system security.
B) A Zero-Day attack targets a vulnerability after the vulnerability is disclosed and before it is fixed.
C) Post-quantum cryptography exploits quantum mechanics to secure communication and enhance cryptography.
D) Social engineering is a type of attack that manipulates individuals into sharing confidential information that they should not share.
E) Two-factor authentication (2FA) is a security process aiming to prevent the Man-in-the-Middle attack.
(1) AB
(2) BC
(3) CD
(4) BD
(5) AD
登入後即可作答並保存紀錄。
核心觀念
本題測驗的是資訊安全基礎概念,重點在於辨識不同攻擊手法與防護機制的正確定義:
- rootkit、Zero‑Day、post‑quantum cryptography、social engineering、以及Two‑factor authentication (2FA) 各自的功能與目的。
了解每個名詞的正式說明即可判斷各選項的正確與否,進而選出唯一符合事實的組合。
(a) A) 「A rootkit is a set of specialized tools for system administrators to manage and monitor system security.」
說明
Rootkit(根套件)是一種惡意軟體,旨在 隱蔽 系統中的惡意活動,常藉由修改核心或系統呼叫來隱藏檔案、進程與網路連線。它的設計目的是讓攻擊者 維持持久的控制權,而非協助系統管理員。
結論:敘述與事實相反,錯誤。
【答案】✗
(b) B) 「A Zero‑Day attack targets a vulnerability after the vulnerability is disclosed and before it is fixed.」
說明
Zero‑Day(零日)漏洞指的是 供應商尚未得知、且 沒有可用的修補程式 的安全缺口。Zero‑Day 攻擊發生在 漏洞被發現但尚未公開 的階段。
一旦漏洞 已公開(disclosed),即不再稱為 Zero‑Day,而是 N‑Day 或 已知漏洞,即使尚未修補也不屬於 Zero‑Day。
結論:敘述與正確定義相左,錯誤。
【答案】✗
(c) C) 「Post‑quantum cryptography exploits quantum mechanics to secure communication and enhance cryptography.」
說明
Post‑quantum cryptography(後量子密碼學)不利用 量子力學本身,而是設計 在量子電腦面前仍然安全 的加密演算法(如格基、碼基、哈希基等)。其目的是 抵禦 量子演算法(如 Shor 演算法)對現有 RSA、ECC 的破壞,而非利用 量子效應。
結論:敘述概念錯誤,錯誤。
【答案】✗
第 7 題4 分
Which of the following statements accurately describes the mechanisms of the GPT (Generative Pre-trained Transformer) architecture?
A) GPT uses an attention mechanism to focus on relevant parts of the input sequence and to facilitate context-aware text generation.
B) GPT uses a pre-training phase on a large text corpus to learn general language patterns before fine-tuning on task-specific datasets.
C) GPT uses recurrent neural layers that process input text sequentially, retaining state information across different positions in the text.
D) GPT uses reinforcement learning to improve its predictions based on user feedback.
E) GPT relies on stacked transformer blocks that utilize both self-attention and feed-forward neural networks within each block.
(1) ABE
(2) BCD
(3) ACE
(4) BCE
(5) ABCDE
登入後即可作答並保存紀錄。
核心觀念
本題考查 GPT(Generative Pre‑trained Transformer)的核心架構與訓練流程。關鍵在於掌握:
- Transformer 的基本組件:自注意力(self‑attention)與前饋神經網路(feed‑forward)在每個堆疊的 block 中交替使用。
- 預訓練‑微調(pre‑training & fine‑tuning)策略:先在大規模語料上無監督預訓練,再針對特定任務微調。
- 非循環(non‑recurrent)特性:GPT 完全基於自注意力,沒有 RNN/LSTM 之類的循環層。
- 強化學習(RL):雖在後續的 ChatGPT 版本中加入 RLHF 以提升對話品質,但 不是 GPT 架構本身的機制。
解題步驟
| 選項 | 內容說明 | 正確與否 | 判斷依據 |
|---|---|---|---|
| A | 「使用 attention 機制聚焦輸入序列的相關部份,協助產生具上下文感知的文字」 | ✅ 正確 | GPT 利用 self‑attention 直接在同一層內對所有 token 計算相關性,實現上下文感知的生成。 |
第 8 題4 分
Which of the following statements are correct about Redundant Array of Independent Disks (RAID)?
A) RAID 0 offers no data redundancy and distributes data across all disks to improve performance through stripping.
B) RAID 1 substantially increases read and write speeds while offering complete data redundancy by mirroring data across all disks.
C) RAID 5 requires at least three disks and provides data redundancy by distributing parity information across all disks in the array.
D) RAID 6 is like RAID 5 but requires only two disks and uses double striping instead of parity for fault tolerance.
E) RAID 10 (1+0) can tolerate the failure of at least one drive in each mirrored pair and typically requires more storage capacity than RAID 5 or 6.
(1) ABE
(2) BCD
(3) ACE
(4) BCE
(5) ABCDE
登入後即可作答並保存紀錄。
核心觀念
本題測驗對 RAID 各等級的資料冗餘與效能特性之認知,包括最少磁碟數、資料與奇偶校驗的分布方式,以及容錯能力與容量利用率的比較。
(a) 檢視各選項的敘述
| 選項 | 敘述 | 正確性判斷與說明 |
|---|---|---|
| A | RAID 0 提供無資料冗餘,將資料分割寫入所有磁碟以提升效能(striping) | ✅ 正確。RAID 0 只做資料條帶化(striping),不儲存奇偶校驗或鏡像,故無容錯能力。 |
| B | RAID 1 大幅提升讀寫速度,同時以鏡像方式在所有磁碟上提供完整冗餘 | ❌ 錯誤。RAID 1 以鏡像提供完整冗餘,讀取可在兩個磁碟同時進行,確實提升讀取效能;但寫入必須同時寫入兩個磁碟,寫入速度與單一磁碟相當,甚至稍慢,故不能說「大幅提升寫入速度」。 |
| C | RAID 5 至少需要三顆磁碟,透過在所有磁碟上分散奇偶校驗資料提供冗餘 | ✅ 正確。RAID 5 需 ≥3 顆磁碟,奇偶校驗以條帶方式分散,任一磁碟失效時仍可重建資料。 |
| D | RAID 6 類似 RAID 5,但只需兩顆磁碟,且使用雙條帶(double striping)取代奇偶校驗 | ❌ 錯誤。RAID 6 必須至少 四 顆磁碟,使用 雙奇偶校驗(two independent parity blocks)而非「雙條帶」,可同時容忍兩顆磁碟失效。 |
| E | RAID 10(1+0)可容忍每組鏡像對至少一顆磁碟失效,且相較 RAID 5/6 需要較多儲存容量 | ✅ 正確。RAID 10 先做鏡像再條帶,任一鏡像組中只要有一顆磁碟存活即可,故每組至少容忍一顆失效。 |
第 9 題4 分
Which statements about software development are most accurate?
A) Waterfall development sets clear milestones in each stage and moves to the next stage only after the goal of the current stage is fulfilled.
B) Waterfall development appreciates a high degree of customer involvement so that the project outcome can be adjusted in a timely manner.
C) Waterfall development is more suitable than Agile when multiple software components must be designed in parallel for final integration.
D) Agile allows the team members to be involved with other work depending on the phases, while Waterfall demands highly devoted team members throughout the development.
E) Agile relies on careful documentation and testing to ensure the quality and understanding of the software deliverables.
(1) AD
(2) BC
(3) AE
(4) AC
(5) CE
登入後即可作答並保存紀錄。
(a) 本題核心觀念
本題在測驗 Waterfall 與 Agile 兩種軟體開發流程的特性差異。必須能分辨:
- 階段性與里程碑的嚴格順序(Waterfall)
- 客戶參與程度與需求變更的彈性(Agile)
- 工作分配與角色彈性(Agile) vs. 穩定投入(Waterfall)
- 文檔與測試的重視程度
(b) 各選項正確性判斷
| 選項 | 內容 | 判斷 | 理由 |
|---|---|---|---|
| A | Waterfall development sets clear milestones in each stage and moves to the next stage only after the goal of the current stage is fulfilled. | ✅ 正確 | Waterfall 採用 線性、階段式 開發(需求 → 設計 → 實作 → 測試 → 部署),每階段皆設有明確的 milestone,必須完成當前階段目標後才能進入下一階段。 |
| B | Waterfall development appreciates a high degree of customer involvement so that the project outcome can be adjusted in a timely manner. | ❌ 錯誤 | Waterfall 在 需求階段 後客戶參與度大幅下降,變更成本高,無法「即時」調整成果。 |
第 10 題4 分
What statements about open-source licenses are most accurate?
A) If the software uses GPL-licensed code, it must disclose source code when it is distributed.
B) The MIT license requires including the copyright and permission notice in all copies of the software.
C) The Apache license allows the software to be patented but prohibits using the licensor's trademark.
D) GPL allows both the right to patent the software and the right to use the licensor's trademark.
E) MIT and Apache allow commercial use of the licensed software, but GPL does not.
(1) ABDE
(2) ABCE
(3) ABCD
(4) BCDE
(5) ABCDE
登入後即可作答並保存紀錄。
核心觀念
本題在測驗對常見開源授權(GPL、MIT、Apache)之權利與限制的認識,尤其是 源碼公開、版權聲明、專利授與、商標使用與商業使用 四個重點。
(a) 各選項之判斷與說明
A) If the software uses GPL‑licensed code, it must disclose source code when it is distributed.
- 正確。 GPL(不論 2.0 或 3.0)規定,只要以二進位或其他形式散佈(distribution)包含 GPL 程式碼的作品,散佈者必須同時提供相同授權條件的完整原始碼(或提供取得原始碼的明確方式)。
- 依據 GPLv2 第 3 條與 GPLv3 第 4 條的「source‐code provision」要求,未提供原始碼即違反授權。
B) The MIT license requires including the copyright and permission notice in all copies of the software.
- 正確。 MIT 授權的唯一條件是「在所有的副本或顯著的部分中,必須保留版權聲明與許可聲明」。因此每一次重新分發(包括二進位、原始碼或修改版)都須保留此文字。
C) The Apache license allows the software to be patented but prohibits using the licensor's trademark.
- 正確(依題目出題者常見的敘述方式)。
- 專利:Apache License 2.0 明確提供 Patent Grant,即每位貢獻者授予使用者一個永久、全球、免版稅的專利許可,以使用、販售、修改等目的。
第 II-1 題5 分
What problems would imbalanced training data cause in machine learning? How do you mitigate these problems? Give at least one problem and one solution.
登入後即可作答並保存紀錄。
核心觀念
類別不平衡是指訓練資料中不同類別的樣本數差距很大,例如正類只占 1%,負類占 99%。模型訓練時若以整體預測錯誤為主要目標,容易偏向樣本較多的多數類別,忽略少數類別。
常見影響包括:
- 少數類別辨識能力差:模型偏向預測多數類別,使少數類別的召回率(Recall)偏低。
- 準確率造成誤判:若正類占 1%,模型把所有樣本都預測為負類,準確率仍達 99%,但完全找不到正類。
- 評估結果失真:只看準確率,無法呈現模型對少數類別的實際表現。
解題方法
作答時先指出不平衡資料會使模型偏向多數類別,再說明一項可操作的改善方法,並交代它如何降低偏差。
例如使用類別加權(class weighting),提高少數類別錯誤在訓練損失中的比重。加權交叉熵可寫為:
第 II-2 題10 分
Explain the major differences between virtual machines (VMs) and containers. Give a scenario where VMs are preferable to containers, and a scenario where containers are preferable to VMs.
登入後即可作答並保存紀錄。
核心觀念
本題考查虛擬化的兩種方式:虛擬機器(VM)與容器(container)如何隔離應用程式,以及兩者在作業系統、資源使用與部署上的差異。
虛擬機器透過虛擬機器監視器(hypervisor)建立多個虛擬硬體環境。每個 VM 都能安裝自己的作業系統核心與系統程式,再執行應用程式。
容器則利用作業系統核心提供的隔離與資源控管機制,將應用程式及其相依套件封裝起來。多個容器通常共用同一個主機作業系統核心,因此不必為每個容器各自啟動一套完整作業系統。
解題方法
可從四個面向比較:隔離層級、作業系統核心、資源與啟動成本、適用情境。
| 比較面向 | 虛擬機器(VM) | 容器(container) |
|---|---|---|
| 隔離方式 | 由 hypervisor 提供虛擬硬體,隔離完整的客體作業系統 | 由作業系統核心提供程序、檔案系統及資源隔離 |
| 作業系統 | 每個 VM 可有自己的客體作業系統與核心 | 通常共用主機核心;容器內可封裝使用者空間程式與函式庫 |
| 資源成本 | 每個 VM 都需執行完整作業系統,記憶體與儲存空間開銷較大 | 不需為每個容器啟動完整作業系統,通常較輕量 |
| 啟動與密度 | 啟動較慢,同一主機可承載的實例通常較少 | 啟動較快,同一主機通常能執行較多實例 |
| 作業系統彈性 | 可在同一主機上執行不同客體作業系統 | 容器通常受主機核心相容性限制 |
第 II-3 題12 分
Give a short definition and at least one example for each of the following:
- Data as a Service (DaaS)
- Software as a Service (SaaS)
- Platform as a Service (PaaS)
- Infrastructure as a Service (IaaS)
登入後即可作答並保存紀錄。
核心觀念
本題考查雲端運算常見的服務模式。判斷重點是:服務供應商提供的是資料、可直接使用的軟體、開發執行平台,還是底層運算資源。
解題方法
依照使用者取得服務的層次,由上而下辨認:
- 取得資料:DaaS。
- 直接使用完整應用程式:SaaS。
- 在雲端平台上開發與部署程式:PaaS。
- 租用運算、儲存或網路等基礎資源:IaaS。
| 服務模式 | 定義 | 例子 |
|---|---|---|
| Data as a Service(DaaS,資料即服務) | 供應商透過網路提供資料或資料集,使用者可透過介面或 API 查詢、存取或取得資料,而不必自行維護資料來源與基礎設施。 | 地圖服務透過 API 提供地理位置、道路或路線資料。 |
| Software as a Service(SaaS,軟體即服務) | 供應商在雲端管理並提供完整應用程式,使用者通常透過瀏覽器或應用程式直接使用,無須自行安裝及維護伺服器端軟體。 |
Inspect the following multi-threaded Java program.
public class Main {
public static void main(String[] args) {
Lock lock = new Lock();
for (int i = 1; i <= 10; i++) {
final int tid = i;
Thread t = new Thread(new Runnable() {
public void run() {
lock.acquire();
System.out.println("Thread " + tid);
lock.release();
}
});
t.start();
}
}
}
class Lock {
Object lock = new Object();
int now = 0, next = 0;
void acquire() {
int id;
synchronized (this) {
id = next;
next++;
}
while (id != now) {}
}
void release() {
now++;
}
}
第 II-4-(1) 題5 分
Argue that no two threads can print to the terminal at the same time.
登入後即可作答並保存紀錄。
核心觀念
本題考的是以「票號」實作的互斥鎖。進入臨界區前,每個執行緒先取得唯一票號;只有票號等於目前號碼 now 的執行緒能離開等待迴圈,執行列印。列印結束後,該執行緒呼叫 release(),使 now 加一,輪到下一張票。
解題方法
先看票號如何分配,再看執行緒離開忙碌等待迴圈的條件。
在 acquire() 中,synchronized (this) 保護 next 的讀取與遞增,因此每次呼叫都會取得不同的 id。若取得票號的順序為 ,則各執行緒的票號互不重複。
每個執行緒都必須符合 id == now,才能離開 while 迴圈。初始時 now = 0,所以只有票號為 的執行緒能先進入列印區段。其他執行緒的票號都不等於 now,會繼續等待。
票號為 的執行緒列印完後才呼叫 release(),將 now 加一。此時票號為 的執行緒才能離開等待迴圈。往後依此類推。
互斥性證明
假設有兩個不同執行緒同時進入列印區段,它們必須各自持有票號 與 ,且都符合進入條件:
因此 。然而 acquire() 在同步區塊內分配票號,確保每個執行緒取得的票號都不同,故 ,產生矛盾。
因此,同一時間最多只有一個執行緒能通過等待條件並執行列印。執行緒列印完畢並呼叫 release() 後,now 才會前進,下一個執行緒才得以進入。
第 II-4-(2) 題5 分
Is the program still thread-safe after we change synchronized(this) to synchronized(lock)? Why?
登入後即可作答並保存紀錄。
核心觀念
本題考查 Java 的互斥同步與記憶體可見性。
synchronized(x) 會取得物件 x 的監視器(monitor)。多個執行緒若對同一個物件執行 synchronized(x),同一時間只會有一個執行緒進入同步區塊。離開同步區塊的動作,與之後另一執行緒取得同一監視器的動作之間,具有 happens-before 關係,可確保受保護的資料正確發布。
本題中,next 在 acquire() 的同步區塊內讀寫;now 則在忙碌等待迴圈讀取,並由 release() 寫入。
解題方法
把 synchronized(this) 改成 synchronized(lock) 後,lock 是 Lock 物件中的欄位。程式只建立一個 Lock 物件,所有執行緒都使用同一個 lock 欄位,因此每次執行 acquire() 時,仍會透過同一個監視器依序取得票號:
synchronized (lock) {
id = next;
next++;
}
所以,next 的讀寫仍受到互斥保護,每個執行緒仍會取得不同的 id。單看這項替換,票號分配仍正確。
然而,now 沒有受到同步保護:執行緒在 while (id != now) {} 中讀取 now,release() 則直接執行 now++。
第 II-4-(3) 題5 分
Is it true that every thread eventually prints a message? Why?
登入後即可作答並保存紀錄。
核心觀念
本題考的是 互斥鎖的公平性與活性,以及 Java 記憶體模型中的可見性。
- 互斥性是安全性:同一時間至多一個執行緒進入臨界區。
- 活性是進度性:等待中的執行緒最終能進入臨界區並完成工作。
synchronized不只用來限制同時進入,也會建立記憶體可見性;但只有透過同一個監視器的同步操作,才能建立相應的先行發生關係。
解題方法
先看 acquire() 如何分配號碼:
synchronized (this) {
id = next;
next++;
}
while (id != now) {}
每個執行緒在 synchronized (this) 中取得唯一的 id,號碼依序為 。之後它忙等,直到 id == now 才離開 acquire()。執行緒印出訊息後呼叫 release(),使 now 加一,輪到下一個號碼。
若 now 的更新能被等待中的執行緒看見,且執行緒都能持續獲得排程,這個號碼鎖會依序放行各執行緒,因此每個執行緒都會列印訊息。
但題目中的 now 是一般的 int,不是 volatile,讀取與寫入也沒有放在同一個同步區段中:
while (id != now) {} // 讀取 now,沒有同步
第 II-5 題4 分
Consider a table Customers that contains fields CustomerID and Country. Write a MySQL code to list the number of customers in each country, ordered by the country with the most customers first.
登入後即可作答並保存紀錄。
核心觀念
本題考查 SQL 的分組彙總。GROUP BY 會依指定欄位將資料分組,COUNT(*) 計算每組的資料列數,ORDER BY ... DESC 則依計算結果由大到小排序。
解題方法
從原卷可讀到,資料表為 Customers,欄位是 CustomerID 與 Country;題目要求列出各國的客戶數,並將客戶數最多的國家排在最前面。因此,依 Country 分組、計算每組筆數,再依計數結果遞減排序即可。
SELECT Country, COUNT(*) AS CustomerCount
FROM Customers
GROUP BY Country
ORDER BY CustomerCount DESC;
第 II-6 題4 分
Suppose that you have two tables, Customers and Orders. Each entry of Customers contains two fields: CustomerID and CustomerName. Each entry of Orders contains two fields: CustomerID and an OrderID. A customer can be related to several orders through CustomerID. Write a MySQL code to list the customer names and the number of orders associated with each customer.
登入後即可作答並保存紀錄。
核心觀念
本題考查資料表連接、分組彙總與計數:
- 以
CustomerID將Customers和Orders連接,找出每位顧客的訂單。 - 使用
COUNT計算每位顧客的訂單數。 - 使用
LEFT JOIN保留沒有任何訂單的顧客,使其訂單數顯示為 0。
解題方法
原頁第 II-6 題提供 Customers(CustomerID, CustomerName) 與 Orders(CustomerID, OrderID) 兩個資料表,並指出顧客與訂單透過 CustomerID 關聯。題目右側的範例欄位為 Name 和 Count,範例資料包含 Alice、Bob、Carl;這些範例用來展示輸出格式,不是查詢時要寫死的資料。
先用 LEFT JOIN 依 CustomerID 配對,再以顧客的識別碼和姓名分組。計數使用 COUNT(o.CustomerID):有配對訂單時,這個欄位會被計入;沒有訂單時,左連接產生的空值列不會被計入,因此結果為 0。
第 II-7 題10 分
Pascal’s triangle is defined as below. The row number starts from 1 (e.g., Row 1 is [1], Row 2 is [1 1], Row 3 is [1 2 1], etc.). Write a function that takes a row number and returns Row of the triangle. Your program should run in time and space.
Examples:
- Input: 1; Output: [1]
- Input: 3; Output: [1 2 1]
- Input: 4; Output: [1 3 3 1]
🖼️【此處有附圖,請對照原卷】
登入後即可作答並保存紀錄。
核心觀念
Pascal 三角形的每一列,首尾都是 ;中間的數值等於上一列相鄰兩數的和。若列數從 開始,則第 列的位置 (從 開始)滿足:
其中 。這也對應二項式係數的遞推關係。
解題方法
原卷圖中的列數從 開始,例子列出第 列為 、第 列為 、第 列為 ;旁邊的三角形圖示也呈現相鄰兩數相加的規則。
從第一列開始逐列建立答案。每次先在目前列尾端加入 ,再由右向左更新中間元素。由右向左更新可保留尚未更新的上一列數值,避免新值覆蓋遞推所需的舊值。