109 年 國立臺灣大學資訊管理學系碩士班《資訊科技概論》
第 1 題
In object-oriented programming, the concept of hiding internal state and requiring all interaction to be performed through an object's methods is known as
(A) Abstraction
(B) Encapsulation
(C) Inheritance
(D) Polymorphism
(E) Enveloping
登入後即可作答並保存紀錄。
本題考查物件導向程式設計 (Object-Oriented Programming, OOP) 的基本觀念。題目描述的是將物件的內部狀態隱藏起來,並且所有互動都必須透過物件的方法來進行。這是物件導向程式設計的核心原則之一,旨在保護資料的完整性並提高程式碼的可維護性。
- (A) Abstraction (抽象化):指將複雜的系統簡化,只呈現使用者需要的部分,隱藏不必要的細節。
- (B) Encapsulation (封裝):指將資料 (屬性) 和操作資料的方法 (行為) 包裝在一個類別 (class) 中,並控制對資料的存取權限,使得外部無法直接存取或修改內部資料,只能透過公開的方法來互動。這與題目描述的「隱藏內部狀態」和「透過方法互動」完全吻合。
第 2 題
Which of the following access modifiers in object-oriented programming specifies that only the current class and subclasses (and sometimes also same-package classes) of this class will have access to the field or method been defined?
(A) internal
(B) private
(C) protected
(D) class public
(E) none of the above.
登入後即可作答並保存紀錄。
本題考查物件導向程式設計中存取修飾符 (access modifiers) 的概念。題目詢問哪種修飾符允許「僅限於目前類別、子類別,以及同一個套件 (package) 中的類別」存取成員。
- (A)
internal:在某些語言 (如 C#) 中,internal存取修飾符表示成員只能在同一個組件 (assembly) 內被存取。在 Java 中,沒有internal關鍵字,但預設的 (沒有修飾符) 存取權限 (package-private) 類似於此,表示只能在同一個套件內存取。 - (B)
private:成員只能在定義它的類別內部被存取。 - (C)
protected:成員可以在定義它的類別內部、該類別的子類別中,以及同一個套件中的類別中被存取。這與題目描述的「current class and subclasses (and sometimes also same-package classes)」非常接近。 - (D)
class public:這個選項的組合 (class public) 並非標準的存取修飾符。
第 3 題
Which of the following about Python programming language is wrong?
(A) it supports functional programming style coding
(B) it supports object-oriented programming
(C) it supports procedural style coding
(D) it supports imperative programming style coding
(E) all of the above are correct (choose this one only if none of the above can be chosen).
登入後即可作答並保存紀錄。
核心觀念
本題測驗考生對 Python 程式語言所支援的程式範式(programming paradigm)之認識。程式範式的定義如下:
- 函數式編程(Functional Programming):以純函式、不可變資料為主,支持高階函式與匿名函式 (lambda) 等特性。
- 物件導向編程(Object‑Oriented Programming, OOP):以類別與物件封裝資料與行為,支援繼承、多型與封裝。
- 程序式編程(Procedural Programming):以順序執行的子程式(procedure)或函式為主,強調控制流程。
- 指令式編程(Imperative Programming):直接描述「如何」執行的步驟,包含變數賦值、迴圈、條件分支等,程序式與指令式在實務上常被交叉使用。
了解 Python 同時具備上述哪些特性,即可判斷選項之正誤。
解題方法
- 先確認 Python 官方文件或教材對各程式範式的支援情形。
- 逐一比對四個選項的描述,檢視是否有任何已知的「不支援」之項目。
- 若全部皆符合 Python 的特性,則依題目最後的說明,選擇 (E) all of the above are correct;否則選出唯一錯誤之選項。
選項分析
| 選項 | 內容 | 正確性說明 |
|---|---|---|
| (A) | “it supports functional programming style coding” | Python 支援函數式編程:提供 lambda、map、filter、reduce、列表生成式、生成器、不可變資料類型 (tuple、frozenset) 以及函式作為第一等公民。雖非純函數式語言,但其語法與標準函式庫足以實作函數式程式碼。正確。 |
| (B) | “it supports object‑oriented programming” | Python 完全支援物件導向:所有資料皆為物件,語法提供 class、__init__、繼承、抽象基底類別、多型、封裝等。正確。 |
第 4 題
Which of the following about Python programming language is wrong?
(A) it is a general-purpose programming language
(B) it is statically-typed
(C) it procedural can be executed by an interpreter
(D) it uses whitespace indentation to delimit blocks
(E) all of the above are correct (choose this one only if none of the above can be chosen).
登入後即可作答並保存紀錄。
本題考查 Python 程式語言的特性,要求找出錯誤的敘述。
- (A) general-purpose programming language:Python 是一種廣泛應用於各種領域(網頁開發、數據科學、自動化、AI 等)的通用程式語言。此敘述正確。
- (B) statically-typed:Python 是一種動態型別 (dynamically-typed) 語言,而不是靜態型別 (statically-typed) 語言。在動態型別語言中,變數的型別是在執行時期確定的,而不是在編譯時期。例如,在 Python 中,你可以寫
x = 10,然後x = "hello",而不會有編譯錯誤。在靜態型別語言(如 Java, C++)中,必須在編譯時期確定變數的型別,且型別一旦確定就不能改變。因此,此敘述是錯誤的。 - (C) it procedural can be executed by an interpreter:Python 程式通常由直譯器 (interpreter) 執行,這與程序式程式設計風格無關,而是指 Python 的執行方式。
第 5 題
Which of the following best describes a software delivery model that is deployed and managed on a vendor's cloud computing infrastructure and accessed by users over the Internet as and when required?
(A) On-demand software
(B) pay-as- you-go
(C) open source
(D) mobile software
(E) code-on-demand
登入後即可作答並保存紀錄。
本題考查軟體交付模式。題目描述的模式是:軟體部署並管理在供應商的雲端基礎設施上,使用者可以透過網際網路隨時隨地存取。
- (A) On-demand software (隨選軟體):這是指軟體可以根據使用者的需求即時提供和使用,通常與雲端服務模型相關。這與題目描述的「accessed by users over the Internet as and when required」非常吻合。
- (B) pay-as-you-go (隨用隨付):這是一種計費模式,使用者根據實際使用的資源量付費。雖然常見於雲端服務,但它描述的是計費方式,而不是軟體交付模型本身。
- (C) open source (開源軟體):這是一種軟體授權模式,其原始碼是公開的,任何人都可以檢視、修改和分發。這與軟體部署地點和存取方式無關。
- (D) mobile software (行動軟體):這類軟體是為行動裝置(如智慧型手機、平板電腦)設計的。
第 6 題
Google App Engine is best characterized as an example of
(A) Infrastructure-as-a-service
(B) Software-as-a-service
(C) App-as-a-service
(D) Platform-as-a-service
(E) Network-as-a-service
登入後即可作答並保存紀錄。
本題考查雲端運算服務模型。Google App Engine (GAE) 是一個讓開發者能夠建立和部署應用程式的平台,它提供了一個執行環境,開發者無需管理底層的伺服器、作業系統或網路。
- (A) Infrastructure-as-a-service (IaaS):提供基本的運算資源,如伺服器、儲存和網路。使用者需要自行管理作業系統、應用程式和資料。例如 Amazon EC2, Google Compute Engine。
- (B) Software-as-a-service (SaaS):提供完整的應用程式,使用者直接透過瀏覽器或客戶端存取。例如 Gmail, Salesforce。
- (C) App-as-a-service (AaaS):這不是一個標準的雲端服務模型術語,但如果存在,可能類似於 SaaS。
第 7 題
Which of the following is generally not characterized as a mobile payment service?
(A) LINE Pay
(B) Pi錢包
(C) 街口
(D) 台灣Pay
(E) 噴噴
登入後即可作答並保存紀錄。
本題考查台灣行動支付市場之常見主流服務品牌及其服務屬性,要求辨識何者不屬於行動支付服務。
一、各選項逐一檢驗:
- (A) LINE Pay:由連加網路商業股份有限公司(LINE Pay Taiwan)營運,為台灣市占率極高的第三方行動支付工具,使用者可綁定信用卡、簽帳金融卡或儲值帳戶於特約商店進行條碼掃描付款。
- (B) Pi拍錢包(Pi錢包):由網路家庭(PChome Online)旗下拍付國際資訊股份有限公司推出的行動錢包 App,支援信用卡綁定與點數折抵,廣泛應用於超商通路、停車費繳納與合作實體通路。
- (C) 街口支付(街口):由街口電子支付股份有限公司營運,為台灣代表性的專營電子支付機構,支援掃碼支付、轉帳、儲值、繳費及跨境掃碼支付等功能。
第 8 題
Which of the following best describes 5G's speed?
(A) 10-100Mbps
(B) 100-500Mbps
(C) 300Mbps-1Gbps
(D) 1-10Gbps
(E) 10-100Gbps
登入後即可作答並保存紀錄。
核心觀念
本題考查行動通訊世代的傳輸速度。5G 的主要特色之一,是大幅提升資料傳輸速率、降低延遲,並支援大量裝置同時連線。
以考試教材常見的概略比較而言:
- 4G:通常約數十 Mbps 至數百 Mbps
- 5G:通常以 Gbps 等級為主要描述
- 5G 的理想峰值速率可達約 10 Gbps 以上,實際環境則會受到基地台距離、頻段、使用者數量與網路負載影響
其中:
因此,– 已經明確屬於 5G 的高速傳輸範圍。
解題方法
判斷本題的關鍵,在於辨認各選項的速率單位與數值範圍。
5G 的速度應以 Gbps 等級描述。將選項換算成相同單位:
- ––
- ––
- ––
- ––
- ––
在一般資訊科技概論教材的世代比較中,5G 通常被描述為約 –,因此選擇(D)。
選項分析
(A)10–100 Mbps:錯誤。
此範圍主要接近早期行動網路或一般 4G 的傳輸速度,未能呈現 5G 的 Gbps 等級高速特性。
(B)100–500 Mbps:錯誤。
此範圍雖比傳統 4G 快,但仍屬於數百 Mbps 等級,通常不能作為 5G 代表性的速度描述。
(C)300Mbps–1Gbps:錯誤。
第 9 題
In computer networks, which of the following refers to a server that acts as an intermediary for requests from clients seeking resources (such as a file or web page) from servers that provide those resources.
(A) cache
(B) proxy
(C) load balancing
(D) firewall
(E) none of the above.
登入後即可作答並保存紀錄。
本題考查電腦網路中的伺服器角色。題目描述的是一個充當中介的伺服器,負責處理用戶端對資源(如檔案或網頁)的請求,並從提供資源的伺服器獲取這些資源。
- (A) cache (快取):快取是用來儲存常用資料的臨時記憶體,以加快存取速度。雖然快取伺服器(如快取代理伺服器)可以作為中介,但「快取」本身更側重於資料儲存和加速,而不是作為請求的中介。
- (B) proxy (代理伺服器):代理伺服器(Proxy Server)就是專門設計來作為用戶端和目標伺服器之間的中介。它接收用戶端的請求,然後代表用戶端向目標伺服器發出請求,並將目標伺服器的回應傳回給用戶端。這完全符合題目描述。
第 10 題
In computer networks, which of the following servers is responsible for resolving a name like www.im.ntu.edu.tw to its IP address?
(A) URL server
(B) web server
(C) HTTP servers
(D) exchange server
(E) none of the above.
登入後即可作答並保存紀錄。
核心觀念
在網路中,將人類易讀的主機名稱(如 www.im.ntu.edu.tw)轉換成機器可識別的 IP 位址的工作稱為 名稱解析(Name Resolution)。負責此任務的服務是 Domain Name System(DNS)伺服器,其運作流程大致為:
- 客戶端向本地 DNS 快取或 DNS 解析器(Resolver)發送查詢請求。
- 若本地快取無法解答,解析器會向根 DNS 伺服器、頂層網域(Top‑Level Domain, TLD)伺服器與授權名稱伺服器逐層請求,最終得到對應的 IP 位址。
- 客戶端取得 IP 後,再以 TCP/UDP 方式與目標主機通訊(例如 HTTP、HTTPS)。
此概念常會與「URL」或「Web Server」混淆,應明確區分:
- URL(Uniform Resource Locator):僅是資源定位字串的語法,內含協定、主機名稱、路徑等資訊,本身不是伺服器。
- Web Server:提供 HTTP 資源的伺服器,接收已解析好的 IP 訪問請求。
- HTTP Server:廣義上指任何支援 HTTP 協定的伺服器,亦非名稱解析者。
- Exchange Server:微軟的郵件與協作平台,與 DNS 無直接關聯。
解題方法
本題屬於概念辨識型選擇題。解題步驟:
- 確認題目詢問「將名稱
www.im.ntu.edu.tw解析為 IP 位址」的職能。 - 於選項中尋找與 名稱解析(Name Resolution) 相關的伺服器名稱。
- 若無符合 DNS 的選項,則選擇「none of the above」作為正確答案。
選項分析
| 選項 | 內容說明 | 是否為負責名稱解析的伺服器 | 判斷理由 |
|---|---|---|---|
| (A) URL server | 「URL」是資源定位字串的格式,並不存在「URL server」此類服務。 | ❌ | 本選項為虛構,無任何實際功能。 |
| (B) web server | 提供 HTTP/HTTPS 網頁內容的伺服器(如 Apache、IIS)。 | ❌ | 僅在取得 IP 後接受連線,名稱解析由 DNS 完成。 |
第 11 題
Which of the following is wrong?
(A) IPv4 uses a 32-bit address space
(B) IPv6 uses a 120-bit address space
(C) IPv6 is intended to replace IPv4
(D) IPv6 is the most recent version of the Internet Protocol (IP)
(E) all of the above are correct (choose this one only if none of the above can be chosen).
登入後即可作答並保存紀錄。
本題考查網際網路協定 (IP) 的版本特性,要求找出錯誤的敘述。
- (A) IPv4 uses a 32-bit address space:IPv4 位址長度為 32 位元,可以提供約 (約 43 億) 個位址。此敘述正確。
- (B) IPv6 uses a 120-bit address space:IPv6 位址長度為 128 位元,而不是 120 位元。128 位元可以提供約 個位址,數量極為龐大。因此,此敘述是錯誤的。
- (C) IPv6 is intended to replace IPv4:由於 IPv4 位址枯竭的問題,IPv6 被設計為下一代 IP 協定,旨在取代 IPv4,提供更龐大的位址空間和更多功能。此敘述正確。
第 12 題
Which of the following network technology is most widely used in local area networks?
(A) Ethernet
(B) Token Ring
(C) FDDI
(D) ARCNET
(E) none of the above.
登入後即可作答並保存紀錄。
本題考查區域網路 (Local Area Network, LAN) 中常用的網路技術。
- (A) Ethernet (乙太網路):乙太網路是目前最普及、最廣泛使用的區域網路技術。它支援多種傳輸速率(從 10 Mbps 到 100 Gbps 以上)和多種傳輸介質(如雙絞線、光纖)。
- (B) Token Ring (令牌環網):令牌環網是一種較早期的區域網路技術,由 IBM 開發。它曾經流行過,但現在已被乙太網路取代,很少使用。
- (C) FDDI (Fiber Distributed Data Interface):FDDI 是一種基於光纖的區域網路技術,提供 100 Mbps 的傳輸速率。
第 13 題
A ____ bit is a check bit that is added to a block of data to ensure that the total number of 1-bits in the string is even or odd.
(A) spin
(B) recovery
(C) parity
(D) detection
(E) singularity
登入後即可作答並保存紀錄。
本題考查資料傳輸中的錯誤偵測機制。題目描述的是一個檢查位元 (check bit),它被添加到資料區塊中,用來驗證資料區塊中 1 的總數是偶數還是奇數。
- (A) spin:與資料傳輸或錯誤偵測無關。
- (B) recovery:指從錯誤中恢復的過程,但不是指檢查位元本身。
- (C) parity:同位元 (parity bit) 是一種簡單的錯誤偵測方法。它被添加到資料中,使得整個資料(包括同位元)中 1 的總數成為偶數(偶同位元,even parity)或奇數(奇同位元,odd parity)。
第 14 題
Bluetooth is popularly used in the following applications as a wireless communication technology except
(A) between a mobile phone and a VR headset
(B) between a mobile phone and a car stereo system
(C) between a wearable device and a mobile phone
(D) between a mouse and a laptop
(E) all of the above are common applications of Bluetooth.
登入後即可作答並保存紀錄。
本題考查藍牙(Bluetooth)無線通訊技術之常見應用場景。題目要求選出「非」藍牙普遍應用的選項(即除了哪一項以外)。
一、藍牙技術特性與應用範疇
藍牙是一種短距離、低功耗的無線射頻通訊技術(通常工作於 2.4 GHz ISM 頻段),專門用於建立個人區域網路(WPAN),其實體層有效頻寬約在 1~2 Mbps(傳統藍牙及 BLE 傳輸率)。其典型應用包括低延遲控制指令傳遞、感測資料同步、語音通話以及立體聲音訊串流傳輸。
二、各選項逐一檢驗:
- (A) between a mobile phone and a VR headset(手機與 VR 頭戴裝置之間):
在行動 VR 或獨立式 VR 應用中,手機與 VR 頭顯之間普遍使用藍牙進行裝置配對、控制訊號回傳、體感手柄/控制器軌跡追蹤資料傳輸及音訊傳輸。雖然 VR 高畫質即時影像串流因頻寬需求極高(通常需數十至數百 Mbps 以上)必須依賴 Wi-Fi 6 或高速有線連接,但藍牙在周邊控制、配對連線與音訊交互上確實屬於普遍標準配備。 - (B) between a mobile phone and a car stereo system(手機與汽車音響系統之間):
這是藍牙最成熟且廣泛的應用之一(利用 A2DP 與 HFP 協定),用於車載免持聽筒通話及手機音樂無線播放。 - (C) between a wearable device and a mobile phone(穿戴裝置與手機之間):
第 15 題
Which if the following is not a non-volatile memory/storage?
(A) Solid-state drive (SSD)
(B) DDR4
(C) read-only memory (ROM)
(D) Flash memory
(E) all of the above are non-volatile.
登入後即可作答並保存紀錄。
本題考查電腦記憶體和儲存裝置的揮發性 (volatility)。非揮發性記憶體 (non-volatile memory) 在斷電後仍能保存資料,而揮發性記憶體 (volatile memory) 在斷電後資料會消失。
- (A) Solid-state drive (SSD):SSD 使用快閃記憶體 (Flash memory) 來儲存資料,是非揮發性儲存裝置。
- (B) DDR4:DDR4 (Double Data Rate 4) 是目前主流的 DRAM (Dynamic Random-Access Memory) 技術。DRAM 是揮發性記憶體,斷電後資料會消失。
第 16 題
Which of the following standards is a specification for an information security management system (ISMS) intended to provide a framework of policies and procedures that includes all legal, physical and technical controls involved in an organization's information risk management processes.
(A) ISO 9001
(B) ISO 7700
(C) ISO 14000
(D) ISO 27001
(E) none of the above.
登入後即可作答並保存紀錄。
本題考查資訊安全管理系統 (Information Security Management System, ISMS) 的相關標準。題目描述了一個用於管理組織資訊風險的政策和程序框架。
- (A) ISO 9001:這是品質管理系統 (Quality Management System) 的標準,與資訊安全無直接關聯。
- (B) ISO 7700:這個編號的 ISO 標準似乎不存在,或者不是一個廣為人知的資訊安全標準。
- (C) ISO 14000:這是環境管理系統 (Environmental Management System) 的標準。
第 17 題
In the CIA triad, a model designed to guide policies for information security within an organization, the letters 'CIA' refers to
(A) Confidentiality, Integrity and Availability
(B) Content, Information and Access
(C) Central Intelligence Agency
(D) Centralized, Independent, and Assurance
(E) none of the above.
登入後即可作答並保存紀錄。
本題考查資訊安全領域中的 CIA 三要素。CIA 三要素是資訊安全的核心原則,用於指導組織制定資訊安全政策。
- C stands for Confidentiality (機密性):確保資訊僅供授權人員存取。
- I stands for Integrity (完整性):確保資訊的準確性和完整性,防止未經授權的修改。
- A stands for Availability (可用性):確保授權使用者在需要時能夠存取資訊和相關資源。
選項分析:
- (A) Confidentiality, Integrity and Availability:這正是 CIA 三要素的標準解釋。
- (B) Content, Information and Access:這不是 CIA 三要素的標準解釋。
第 18 題
Which of the following SQL Commands creates a table "Customers" with two columns "CustomerID" and "CustomerName":
(A) CREATE TABLE Customers (CustomerID int, CustomerName varchar(255) );
(B) CREATE DATABASE Customers (CustomerID int, CustomerName varchar(255));
(C) BUILD TABLE Customers (CustomerID int, CustomerName varchar(255));
(D) BUILD DATABASE Customers (CustomerID int, CustomerName varchar(255));
(E) none of the above.
登入後即可作答並保存紀錄。
本題考查 SQL (Structured Query Language) 的語法,用於建立資料庫表格。
-
(A) CREATE TABLE Customers (CustomerID int, CustomerName varchar(255) );
CREATE TABLE是用於建立新表格的標準 SQL 命令。Customers是表格的名稱。(CustomerID int, CustomerName varchar(255))定義了表格的欄位(列),包括欄位名稱和資料型別。int是整數型別,varchar(255)是可變長度的字串型別,最多 255 個字元。- 這個語法是正確的,用於建立一個名為
Customers的表格,包含CustomerID和CustomerName兩個欄位。
-
(B) CREATE DATABASE Customers (CustomerID int, CustomerName varchar(255));
CREATE DATABASE用於建立一個新的資料庫,而不是表格。
第 19 題
Which of the following is a community-developed relational database management system intended to maintain high compatibility with MySQL and remain free and open-source due to the concerns over Oracle Corporation's acquisition of MySQL in 2009?
(A) MonetDB
(B) SAP R3
(C) Sybase
(D) MariaDB
(E) Firebird
登入後即可作答並保存紀錄。
本題考查關聯式資料庫管理系統 (RDBMS)。題目描述了一個由社群開發、與 MySQL 高度相容、且免費開源的資料庫系統,其出現是為了應對 Oracle 收購 MySQL 後的疑慮。
- (A) MonetDB:MonetDB 是一個開源的列式資料庫管理系統,主要用於數據倉儲和分析,與 MySQL 的架構和相容性不同。
- (B) SAP R3:SAP R3 是一個企業資源規劃 (ERP) 系統,其底層資料庫可以是多種,但 SAP R3 本身不是一個獨立的 RDBMS,且通常是商業軟體。
- (C) Sybase:Sybase (現為 SAP ASE) 是一個商業的關聯式資料庫管理系統,與 MySQL 的開源社群和起源不同。
第 20 題
In the age of Internet of Things (IoT), the number of connected devices has grown rapidly in the past 10 years. Which of the following best describes the estimated number of connected devices worldwide in 2020?
(A) 10-100 million
(B) 100-500 million
(C) 1-5 billion
(D) 10-50 billion
(E) 100-500 billion
登入後即可作答並保存紀錄。
核心觀念
本題考查資訊科技發展史中對於「物聯網 (Internet of Things, IoT)」規模的趨勢預測。物聯網是指透過網際網路連結實體裝置、感測器與軟體,使其具備資料交換能力的技術。在 2010 年代中後期,隨著 5G 技術佈局與低功耗廣域網路 (LPWAN) 的發展,國際研究機構對於 2020 年全球聯網裝置數量的預測,是檢視該領域成長動能的關鍵指標。
解題方法
要準確判斷本題答案,需參考當時主流市場研究機構(如 Gartner、IDC、Statista)針對 2020 年 IoT 裝置數量的預估報告。這些數據的統計範疇包含智慧家庭裝置、智慧穿戴、工業物聯網 (IIoT)、車聯網及智慧城市基礎建設。
根據多家機構於 2015 至 2018 年間的推估,2020 年全球聯網裝置數量預期將突破 200 億大關,平均分佈在 200 億至 300 億之間。因此,在數量級 (Order of Magnitude) 的分類上,應落在 單位,即 100 億至 500 億的區間。
選項分析
- (A) 10-100 million:此數量級僅在物聯網發展初期(約 2000 年代初期)可能達到,對於 2020 年而言數值過低,甚至未及當代手機用戶數。
- (B) 100-500 million:此區間僅反映了數億台級別的連網規模,遠低於當時僅智慧型手機市場的活躍裝置數量。
- (C) 1-5 billion:此選項在 2010 年前後或許合理,但低估了物聯網在 2020 年的廣度。全球人口已達 70 億,若僅 10-50 億裝置,代表每人連網裝置低於 1 台,與當時物聯網裝置呈現「每人擁有多個聯網節點」的趨勢不符。
第 21 題
An ADT LIST contains items of the same type. The following C++ code fragment shows the class definition of an array-based implementation of ADT LIST that contains a list of integers.
class List
{
public:
// value: the integer to remove
// success: whether the removal is successful or not
void remove (int value, bool & success);
private:
// double the size of the array pointed by p_items
void doubleArraySize();
// pointing to an integer array that stores integers
// initialized as p_items = new int[arraySize);
int *p_items;
// size of item array
int arraySize; // default 20
// number of list items
int itemCount; // default 0
};
(a) (15 points) Write the List::remove(int value, bool& success) function that removes value from the list. Note that the integers in the array are sorted in ascending order and the list has NO duplicate items.
(b) (15 points) If the array pointed by p_items is full, the list must resize it in order to add integers to the list. Write the function List::doubleArraySize() that doubles the size of the integer array.
登入後即可作答並保存紀錄。
核心觀念
- ADT LIST:抽象資料型別 List,提供插入、刪除、搜尋等操作。實作時常以陣列或鏈結串列為基礎。
- 有序序列與無重複:題目保證陣列內的整數已依 升序 排列且 不會有重複,因此可利用二元搜尋定位目標值,或直接線性掃描即可。
- 陣列縮減:刪除元素後需將後方元素左移填補空位,維持連續性。
- 動態擴充:當陣列已滿時(
itemCount == arraySize),必須配置更大的儲存空間,舊資料拷貝過去,再釋放舊指標。
解題方法 – List::remove
- 搜尋目標
- 由於陣列已排序且無重複,可使用 二元搜尋()找出
value的索引pos。若未找到則success = false,直接返回。
- 由於陣列已排序且無重複,可使用 二元搜尋()找出
- 左移元素
- 若找到,將
pos之後的所有元素左移一格,覆寫被刪除的位置。左移操作為線性掃描,時間複雜度 ,最壞情況 。
- 若找到,將
- 更新計數
itemCount--,並將success設為true。
程式實作
void List::remove(int value, bool &success)
{
// 1. 二元搜尋定位 value
int left = 0, right = itemCount - 1;
int pos = -1; // 若未找到保留 -1
while (left <= right) {
int mid = left + (right - left) / 2;
if (p_items[mid] == value) { // 找到目標
pos = mid;
break;
} else if (p_items[mid] < value) {
left = mid + 1;
} else {
right = mid - 1;
}
}
if (pos == -1) { // 沒有此值
success = false;
return;
}
// 2. 左移以刪除元素
for (int i = pos; i < itemCount - 1; ++i) {
p_items[i] = p_items[i + 1];
}
// 3. 更新計數與成功旗標
--itemCount;
success = true;
}
時間與空間分析
- 時間:二元搜尋 + 左移 → 總體 (最壞情況)。
- 空間:僅使用常數額外變數,。
解題方法 – List::doubleArraySize
- 配置新陣列:新大小為
arraySize * 2。使用new int[newSize]取得指標p_new. - 拷貝舊元素:將
itemCount個有效元素從p_items複製到p_new(for迴圈或std::copy)。 - 釋放舊陣列:
delete[] p_items; - 更新指標與大小:
p_items = p_new;、arraySize = newSize;
程式實作
void List::doubleArraySize()
{
// 1. 計算新大小
int newSize = arraySize * 2; // 若 arraySize 為 0,仍會變為 0,題目假設預設 20,故安全
// 2. 配置新陣列
int *p_new = new int[newSize];
// 3. 複製舊元素
for (int i = 0; i < itemCount; ++i) {
p_new[i] = p_items[i];
}
// 4. 釋放舊陣列
delete[] p_items;
// 5. 更新成員
p_items = p_new;
arraySize = newSize;
}
時間與空間分析
- 時間:拷貝
itemCount個元素 → ()。 - 空間:額外配置一塊大小為
2 * arraySize的陣列 → 暫存空間,完成後釋放舊陣列。
解題技巧與常見陷阱
- 二元搜尋 vs. 線性掃描:若
itemCount較小(如 ≤ 20),線性掃描的常數因子更低,實務上可直接遍歷;但在參考書等級的解說中,必須展示最適化的二元搜尋,以凸顯「利用已排序」的概念。 - 左移時的迴圈上界:
i < itemCount - 1為正確上界,避免讀取未初始化的p_items[itemCount]。 - 擴充時的例外安全:
new失敗會拋出std::bad_alloc,若需 robust implementation,可先檢查newSize是否合理,或使用try / catch。在考試情境下,僅需示範基本流程即可。
第 22 題
HEAP is a complete binary tree that provides an efficient implementation of priority queues. Suppose we use a heap to manage a list of positive integers that large values mean high priority. The following C++ code fragment shows the class definition of the heap.
class HEAP
{
public:
// boost items[i]'s priority
void boostPriority (int i,int newPriorityValue);
private:
int itemCount; // number of heap items
int items [100]; // array of heap items
};
(a) (20 points) Complete the following C++ function that boosts the priority value of element HEAP::items[i].
// assume 0 <= i < itemCount,
// and newPriorityValue > items[i]
void Heap:: boostPriority (int i, int newPriorityValue)
{
}
登入後即可作答並保存紀錄。
核心觀念
-
Max-Heap(最大堆積)性質:
- 題目說明「數值愈大代表優先權愈高(large values mean high priority)」,故此處實作的是 Max-Heap。
- 結構性質:Heap 是一棵完全二元樹(Complete Binary Tree)。
- 堆積順序性質(Heap-Order Property):除了根節點外,任意節點的值皆小於或等於其父節點的值()。
-
0-indexed 陣列在完全二元樹中的父子節點索引關係:
- 題目中前置條件給定
0 <= i < itemCount,代表陣列索引從0開始(0-indexed)。 - 對於任意節點索引 :
- 其父節點(Parent)索引為:
- 其左子節點(Left Child)索引為 。
- 其右子節點(Right Child)索引為 。
- 題目中前置條件給定
-
提高優先權(Increase-Key / Boost Priority)與 Percolate Up(向上過濾):
- 當節點 的值調高(
newPriorityValue > items[i])時,該節點的值可能違反 Max-Heap 特質,亦即可能會「大於」其父節點。 - 為恢復 Max-Heap 特質,需將該元素沿著往根節點的路徑向上移動(Percolate Up / Sift Up / Bubble Up),直到其父節點的值大於或等於該元素,或者該元素已到達根節點(索引
0)為止。
- 當節點 的值調高(
解題方法
-
更新目標節點的數值:
直接將新值賦予目標位置:items[i] = newPriorityValue;。 -
實作向上調整(Percolate Up / Sift Up):
- 使用迴圈持續比較當前節點 與其父節點 的大小。
- 終止條件:
- 當前節點已是根節點(即 )。
- 父節點的值已經大於或等於當前節點的值(即 )。
- 調整動作:若當前節點大於父節點,則交換 與 ,並將指針移動至父節點位置(),繼續往上檢查。
關鍵程式碼實作
void HEAP::boostPriority(int i, int newPriorityValue)
{
// 1. 更新節點的優先權值
items[i] = newPriorityValue;
// 2. Percolate Up(向上調整以維持 Max-Heap 性質)
while (i > 0)
{
int parent = (i - 1) / 2;
if (items[i] > items[parent])
{
// 交換當前節點與父節點
int temp = items[i];
items[i] = items[parent];
items[parent] = temp;
// 將當前檢查索引移至父節點
i = parent;
}
else
{
// 已滿足 Max-Heap 性質,提前結束
break;
}
}
}
進階優化寫法(減少交換次數的賦值法):
也可以先暫存newPriorityValue,在上移過程中僅將較小的父節點往下移,最後再將newPriorityValue放入最終正確的空位,寫法同樣精確且減少 assignment 次數:void HEAP::boostPriority(int i, int newPriorityValue) { while (i > 0 && items[(i - 1) / 2] < newPriorityValue) { items[i] = items[(i - 1) / 2]; i = (i - 1) / 2; } items[i] = newPriorityValue;