免費詳解 / 中正 / 軟體 / 104

104 中正資工所考古題解答|軟體逐題詳解

104 中正資工所軟體考古題的逐題詳解,共 15 題,免費公開。

逐題詳解

19%

Consider the following programming languages: C, C++, Java, Ruby, Python.

a. (3%) Which languages do not require a compiling stage before execution? b. (3%) Which languages support object-oriented programming? c. (3%) Which languages are supported with compiler (or interpreter) on both Windows and Linux operating systems?

a. Ruby、Python:由 interpreter 直接讀 source code 執行。C、C++ 要先 compile 成機器碼;Java 要先用 javac compile 成 bytecode 才交給 JVM,所以也有 compile 階段

b. C++、Java、Ruby、Python(有 class、inheritance、polymorphism);C 是 procedural language,不算

c. 五個都有:C/C++ 有 GCC、Clang;Java 有兩個平台的 JDK;Ruby、Python 都有 Windows 與 Linux 版 interpreter

a. Ruby、Python b. C++、Java、Ruby、Python c. 全部五個

26%

Write down the output of the following C program

void reset_array(int a[])
{
   for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++) a[i]=0;
}

void main()
{
   int a[]={1,2};
   reset_array(a);
   for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
      printf("%d ",a[i]);
}

參數 int a[] 其實就是 int *a,所以 reset_array 裡的 sizeof(a) 是 pointer 的大小,不是整個陣列的大小

pointer 4 bytes(32 位元):\(4/4=1\),迴圈只跑 1 次,只清 a[0]

main 裡 a 是真的陣列:\(8/4=2\),印出兩個元素

在 pointer 8 bytes 的 64 位元環境,reset_array 的迴圈剛好跑 2 次,輸出變成 0 0,但這只是湊巧——function 並不知道陣列長度;要把長度另外傳進去

0 2(pointer 4 bytes;64 位元則為 0 0)

310%

Write down the output of the following C program

void main()
{
   int s[5]={2, 10, 5, 3,7};
   int *p=s , *ptr=s+2 ;
   printf("A:%d\n", *p+1);
   printf("B:%d\n", *ptr);
   printf("C:%d\n", s[0]);
   printf("D:%d\n", *p++);
   printf("E:%d\n", *p);
   printf("F:%d\n", (*p)--);
   printf("G:%d\n", ++*p);
   printf("H:%d\n", *++p);
   printf("I:%d\n", *(s+2));
   printf("J:%d\n", --s[0]);
}

起始 \(s=\{2,10,5,3,7\}\),p 指向 s[0],ptr 指向 s[2]

運算式輸出之後的狀態
A*p+12+1 = 3不變
B*ptr5不變
Cs[0]2不變
D*p++2p 移到 s[1]
E*p10不變
F(*p)--10(先印)s[1] 變 9
G++*p10s[1] = 10
H*++p5p 移到 s[2]
I*(s+2)5不變
J--s[0]1s[0] = 1

*p++ 的 ++ 作用在 p(postfix 優先權高於 *);++*p、(*p)-- 才是改指到的值

A:3 B:5 C:2 D:2 E:10 F:10 G:10 H:5 I:5 J:1(每個一行)

44%

List two differences between a function prototype and a function header.

① prototype 是 declaration,後面接分號、沒有 function body;header 是 function definition 的第一行,後面直接接 { … } 的 body

② prototype 的參數名稱可以省略,只寫型別(int max(int, int););header 的參數一定要有名稱,body 才能使用

prototype 要放在呼叫之前(或 header file 裡),compiler 才知道回傳型別與參數型別

58%

Is the following class declaration correct? If not, clearly indicate what lines are wrong and why they are wrong.

class M {                                          // 1
  private:                                             // 2
    int value;                                         // 3
                                                       // 4
  public:                                              // 5
    M (int i)            { value = i; }                // 6
    void showValue () { cout << value << endl; }       // 7
                                                       // 8
    void fn1 (int& input)              { value = input; }   // 9
    void fn2 (const int& input)        { value = input; }   // 10
    void fn3 (int& input) const        { value = input; }   // 11
    void fn4 (const int& input) const  { value = input; }   // 12
                                                       // 13
    void fn5 (int& input)              { input = value; }   // 14
    void fn6 (const int& input)        { input = value; }   // 15
    void fn7 (int& input) const        { input = value; }   // 16
    void fn8 (const int& input) const  { input = value; }   // 17
};                                                     // 18

