lawpalyer logo

資訊處理 110 年程式語言考古題

民國 110 年(2021)資訊處理「程式語言」考試題目,共 11 題 | 資料來源:考選部

0 題選擇題 + 11 題申論題

在程式語言中有所謂的早期繫結(early binding)和晚期繫結(late binding), 請詳述這兩個繫結的差異性和使用時機點,並以C++程式語言說明如何達 成晚期繫結的功能。(20 分)
第5 代行動通訊標準(5G)的網路架構包含終端設備、接取網路、邊緣 運算、核心網路、外部服務等,其系統程式的開發需要許多軟體安全開 發機制。 為強化5G 系統開發之安全性,可使用SSDLC 和DevSecOps 開發流 程;請說明DevSecOps 開發流程的要點。(15 分) 軟體安全開發機制,包含建立威脅模型與分析、設計威脅模型的緩解 措施。請說明若威脅模型中有會話劫持(Session hijacking)攻擊、機 敏資料洩漏、Cookie 操控(Manipulation),其緩解措施分別如何設計。 (10 分)
語意分析(Semantics Analysis)對於程式語言的正確執行非常重要。 以下是Java 和C++程式,請說明程式編譯、執行結果與其原因,以及 程式指令之意義或影響。(18 分) ⑴Java 程式 static void test1(){ int n; int [] x = new int[n]; } ⑵Java 程式 static void test2(){ int n=0; int [] x = new int[n]; } ⑶Java 程式 static void test3(){ int n=0; int [] x = new int[n]; x[0]=0; } ⑷C++程式 void test4(){ int n; int x[n]; } ⑸C++程式 void test5(){ int n=0; int x[n]; x[0] =0; } ⑹C++程式 void test6(){ int n=0; int *x = new int[n]; x[0] =0; } 請說明Java 與C++語言在陣列宣告上的語意分析的方法,與其優缺 點。(7 分)
函式之間的呼叫,其參數的傳送可分為那幾種?請詳述之;並以任何一種 程式語言撰寫兩個整數對調的情形,並加以說明最後處理的結果。需註明 使用的程式語言。(20 分)
二元樹(Binary tree)可使用陣列(Array)和鏈結串列(Linked list)設計。 以二元樹表達算術運算式c *(d - e)+ f / g,並寫出此運算式的後置 表示法(Postfix Expression)。(10 分) 以一維陣列設計二元樹,節點循序編號,從陣列1 開始表達根節點, 節點編號規則為:左子樹是父節點編號乘以2,右子樹是父節點編號 乘以2 加1,-1 表示沒有子節點。請填寫以下陣列表達題算術運算 式之二元樹。(10 分) 索引 0 1 2
程式驗證的應用。 請說明「測試驅動發展方法(Test Driven Development, TDD)」的概念 及優點。(7 分) 有一MySort 類別的方法int[] binarySort(int data[]),將陣列data 內的 資料由小到大排序後回傳,請依據TDD 的概念設計測試案例。(10 分) 請以Java/JUnit 語言完成以下測試程式(I)~(II)。(8 分) @Test public void testBinarySort(MySort (I) ){ int[] source = {2, 3, 5, 9, 12, 7}; int[] target = obj.binarySort(source); for(int i=0; i<source.length-1; i++){ assertTrue(target[i] < (II) ); } }
程式語言中有一重要的主題是指標(pointer)。何謂指標?其功用為何? int a = 100;並詳述⑴int *ptr = &a;⑵int *fn(int a);⑶int(*fn)(int a); ⑷int *arr[3];⑸int(*arr)[3]。(30 分)
建構股票交易資料庫(Stock),請寫出SQL 指令。 客戶表格(Customer) 客戶編號(cid) [整數、主鍵] [自動增加] 客戶姓名(cname) [少於10 字元可變字串] 客戶帳戶餘額 (balance) [整數、非空值] 客戶融資餘額 (margin) [整數、非空值] 證券交易表格(StockTrade) 交易編號 (id) [整數、主鍵] [自動增加] 證券編號 (sid) [整數、非空值] 證券每股購入價格 (price) [整數、非空值] 證券購入股數 (share) [整數、非空值] 客戶編號(cid) [整數、非空值] 造出Customer, StockTrade 表格。(10 分) CREATE TABLE Customer ( _____________________ ); CREATE TABLE StockTrade ( _____________________ ); 查詢客戶姓名是"Tom"所有購買股票編號與購入總股數。(5 分) 撰寫Store Procedure,造出一個暫時的資料表Report,含兩個整數資 料欄位(證券編號sid, 證券價格price);加入10 筆資料,再根據證券 價格由小到大排序,查詢列出此10 筆資料。(10 分) delimiter $$ CREATE PROCEDURE x() BEGIN DECLARE i INT DEFAULT 1; ____________________ END$$
程式語言中有一重要的主題是指標和參考(reference),它們當用來撰寫 鏈結串列或樹狀結構的問題。我們將它用來處理鏈結串列,假設有一單向 鏈結串列(singly linked list)之節點中有三個項目,分別是id(整數型態)、 score(浮點數型態)以及next(指標或參考型態),如下圖所示: id score next 今有一節點名為ptr,試分別撰寫將此ptr 節點加入於已含有多個節點的單 向鏈結串列之尾端,以及刪除鏈結串列尾端節點的片段程式。可以任何程 式語言撰寫之,但請註明使用的程式語言。(30 分)
程式例外處理的設計對於資訊系統的可靠性非常重要。 請完成以下C++程式(I)~(V)指令,處理兩數相除的例外狀況,使 輸出為:(15 分) Exception:empty Exception:not a number Quotient:Exception:divided by zero Quotient:2.4 #include <iostream> #include <exception> #include <string.h> #define N 10 using namespace std; class EmptyException:public exception { public: virtual const char* what()const throw(){ (I) ; } }; class NotNumberException:public exception { public: virtual const char* what()const throw(){ (II) ; } }; class DividedByZeroException:public exception { public: virtual const char* what()const throw(){ (III) ; } }; int valid(const char x[N]){ int result=0; if(strlen(x)==0)throw EmptyException(); for(int i=0; i<strlen(x); i++){ if(!isdigit(x[i])) throw NotNumberException(); result = (IV) ; } return result; } double quotient(int n1, int n2){ if( (V) ) throw DividedByZeroException(); return static_cast<double>(n1/n2); } void test(const char x1[N], const char x2[N]){ int n1, n2; try { n1=valid(x1); n2=valid(x2); cout<<"Quotient:"<<quotient(n1, n2); } catch(EmptyException &e){ cout<<"Exception:"<< e.what(); } catch(NotNumberException &e){ cout<<"Exception:"<< e.what(); } catch(DividedByZeroException &e){ cout<<"Exception:"<< e.what(); } cout<<endl; } int main(){ test("",""); test("a","12"); test("10","0"); test("12","5"); return 0; } 請說明使用try-catch 與if-else,處理例外狀況的優缺點。(5 分) 請說明C++與Java 在try-catch 中finally 設計的異同與其理由。(5 分)
9 10 11 12 13 14 15 值 + * / 請根據右下圖完成下面C 程式實作(I)~(V)之鏈結串列的表示,其中 btree 為指向一個二元樹的指標變數。(5 分) (I) node { (II) ; (III) ; (IV) ; } treeNode_t; typedef (V) tree_t; tree_t btree; btree data left right data left right data left right 三、給定正整數二維陣列,起點為其中數值最大的點。從起點開始移動,求 經過點的數值之加總。移動規則:⑴從相鄰的點(上、下、左、右)選 擇一個最大的值移動;⑵走過的點不能重複。 請完成遞迴程式與非遞迴程式(I~XI)空格,使以下C 程式均能執行 出下列結果。(22 分) path= 88, 42, 31, 18, 23, 21, 68, 36, 55, 77, 66, 63, 28, 33, 52, sum=701 請比較遞迴與非遞迴程式記憶體空間使用狀況。(3 分) #include <stdio.h> #define maxN 30 const int NV = -1; int max(int x, int y){int r = x>y?(I) ; return r;} int findR(int m[maxN][maxN], int x, int y, int sum){ int mi = max(max(m[x+1][y],m[x-1][y]),max(m[x][y+1],m[x][y-1])); sum += m[x][y]; printf("%d, ", m[x][y]); m[x][y] = NV; if (mi == NV) return (II) ; if (m[x+1][y] == mi) return findR(m, (III) , sum); if (m[x-1][y] == mi) return findR(m, (IV) , sum); if (m[x][y+1] == mi) return findR(m, (V) , sum); if (m[x][y-1] == mi) return findR(m, (VI) , sum); } int findI(int m[maxN][maxN], int x, int y, int sum){ while(1){ int mi = max(max(m[x+1][y],m[x-1][y]),max(m[x][y+1],m[x][y-1])); sum += m[x][y]; printf("%d, ", m[x][y]); m[x][y] = NV; if (mi == NV) (VII) ; else if (m[x+1][y] == mi) (VIII) ; else if (m[x-1][y] == mi) (IX) ; else if (m[x][y+1] == mi) (X) ; else (XI) ; } return sum; } int main(){ int x=0, y=0, mi=NV, n=5, m=6; int map[maxN][maxN]={{NV, NV, NV, NV, NV, NV, NV}, {NV, 11, 15, 23, 18, 31, NV}, {NV, 31, 68, 21, 88, 42, NV}, {NV, 19, 36, 52, 33, 28, NV}, {NV, 12, 55, 77, 66, 63, NV}, {NV, NV, NV, NV, NV, NV, NV}}; // 邊界都是NV for (int i = 1 ; i <= n ; i++){ for (int j = 1 ; j <= m ; j++){ if (mi < map[i][j]){ mi = map[i][j]; x=i; y=j; } } } printf("path= "); printf("sum=%d\n",findR(map, x, y, 0)); return 0; } 四、敘述統計學上中位數和平均數均為數據資料的集中趨勢,中位數是將一 組數值資料由小到大排列,最中間的數值為中位數。若有奇數個資料, 則取最中間的數值為中位數,例如1, 2, 3, 3, 4, 6, 7, 7, 21 的中位數是4; 若有偶數個資料,則取最中間兩個數值的平均為中位數,例如1, 2, 3, 3, 4, 6, 7, 7, 8, 21 的中位數是(4+6)/2=5。算術平均數是將一組數值加總, 除以這組數值的個數,例如1, 2, 3, 3, 4, 6, 7, 7, 21 的算術平均數=54/9=6。 以下C++程式輸出:4, 6, 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include <iostream> #include <algorithm> using namespace std; class Compute{ public: void setData(int *, int); (I) =0; protected: int *data, d_size; }; class ComputeMedian: public Compute{ public: double getNum(); }; class ComputeMean: public Compute{ public: double getNum(); }; void Compute::setData(int *d, int s){ data = d; d_size = s; sort(data, data+d_size); } double ComputeMedian::getNum(){ if ( (II) ==1) return data[d_size/2]; else return (data[ (III) ]+data[1+d_size/2])/2.0; } double ComputeMean::getNum(){ double sum=0, avg=0; for (int i=0; i<d_size; i++) sum = sum + data[i]; return ( (IV) ); } class Report { public: Report(int *d, int s){ data = d; d_size = s; } void setCompute(Compute *c) { cp = c; } void report(){ cp-> (V) ; cout<<cp->getNum()<<", "; } private: int *data, d_size; Compute *cp; }; int main(){ int data[10] ={6, 7, 1, 21, 2, 3, 4, 3, 7}; Report r(data, 9); Compute *cp1 = new ComputeMedian(); Compute *cp2 = new ComputeMean(); r.setCompute(cp1); r.report(); r.setCompute(cp2); r.report(); return 0; } 請完成程式碼(I~V)使程式正常運作。(15 分) 請根據程式碼完成下面UML 類別圖的關係連線,並說明此設計對模 組耦合性(Coupling)的影響。(10 分) {abstract} Compute -data -d_size +setData() +getNum(){abstract} ComputeMean +getNum() ComputeMedian +getNum() Report -data -d_size +setCompute() +report()
(1) 15 分