109 年 國立中正大學資訊工程學系碩士班乙組《計算機概論》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 1 題12 分

Assume that the following program is executed on a little-endian machine. Write down the output of the following C program.

#include <stdio.h>
#define LOC(row, col) row*3+col

int main(void)
{
    int A[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    int *ptr1;
    char *ptr2;

    ptr1 = (int*) A;
    printf("%d\n", *ptr1);
    printf("%d\n", *(ptr1+LOC(1,1)));
    printf("%d\n", ptr1[LOC(2,1)]);
    printf("%d\n", ptr1[5]/5);

    ptr2 = (char*) A;
    printf("%d\n", ptr2[0]);
    printf("%d\n", ptr2[1]);

    return 0;
}

登入後即可作答並保存紀錄。

這一題的完整詳解

本題測驗 C 語言的巨集展開、二維陣列在記憶體中的列優先(row-major)配置、指標算術,以及 little-endian 架構下的位元組序。

二維陣列 int A[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; 在記憶體中依列優先順序連續存放,依序為 1, 2, 3, 4, 5, 6, 7, 8, 9。

  1. ptr1 = (int*) A;
    將 A 的起始位址轉型為 int *,使 ptr1 指向 A[0][0](即連續記憶體中的第 0 個整數)。

  2. printf("%d\n", *ptr1);
    *ptr1 即 ptr1[0],對應 A[0][0],輸出 1。

  3. printf("%d\n", *(ptr1+LOC(1,1)));
    巨集 LOC(1,1) 展開為 1*3+1。運算式 ptr1 + 1*3 + 1 因乘法優先權高於加法,等同於 ptr1 + 3 + 1,即 ptr1 + 4。*(ptr1 + 4) 對應 A[1][1],輸出 5。

  4. printf("%d\n", ptr1[LOC(2,1)]);
    巨集 LOC(2,1) 展開為 2*3+1 = 7。ptr1[7] 對應 A[2][1],輸出 8。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題8 分

#include <stdio.h>
void func(int **a, int **b)
{
int *temp;
temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int **x1, y1, z1, **x2, y2, z2;
z1 = 10, z2 = 20;
x1 = &y1;
y1 = &z1;
x2 = &y2;
y2 = &z2;
printf("%d %d %d %d\n", *y1, *y2, **x1, **x2);
func(x1,x2);
printf("%d %d %d %d\n", *y1, y2, **x1, **x2);
return 0;
}
Write down the output of the following C program.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考察 C 語言指標與雙重指標,以及函式透過指標修改呼叫端變數的方式。

  • int *p 表示 p 是「指向 int 的指標」。
  • int **q 表示 q 是「指向 int * 的指標」。
  • *q 會取出 q 指向的指標;**q 再取出該指標所指向的整數。
  • 函式參數若是 int **,函式內可透過 *a 修改呼叫端的 int * 變數。

解題方法

題目程式有兩處型別錯誤,因此照原文無法產生有效輸出:

  1. 宣告 int **x1, y1, z1, **x2, y2, z2; 中,只有 x1 和 x2 是 int **;y1、z1、y2、z2 都是 int。因此 y1 = &z1 不符合型別,後續 *y1 也無法對整數解參照。
  2. 即使將 y1、y2 改宣告為 int *,第二個 printf 仍以 %d 輸出指標 y2,格式指定與引數型別不符,行為未定義。

依程式其餘內容推定,原意是將 y1、y2 宣告為 int *,並在第二個 printf 使用 *y2。以下依此修正推導。

初始狀態:

變數指向/內容
z110
z220
y1指向 z1
y2指向 z2
x1指向 y1
x2指向 y2

第一個 printf:

  • *y1 是 z1 的值,即 10。
  • *y2 是 z2 的值,即 20。
  • **x1 是 x1 經過 y1 指向 z1 後的值,即 10。
  • **x2 是 x2 經過 y2 指向 z2 後的值,即 20。

所以第一行輸出為:

10 20 10 20
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題30 分

A memory leak is a dynamically allocated-memory object that is not deallocated properly and cannot be accessed again. A double-free error happens when a deallocated-memory object is freed again. These issues often occur in programming languages like C and C++.

(a) (21%) There are three code fragments in (1), (2), and (3). L1~L17 are line numbers. Do the code fragments (1), (2), and (3) exist memory-leak or double-free problems at run-time? If yes, please move the existed free() statement or insert a free() statement to correct the problems. The modified programs do not have any memory-leak or double-free errors.
(Answer sample: insert a statement "free (...)" immediately after L1)

Code Fragment (1):

L1  #include <stdio.h>
L2  #include <stdlib.h>
L3
L4  int main(void)
L5  {
L6    int *ptr;
L7
L8    ptr = (int*) malloc(10);
L9    *ptr = 2020;
L10   printf("%d\n", *ptr);
L11   ptr = (int*) malloc(20);
L12   ptr[0] = 10;
L13   ptr[1] = 20;
L14   printf("%d %d\n", *ptr, ptr[1]);
L15   free(ptr);
L16   return 0;
L17 }

Code Fragment (2):

L1  #include <stdio.h>
L2  #include <stdlib.h>
L3    int main(void)
L4  {
L5    int *ptr;
L6    int i;
L7
L8    for (i=0; i<100; i++) {
L9      ptr = (int*) malloc(10);
L10     *ptr = i+1;
L11     printf("%d\n", *ptr);
L12   }
L13   free(ptr);
L14
L15   return 0;
L16 }
L17

Code Fragment (3):

L1  #include <stdlib.h>
L2  int main(void)
L3  {
L4    int *ptr;
L5    int i;
L6
L7    for (i=0; i<100; i++) {
L8      ptr = (int*) malloc(10);
L9      *ptr = i+1;
L10     if (i%2 == 0) {
L11       *ptr ++;
L12       free(ptr);
L13     }
L14   }
L15   return 0;
L16 }
L17

(b) (9%) Java programming language provides a garbage collection technique to avoid memory-leak errors. What is garbage collection?

登入後即可作答並保存紀錄。

這一題的完整詳解

(a) 動態記憶體錯誤分析與修正

判斷原則:每一次 malloc() 配置的記憶體區塊,都必須在指標遺失前,以原本回傳的起始位址呼叫恰好一次 free() 釋放;未釋放或遺失指標會造成 memory leak,對同一區塊重複呼叫 free() 則為 double-free。


Code Fragment (1)

  • 問題判斷:有 memory leak,無 double-free。
  • 原因分析:
    • L8 配置 10 bytes 記憶體並由 ptr 指向;L10 輸出後並未釋放。
    • L11 直接將新的 malloc(20) 位址覆寫給 ptr,導致 L8 配置的區塊失去唯一指標而無法釋放,造成 memory leak。
    • L15 的 free(ptr); 僅釋放 L11 配置的區塊。
  • 修正方式:
    在 L10 後(L11 重新指派 ptr 前)插入釋放敘述:

    Insert a statement free(ptr); immediately after L10.


Code Fragment (2)

  • 問題判斷:有 memory leak,無 double-free。
  • 原因分析:
    • for 迴圈共執行 100100 次,每次 L9 都會配置新的 10 bytes 區塊並覆寫 ptr。
    • 迴圈中沒有 free(ptr);,導致前 9999 次配置(i = 0 至 i = 98)的位址全數遺失,造成 memory leak。
    • 迴圈結束後的 L13 free(ptr); 僅能釋放最後一次(i = 99)配置的區塊。
  • 修正方式:
    將 L13 的 free(ptr); 移至 L11 後(即每次迴圈使用完畢立即釋放;注意不可保留 L13,否則最後一筆會變成 double-free):

    Move free(ptr); from L13 to immediately after L11.


Code Fragment (3)

  • 問題判斷:有 memory leak 與無效位址釋放(invalid free),無 double-free。
  • 原因分析:
    1. 奇數次迴圈造成 memory leak:free(ptr);(L12)位於 if (i%2 == 0) 條件式內,當 i 為奇數(i = 1, 3, ..., 99)時不會執行 free(ptr);,共 5050 個配置區塊未釋放而發生 memory leak。
    2. 偶數次迴圈因 *ptr ++; 造成無效位址釋放:在 C 語言中,後置遞增 ++ 的優先順序高於間接運算子 *,因此 L11 的 *ptr ++; 等價於 *(ptr++);,會將指標 ptr 本身向後移動 sizeof(int) bytes。接著 L12 呼叫 free(ptr); 時,傳入的已非 malloc() 回傳的原始位址,屬於未定義行為(invalid free),且原配置區塊未能正確釋放。
  • 修正方式:
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4 題10 分

Please answer the following questions for the two binary trees in the following figure.
🖼️【此處有附圖,請對照原卷】

(a) For each tree in the following figure, list the leaf nodes and the non-leaf nodes.
(b) Write out the inorder, preorder, and postorder traversals for each tree in the following figure.

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 4 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題評量資料結構中二元樹(Binary Tree)​的基本組成要素與三種深先搜尋走訪(Depth-First Traversals):

  1. 葉節點(Leaf Node / Terminal Node):分支度(Degree)為 00 的節點,亦即沒有任何子節點的節點。
  2. 非葉節點(Non-leaf Node / Internal Node):分支度大於 00 的節點,在二元樹中代表具有 11 個或 22 個子節點的節點。
  3. 二元樹走訪(Tree Traversals):
    • 前序走訪(Preorder Traversal):順序為「根節點 →\to 左子樹 →\to 右子樹」(V-L-R)。
    • 中序走訪(Inorder Traversal):順序為「左子樹 →\to 根節點 →\to 右子樹」(L-V-R)。
    • 後序走訪(Postorder Traversal):順序為「左子樹 →\to 右子樹 →\to 根節點」(L-R-V)。

解題方法

由原卷附圖讀取兩棵二元樹的結構如下:

  • 左圖(Tree 1):為一棵嚴格的左偏斜二元樹(Left-skewed binary tree),共包含 5 個節點(AA 至 EE),樹根為 AA。AA 的左子節點為 BB、BB 的左子節點為 CC、CC 的左子節點為 DD、DD 的左子節點為 EE;所有節點皆無右子節點。
  • 右圖(Tree 2):共包含 9 個節點(AA 至 II),樹根為 AA。
    • AA 的左子節點為 BB、右子節點為 CC。
    • BB 的左子節點為 DD、右子節點為 EE。
    • CC 的左子節點為 FF、右子節點為 GG。
    • DD 的左子節點為 HH、右子節點為 II。
    • 節點 E,F,G,H,IE, F, G, H, I 均無子節點。

(a) 判斷各樹的葉節點與非葉節點

  1. 左圖(Tree 1):

    • 葉節點(Leaf nodes):僅節點 EE 的分支度為 00(無子節點)。
    • 非葉節點(Non-leaf nodes):節點 A,B,C,DA, B, C, D 的分支度皆為 11。
  2. 右圖(Tree 2):

    • 葉節點(Leaf nodes):節點 H,I,E,F,GH, I, E, F, G 的分支度皆為 00。
    • 非葉節點(Non-leaf nodes):
      • 分支度為 22 的節點:A,B,C,DA, B, C, D。

(b) 推導各樹的走訪序列

  1. 左圖(Tree 1)的走訪推導:

    • 前序(Preorder, V-L-R):
      先訪問樹根 AA,遞迴走訪左子樹(依序訪問 B,C,D,EB, C, D, E),均無右子樹。
      Preorder: A,B,C,D,E\text{Preorder: } A, B, C, D, E
    • 中序(Inorder, L-V-R):
      一路往左走到最底部的葉節點 EE 先輸出,再依序返回訪問各父節點 D,C,B,AD, C, B, A。
      Inorder: E,D,C,B,A\text{Inorder: } E, D, C, B, A
    • 後序(Postorder, L-R-V):
      各子樹皆先走訪左子樹,右子樹為空,最後訪問根節點。由底層逐層向上訪問:E→D→C→B→AE \to D \to C \to B \to A。
      Postorder: E,D,C,B,A\text{Postorder: } E, D, C, B, A
  2. 右圖(Tree 2)的走訪推導:

    • 前序(Preorder, V-L-R):

      1. 訪問樹根 AA。
      2. 走訪左子樹(根為 BB):
        • 訪問 BB。
        • 走訪 BB 的左子樹(根為 DD):訪問 D→D \to 訪問左子節點 H→H \to 訪問右子節點 II。
        • 走訪 BB 的右子節點:訪問 EE。
        • 左子樹前序序列為:B,D,H,I,EB, D, H, I, E。
      3. 走訪右子樹(根為 CC):
        • 訪問 C→C \to 訪問左子節點 F→F \to 訪問右子節點 GG。
        • 右子樹前序序列為:C,F,GC, F, G。
      4. 結合得前序走訪:A,B,D,H,I,E,C,F,GA, B, D, H, I, E, C, F, G。
    • 中序(Inorder, L-V-R):

      1. 走訪左子樹(根為 BB):
        • DD 的子樹中序:左子節點 H→H \to 根 D→D \to 右子節點 II(即 H,D,IH, D, I)。
        • 訪問根 BB。
        • 訪問右子節點 EE。
        • 左子樹中序序列為:H,D,I,B,EH, D, I, B, E。
      2. 訪問樹根 AA。
      3. 走訪右子樹(根為 CC):
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5 題20 分

Carefully examine the following twenty sentences and give your answer, true (T) or false (F), for each sentence. Two points are given for each correct answer, whereas three points are deducted for each incorrect answer. Therefore, please do not guess at the answer if you are unsure since the expected value is negative. Note that the deduction for incorrect responses only affects the 20 points of this question.

(a) The fields of a user-defined data type "union" in C share their memory space.
(b) The maximum number of nodes in a binary tree of depth 10 is 1024.
(c) We can invert a linked list in place (i.e., no need to use additional memory with a size proportional to the elements in the linked list).
(d) A heap (i.e., priority queue) is better than a binary search tree when we examine whether an element is stored in the data set.
(e) Dijkstra algorithm cannot deal with the graph with negative edge weights.
(f) In most cases, prime numbers are good divisors for implementing a division-based hash function.
(g) A balanced binary search tree is always the best binary search tree for storing the records even if the identifiers are with different searching probabilities.
(h) Quick sort is faster than insertion sort even if the input size is small.
(i) A stack is a first-in-first-out data structure.
(j) A user-defined "struct" in C creates a data type that can be used to group items of possibly different types into a single type.

登入後即可作答並保存紀錄。

這一題的完整詳解

本題為是非題(True / False),共 10 小題,每題答對得 2 分、答錯倒扣 3 分。各小題解析如下:

(a) T
在 C 語言中,union(共用體)的所有成員共享同一塊記憶體空間,該空間大小至少等於最大成員所需的大小,同一時間僅能有效儲存其中一個成員的值。

(b) F
無論二元樹的深度從 00 或從 11 開始計算,深度為 dd 的二元樹最多可容納的節點數分別為 2d+1−12^{d+1}-1(樹高 1010、共 1111 層時為 211−1=20472^{11}-1 = 2047)或 2d−12^d-1(共 1010 層時為 210−1=10232^{10}-1 = 1023)。最大節點數必為奇數,不可能是偶數 10241024(1024=2101024 = 2^{10} 僅是第 1111 層單層的最大節點數)。

(c) T
反轉鏈結串列可透過迭代法,僅使用三個指標變數(如 prev、curr、next)逐一修改節點的指標方向來完成,額外記憶體空間複雜度為 O(1)O(1),不需要與節點數量成正比的額外空間(註:若改用遞迴法則需 O(n)O(n) 函式呼叫堆疊空間,非 in-place)。

(d) F
堆積(Heap / 優先佇列)僅能保證在 O(1)O(1) 時間內取得極值(最大或最小值),若要搜尋任意元素是否存在於資料集中,必須線性掃描,時間複雜度為 O(n)O(n);相對地,二元搜尋樹(BST)的平均搜尋時間複雜度為 O(log⁡n)O(\log n)(平衡 BST 最壞亦為 O(log⁡n)O(\log n)),因此檢查元素是否存在時 BST 優於 Heap。

(e) T
Dijkstra 演算法採用貪婪策略,假設所有邊的權重皆為非負數,才能保證每次從尚未確定的頂點中選出距離起點最近者時,該距離已是最短路徑長度。若圖中存在負權重邊,該假設不成立,可能導致錯誤結果(此時應改用 Bellman-Ford 演算法)。

(f) T
在除法雜湊函數 h(k)=k mod Mh(k) = k \bmod M 中,若 MM 與鍵值分布存在共同因數(例如 MM 為 22 的冪次或合數),容易使鍵值集中於特定雜湊值而增加碰撞。選取質數作為除數 MM 可減少鍵值規律帶來的碰撞,使雜湊結果分布較為均勻。

(g) F
平衡二元搜尋樹(如 AVL 樹、紅黑樹)能保證最壞情況搜尋時間為 O(log⁡n)O(\log n),但這是針對所有鍵值搜尋機率相近的情況。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 6 題20 分

Consider the following graph. Please write down the sequences of edges selected by the following two algorithms, respectively.
🖼️【此處有附圖,請對照原卷】

(a) (10%) Kruskal's algorithm.
(b) (10%) Prim's algorithm (start from vertex a).

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 5 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

最小生成樹是在連通且無向的加權圖中,選出連接所有頂點、沒有環且總權重最小的邊集合。Kruskal 演算法依邊權重由小到大考慮,只有在加入邊不會形成環時才選取;Prim 演算法則從指定頂點開始,每次選擇一條連接「已納入頂點」與「未納入頂點」且權重最小的邊。

解題方法

原圖有頂點 a,b,c,d,e,f,g,h,ia,b,c,d,e,f,g,h,i,邊及權重為:ab=4ab=4、ah=8ah=8、bh=11bh=11、bc=9bc=9、hi=7hi=7、hg=1hg=1、ig=6ig=6、ic=2ic=2、cd=7cd=7、cf=4cf=4、gf=2gf=2、df=14df=14、de=9de=9、ef=6ef=6。下方分別依權重排序執行 Kruskal,以及從 aa 開始逐步擴張執行 Prim。

(a) Kruskal 演算法

依權重由小到大考慮邊;權重相同時,依序處理不影響本題的最小生成樹結果。

  1. 選 hg(1)hg(1)。
  2. 選 ic(2)ic(2)。
  3. 選 gf(2)gf(2),連接兩個不同的連通分量。
  4. 選 ab(4)ab(4)。
  5. 選 cf(4)cf(4),連接目前不同的連通分量。
  6. 權重為 66 的 igig 會形成環,略過;選 ef(6)ef(6)。
  7. 權重為 77 的 hihi 會形成環,略過;選 cd(7)cd(7)。
  8. 選 ah(8)ah(8),將 {a,b}\{a,b\} 連到其他頂點。

原圖有 99 個頂點,因此最小生成樹需有 9−1=89-1=8 條邊;此時已選滿 8 條。

(b) Prim 演算法,從 aa 開始

每一步只在連接已納入頂點與未納入頂點的邊中,選權重最小者:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題