不正確。錯在 line 11、12、15、17:

linefunction結果原因
9fn1對讀 input、寫 value
10fn2對只讀 const 參數
11fn3錯const member function 不能改 value
12fn4錯同上
14fn5對input 是非 const reference,可以寫
15fn6錯input 是 const reference,不能被賦值
16fn7對函式尾端的 const 只限制成員;讀 value、寫非 const 參數都行
17fn8錯input 是 const reference,不能被賦值

函式後面的 const 讓 this 變成 const M*,只禁止修改 data member;參數前面的 const 只禁止修改那個參數,兩者互不相干

line 11、12、15、17 錯

67%

Is the following program correct? If not, clearly indicate all the lines in the code that are not correct and also what is wrong with them. If the compiling errors are fixed, what is the output of line 29?

class Parent {                                   // 1
  private:                                           // 2
    int a, b;                                        // 3
  protected:                                         // 4
    int c, d;                                        // 5
  public:                                            // 6
    int e, f;                                        // 7
                                                     // 8
    Parent () {a = b = c = d = e = f = 0; }          // 9
    void showValues() {                              // 10
        cout << a << " " << b << " " << c << " "         // 11
             << d << " " << e << " " << f << endl; }    // 12
    friend void foo(Parent);                         // 13
};                                                   // 14
                                                     // 15
class Child : public Parent {                        // 16
    Child ()   { a = b = c = d = e = f = 0; }        // 17
    void fn1 () { a = 2; b = 3; }                    // 18
    void fn2 () { c = 4; d = 5; }                    // 19
    void fn3 () { e = 6; f = 7; }                    // 20
};                                                   // 21
                                                     // 22
void foo (Parent p) {                                // 23
    p.a = p.b = p.c = p.d = p.e = p.f = 10; }        // 24
                                                     // 25
int main() {                                         // 26
    Parent *p = new Child();                         // 27
    foo(*p);                                         // 28
    p->showValues();                                 // 29
}                                                    // 30

不正確,錯在:

line 17 a、b 是 Parent 的 private 成員,public 繼承後 Child 仍然存取不到 → compile error

line 18 fn1() 設定 a、b,同樣存取不到 → compile error

line 27 class Child 裡沒寫存取權,class 預設是 private,所以 Child() 是 private constructor,main 呼叫 new Child() 不到;要在 line 16 之後補 public:

其他行沒問題:c、d 是 protected,Child 可以存取;e、f 是 public;foo 在 Parent 裡宣告為 friend,所以 line 24 存取 p.a、p.b 等 private/protected 成員是合法的

修正後(補 public:、line 17 只設 c~f、line 18 拿掉 a、b):new Child() 先跑 Parent() 把六個成員都設成 0;foo(*p) 是 pass by value,函式裡改的是複本,*p 沒變;p->showValues() 印出原物件的值

line 17、18、27 有錯;修正後 line 29 輸出 0 0 0 0 0 0

76%

What is the output of the following program?

class A {
  public:
    A () { cout << "In A's constructor" << endl; }
    ~A () { cout << "In A's destructor" << endl; }
};

class B : public A {
  public:
    B () { cout << "In B's constructor" << endl; }
    ~B () { cout << "In B's destructor" << endl; }
};

class C : public B {
  public:
    C () { cout << "In C's constructor" << endl; }
    ~C () { cout << "In C's destructor" << endl; }
};

int main() {
    C x1;
    C *x2 = new C;
}

建構由 base 往 derived:A → B → C;x1 與 new C 各建一次,印兩輪

main 結束時只有 local 物件 x1 會自動解構,順序反過來 C → B → A;x2 指到的物件在 heap 上、沒有 delete,destructor 不會被呼叫

In A's constructor
In B's constructor
In C's constructor
In A's constructor
In B's constructor
In C's constructor
In C's destructor
In B's destructor
In A's destructor
85%

Suppose we are to insert the following data in the given order, D, M, Q, A, E, K, to build an AVL tree. Please show the final AVL tree.

DMQ
① 插 D、M、Q:D 失衡(RR)
DMQ
② 以 D 左旋,M 升為根
ADEMQ
③ 插 A、E
ADEKMQ
④ 插 K:M 失衡(LR)
ADEKMQ
⑤ 先以 D 左旋(E 升起)
ADEKMQ
⑥ 再以 M 右旋,E 升為根

插 D、M、Q:D 的右邊太重(RR),以 D 左旋,M 升為根

插 A、E:A 在 D 左、E 在 D 右(\(E\lt M\),進左子樹;\(E>D\),在 D 右邊)

插 K:路徑 M → D(左)→ E(右)→ E 的右邊,M 的左邊比右邊高 2,且新點在左子樹的右邊(LR):先對 D 左旋(E 升起,D 帶著 A 當左子,K 當 E 的右子),再對 M 右旋(E 升為根,M 接 E 原本的右子 K 當左子、保留 Q 當右子)

ADEKMQ

紅筆標的是剛插入的點與失衡點

根 E;左 D(左子 A);右 M(左子 K、右子 Q)

910%

Please write a C function to insert a new node in the tail of a linked list. Assume that a data node has the following data structure:

struct lnode {
    char key[MaxKeyLen];
    struct lnode *next;
}

The input to the function is a string.

#include <stdlib.h>
#include <string.h>

/* 回傳新的 head;原本是空串列時,新節點就是 head */
struct lnode *insertTail(struct lnode *head, const char *str)
{
    struct lnode *node = (struct lnode *)malloc(sizeof(struct lnode));
    if (node == NULL) return head;            /* 配置失敗就不動 */
    strncpy(node->key, str, MaxKeyLen - 1);
    node->key[MaxKeyLen - 1] = '\0';
    node->next = NULL;

    if (head == NULL) return node;            /* 空串列 */
    struct lnode *cur = head;
    while (cur->next != NULL)                 /* 走到最後一個節點 */
        cur = cur->next;
    cur->next = node;
    return head;
}

字串不能用 = 指定給字元陣列,要用 strncpy 複製,並自己補 '\0'(超過 MaxKeyLen 時才有結尾);空串列要單獨處理,否則 cur->next 會對 NULL 取值

1010%

Please write a C program to evaluate a postfix expression. The expression contains two kinds of operators: + and *. The operands are positive integers. The expression will be given in the command line arguments. For example, posteval "10 20 5 * +"

從左到右掃 token:數字就 push;運算子就 pop 兩個數、算完 push 回去;掃完 stack 剩下的唯一一個數就是答案

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 100

long stack[MAX];
int top = -1;

void push(long v)
{
    if (top == MAX - 1) { fprintf(stderr, "stack overflow\n"); exit(1); }
    stack[++top] = v;
}
long pop(void)
{
    if (top < 0) { fprintf(stderr, "invalid expression\n"); exit(1); }
    return stack[top--];
}

int main(int argc, char *argv[])
{
    /* 整串放在一個引數,或拆成多個引數都能處理 */
    for (int i = 1; i < argc; i++) {
        for (char *tok = strtok(argv[i], " "); tok != NULL; tok = strtok(NULL, " ")) {
            if (isdigit((unsigned char)tok[0])) {
                push(atol(tok));
            } else if (strcmp(tok, "+") == 0 || strcmp(tok, "*") == 0) {
                long b = pop();              /* 先 pop 的是右運算元 */
                long a = pop();
                push(tok[0] == '+' ? a + b : a * b);
            } else {
                fprintf(stderr, "unknown token: %s\n", tok);
                return 1;
            }
        }
    }
    if (top != 0) { fprintf(stderr, "invalid expression\n"); return 1; }
    printf("%ld\n", pop());
    return 0;
}

posteval "10 20 5 * +":

token動作stack(底→頂)
10push10
20push10, 20
5push10, 20, 5
*pop 5、20,push 10010, 100
+pop 100、10,push 110110

輸出 110。引數要加引號(* 在 shell 會被展開成檔名);這題只有 +、*,運算元順序不影響結果,但寫成 a op b 才能直接擴充到 -、/

輸出 110

114%

What is the lower bound of the comparison based sorting algorithm? Justify your answer.

任何 comparison sort 都可畫成 binary decision tree:內部節點是一次比較,leaf 是一種輸出排列

\(n\) 個相異元素有 \(n!\) 種排列,每種都要出現在某個 leaf → leaf 數 \(\ge n!\)

高度 \(h\) 的 binary tree 至多 \(2^h\) 個 leaf:

\(2^h\ge n!\ \Rightarrow\ h\ge\log_2n!\ge\log_2\left(\dfrac n2\right)^{n/2}=\dfrac n2\log_2\dfrac n2=\Omega(n\log n)\)

\(h\) 就是 worst case 的比較次數

\(\Omega(n\log n)\)

125%

The following table shows the used alphabets and their frequencies of appearance (happen to be prime number series) when encrypting a piece of information. Please use greedy algorithm to find its optimal ternary Huffman code for transmission.

alphabetabcdefgh
frequency3571113171923

ternary 每次合併 3 個、節點數減 2,要剛好合併到剩 1 個,節點數必須是奇數;\(n=8\) 不是,先補一個頻率 0 的 dummy 湊成 9 個,每次取最小的三個合併:

回合取出合併成剩下
10、3、587, 8, 11, 13, 17, 19, 23
27、8、112613, 17, 19, 23, 26
313、17、194923, 26, 49
423、26、499898

每個節點的三個子節點由小到大標 0、1、2:

01010122201298h2326c78dummy0a3b5d1149e13f17g19

字母abcdefgh
code11111210122021220

總長度 \(3\cdot3+5\cdot3+7\cdot2+11\cdot2+13\cdot2+17\cdot2+19\cdot2+23\cdot1=181\),平均每字母 \(181/98\approx1.85\) 個 ternary digit

dummy 拿到 code 110,不會被使用

136%

Analyze each of (1) Quicksort, (2) heap sort, and (3) Merge sort to determine if they are asymptotically optimal or not.

comparison sort 的下界是 \(\Omega(n\log n)\)(第 11 題),worst case 做到 \(O(n\log n)\) 就是 asymptotically optimal

bestaverageworstoptimal?
Quicksort\(n\log n\)\(n\log n\)\(n^2\)worst case 不是(average case 是)
Heap sort\(n\log n\)\(n\log n\)\(n\log n\)是
Merge sort\(n\log n\)\(n\log n\)\(n\log n\)是

Quicksort:pivot 每次選到最大或最小(例如已排序的輸入配上固定取第一個當 pivot),遞迴深度變 \(n\),worst case \(\Theta(n^2)\),所以以 worst case 看不是 asymptotically optimal;只有 expected/average 是 \(\Theta(n\log n)\)

(1) 不是(worst case \(n^2\)) (2) 是 (3) 是

144%

Given an array \(A\) with \(n\) elements that only the adjacent elements might be out of order—i.e., if \(i\lt j\) and \(A[i]>A[j]\), then we will know that \(j=i+1\). To sort \(A\), which of Insertion-sort or Merge-sort is better or equivalent in terms of the time cost? Justify your answer.

條件:所有 inversion 都是相鄰的一對

若某元素與左右鄰居都逆序(\(A[i-1]>A[i]>A[i+1]\)),會推出 \(A[i-1]>A[i+1]\),是距離 2 的 inversion,違反條件;所以 inversion 彼此不重疊,總數 \(I\le\lfloor n/2\rfloor\)

insertion sort 時間 \(\Theta(n+I)\),每個元素最多往左移一格:\(\Theta(n)\)

merge sort 不管輸入怎樣都要切 \(\log n\) 層、每層 merge \(\Theta(n)\),固定 \(\Theta(n\log n)\)

insertion sort 較好:\(\Theta(n)\) 對 \(\Theta(n\log n)\)

156%

Except the running time, what are the main differences between Dijkstra, Bellman-Ford, and Floyd-Warshall algorithms?

DijkstraBellman-FordFloyd-Warshall
問題single-sourcesingle-sourceall-pairs
負權重邊不行可以可以
負環不處理能偵測能偵測(對角線出現負值)
策略greedy反覆 relax 所有邊dynamic programming
常用表示法adjacency list + priority queueedge listadjacency matrix

Dijkstra 每次選目前距離最小的點並定案,這個 greedy 要靠「邊權重非負」才成立,有負邊時定案的距離可能被更短的路徑推翻

Bellman-Ford 把所有邊 relax \(|V|-1\) 輪,第 \(|V|\) 輪若還能更新就有負環

Floyd-Warshall 依序允許頂點 \(k=1,\dots,n\) 當中繼點,一次算出所有點對的最短距離;可以有負邊,不能有負環

109–115 的詳解也是這樣寫的。

國立中正大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 351 頁,逐題推導。

購買 · NT$ 850 看中正其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

題目有錯、詳解看不懂,都歡迎來信

想許願補哪一份、對購買有疑問也可以,收到後會盡快回覆。

william.chou.123123@gmail.com