程式設計 申論題歷屆試題與參考架構

高考三級,民國 111~112 年共 2 份試卷、8 題,其中 4 題附參考答題架構。考這一科的類科:資訊處理。本頁列出歷年全部題目,參考架構只列開頭的「破題」,完整的答題架構、關鍵字與作答提醒請到站內查看。

▶ 看完整參考架構(程式設計)

112 年(考試時間 120 分鐘) 原卷 PDF

  1. 1

    關於以下C程式碼:01 #include<stdio.h> 02 #define SIZE 10 03 #define THREE 3 04 unsigned int f1(unsigned int a, unsigned int b){ return (a&&!b); } 05 unsigned int f2(unsigned int a, unsigned int b){ return (a<<b); } 06 unsigned int f3(unsigned int a, unsigned int b){ return (a&~b); } 07 int f4(int a, int b) { return a*b+a-b; } 08 int f5(int a, int b) { 09 int data[SIZE]; 10 for (int i=1, k=0; i<a; i++) { 11 if (i%3==0) data[k++]=f4(i, i+1); 12 } 13 return data[b]; 14 } 15 int f6(int a, int b) { 16 int data[][THREE] = {{4,3,2},{3,4,2},{2,3,3}}; 17 for (int i=0; i<THREE; i++) { 18 for (int j=0; j<THREE; j++) { 19 if (i>a || j>b) 20 data[i][j]= data[j][i]+b; 21 } 22 } 23 return data[a][b]; 24 } 25 int main() { 26 printf("%u\n", f1(6, 2)); 27 printf("%u\n", f2(6, 2)); 28 printf("%u\n", f3(7, 2)); 29 printf("%d\n", f4(3, 12)); 30 printf("%d\n", f5(15, 3)); 31 printf("%d\n", f5(3, 15)); 32 printf("%d\n", f6(1, 1)); 33 printf("%d\n", f6(3, 2)); 34 return 0; 35 }請說明程式執行後,程式碼編號26~33的輸出,以及其運算邏輯。(25分)

    (25 分)

    參考架構・破題

    本題評量 C 語言之運算子優先順序、位元與邏輯運算、二維陣列索引、迴圈控制流程及未定義行為。作答核心在於精確追蹤各函式內部之運算步驟,並明確指出邊界條件與陣列越界存取之潛在陷阱。

    完整答題架構與關鍵字:到站內看全文

  2. 2

    關於以下C++程式碼:01 #include <iostream> 02 #include <string> 03 #include <exception> 04 #include <stdexcept> 05 #include <assert.h> 06 using namespace std; 07 class NegativeException: public exception { 08 const char * what () const throw () { return "negative"; } 09 }; 10 class DivideByZeroException: public logic_error{ 11 public: 12 DivideByZeroException() : logic_error( "divide by zero" ) {} 13 }; 14 int getResult(int x, int y) { 15 if (x<0 || y<0) throw NegativeException(); 16 else if (y==0) throw DivideByZeroException(); 17 return (x/y); 18 } 19 void f(int x, int y) { 20 try { cout << "Result:"<< getResult(x, y)<<endl; } 21 catch (std::exception &e) { cout << "1: " << e.what() << "\n"; } 22 } 23 void testResult() { 24 f(2, -1); 25 f(2, 0); 26 f(2, 3); 27 f(6, 3); 28 } 29 void assertResult() { 30 assert(getResult(8, 4)==1); 31 } 32 int main() { 33 testResult(); 34 assertResult(); 35 return 0; 36 }

    (一)請說明程式執行後的輸出。(15分)

    (二)請說明程式中assert與exception的使用時機與目的。(10分)

    (25 分)

    參考架構・破題

    本題檢驗 C++ 物件導向程式設計之例外處理機制與斷言除錯技術。作答應分別剖析 try-catch 區塊攔截自訂與標準例外的執行順序,並深入辨析斷言與例外在軟體生命週期中的角色分工與應用原則。

    完整答題架構與關鍵字:到站內看全文

  3. 3

    關於以下Java程式:01 import java.io.*; 02 import java.util.ArrayList; 03 abstract class Fruit { 04 public Fruit(int sweetness) {this.sweetness = sweetness; } 05 public abstract String eat(); 06 protected String taste() { 07 if (sweetness>0 && sweetness<5) return "no"; 08 else if (sweetness<=10) return "little"; 09 else if (sweetness<=15) return "some"; 10 else if (sweetness<=20) return "more"; 11 else return "super"; 12 } 13 private int sweetness; 14 } 15 class Apple extends Fruit { 16 public Apple(String c, int s) { super(s); this.color = c; } 17 public String eat() { return color +":"+taste()+" sweetness "; } 18 private String color; 19 } 20 class Watermelon extends Fruit { 21 public Watermelon(String v, int s) { super(s); this.volume = v; } 22 public String eat() { return volume +":"+taste()+" sweetness "; } 23 private String volume; 24 } 25 public class Test { 26 public static void test01() { 27 ArrayList<Fruit> fs = new ArrayList<Fruit>(); 28 fs.add(new Apple("red", 18)); 29 fs.add(new Watermelon("big", 20)); 30 fs.add(new Apple("green", 10)); 31 fs.forEach((n) -> System.out.println(n.eat())); 32 } 33 public static void main(String[] args) throws InterruptedException { 34 test01(); 35 } 36 }

    (一)請說明程式執行後其輸出與其運作程式碼行數順序。(12分)

    (二)請依下面表格,說明Fruit的設計功用,包含Fruit類別類型與功用以及方法(method)。(13分)Fruit類別類型與功用 Fruit方法功用 eat功用 taste功用說明

    (25 分)

    本題含圖表或公式,請對照原卷 PDF。

  4. 4

    以下C++程式有部分違反安全程式設計原則,可能具有許多潛在風險。01 #include <iostream> 02 #include <string> 03 #define SIZE 10 04 using namespace std; 05 class Food { 06 public: 07 Food() = default; 08 Food(int c) { cal = c; } 09 int getCal() { return cal; } 10 private: 11 int cal; 12 }; 13 void f1() { 14 Food *f[SIZE]; 15 cout<<f[0]->getCal()<<endl; 16 } 17 void f2(int n) { 18 string *f = NULL; 19 for(int i = 0; i < n; i++) 20 f = new string("ok"); 21 cout<<*f<<endl;; 22 } 23 void f3(int n) { 24 double x = 3, y1 = 5, y2 = 2; 25 for (int i=0; i<n; i++) { 26 x = x/10.0; 27 y1 = y1/10.0; y2 = y2/10.0; 28 } 29 if(x == (y1-y2)) cout<<"X == Y"<<endl; 30 } 31 void f4(char *s1, char *s2) { 32 int len =0; 33 char *s =s1 ; 34 while (*s2!='\0') { 35 *s1=*s2; 36 s1++; s2++; 37 } 38 cout<<s<<endl; 39 } 40 void f5(int n) { 41 int result = 0; 42 int *d = new int[n]; 43 d[0] = d[1] = 1; 44 for(int i = 0; i < n-2; i++) d[i+2] = d[i+1]+d[i]; 45 for(int i = 0; i < n; i++) result = result + d[i]; 46 cout<<result<<endl; 47 } 48 int main() { 49 char s1[]="goodness", s2[]="food"; 50 f1(); 51 f2(2); 52 f3(1); 53 f4(s1, s2); 54 f5(6); 55 return 0; 56 }請說明此程式,執行函式f1()~f5()的輸出,以及函式f1()~f5()可能具有的潛在風險。(25分)

    (25 分)

    參考架構・破題

    本題評量安全程式設計與 C++ 記憶體安全規範。作答重點在於依序推導各函式的執行輸出,並從軟體安全弱點觀點,精準剖析指標未初始化、記憶體洩漏、浮點數精度損失、緩衝區溢位及邊界檢查缺失等潛在漏洞。

    完整答題架構與關鍵字:到站內看全文

111 年(考試時間 120 分鐘) 原卷 PDF

  1. 1

    請問以下三小題 C 程式執行的結果為何?請注意須說明答案是如何產生的,否則不給分。

    (一)(5 分)#include <stdio.h> #include <stdlib.h> int main(void) { int x; float y; for (x=0, y=50; x<25; x+=5, y/=2) printf("x=%d, y=%4.2f\n", x, y); return 0; }

    (二)(5 分)#include <stdio.h> #include <stdlib.h> int a=10, fun(int); int main(void) { int b=6; printf("a=%d, b=%d, fun(a)=%d\n", a, b, fun(a)); return 0; } int fun(int b) { a-=5; b/=2; return(a+b); }

    (三)(10 分)#include <stdio.h> #include <stdlib.h> #define SIZE 10 void fun(int *, int); int main(void) { int x[SIZE] = {1,2,3,4,5,6,7,8,9,10}; fun(x, SIZE); printf("\n"); return 0; } void fun(int *a, int size) { if (size > 0) { fun(a+3, size-3); printf("*(a+%d)=%d\n", SIZE-size, *a); } }

    (20 分)

    參考架構・破題

    本題評量 C 語言迴圈控制、浮點數格式化輸出、全域變數與副作用、函式引數求值順序及遞迴函式堆疊運作。作答關鍵在於逐步列出變數追蹤歷程,並特別點出編譯器未指明行為對引數求值結果的影響。

    完整答題架構與關鍵字:到站內看全文

  2. 2

    資料庫是企業組織或政府部門中用以保存大量資料的機制。下圖為資料表的結構,請回答以下各小題。(每小題 10 分,共 20 分)employee salary_slip id : varchar(10) id : varchar(10) first_name : varchar(30) year : int last_name : varchar(20) month : int amount : int

    (一)請以 SQL 列出某一個人的年度薪資總額,必須顯示 id, first_name, last_name 及總額。

    (二)現在的資料查詢服務常見於網頁系統,而 SQL Injection 是最常見的資料庫攻擊方式。請問下列查詢語句是否會有被攻擊的疑慮?若有的話該如何改善?程式碼可以使用 PHP 或 ASP.NET。SELECT first_name, last_name FROM employee WHERE id=’A001’

    (20 分)

    本題含圖表或公式,請對照原卷 PDF。

  3. 3

    請使用 C, C++, Java 或 Python 程式語言撰寫採用五位數整數來進行資料加密的完整程式。(30 分)撰寫加密函數 encrypt(),此函數可將所傳入的五位數整數進行加密,並將加密後的密碼傳回。加密的規則如下: 先將該五位數整數的每位數值分別以「加 5 後除以 10 之餘數」取代。 再將取代後之數字的第一位數與第五位數互換,第二位數與第四位數互換,第三位數不變後取得一個新整數,此即為加密後的密碼。 (註:第一位數為萬分位、第二位數為千分位,以此類推。)撰寫解密函數 decipher(),此函數可將所傳入的五位數密碼進行解密,並將解密後的整數傳回。解密的規則是將上述加密後之密碼還原為原傳入的整數。撰寫主程式來驗證上述之加密與解密函數的正確性。程式一開始先讓使用者輸入任一之五位數整數[不在合理範圍內(10000~99999)須請使用者重新輸入],在呼叫加密函數後將所傳回之密碼從螢幕上顯示出;再將該密碼傳入解密函數,再將所傳回之整數從螢幕上顯示出。程式須可讓使用者持續輸入五位數之整數,直到使用者輸入 0(整數)時結束程式的執行。以下為程式執行的範例: (備註:斜體加外框線之整數為使用者所輸入,其餘皆為程式執行的輸出。)請輸入一個五位數的整數(輸入 0 結束程式):1234所輸入的數值並不是五位數之整數請重新輸入:12345加密後的密碼為:09876解密後的密碼為:12345請輸入一個五位數的整數(輸入 0 結束程式):67890加密後的密碼為:54321解密後的密碼為:67890請輸入一個五位數的整數(輸入 0 結束程式):0感謝使用此程式,歡迎您對此程式的優化提出建議。

    (30 分)

  4. 4

    抽象(abstract)類別、介面(interface)、覆寫(override)方法、多載(overload)方法是物件導向程式設計實現多型(polymorphism)的方式。下圖是人事系統中人員基本資料維護的類別圖,請說明類別圖中的覆寫方法及多載方法名稱(須說明理由) ,並使用 C++, Java 或 Python 程式語言撰寫Person 類別與 Police 類別的程式碼。Police 類別之 GetPersonalInformation函數必須傳回身分證(id_no) 、姓名(name) 、職稱(rank) ,UpdateRoleTitle函數必須完整實作更新 police_rank。(30 分)Person <<abstract>> Program # id_no : string # name : string Main(string[] args):void + GetPersonalInformation() : string + UpdateRoleTitle() : void Police Staff - police_rank : PoliceRank - staff_position : StaffPosition + Police(id : string, name : string) + Staff(id : string, name : string) + GetPersonalInformation() : string + GetPersonalInformation() : string + UpdateRoleTitle(title : ?) : void + UpdateRoleTitle(title : ?) : void PoliceRank StaffPosition + code : string + code : string + rank : string + title : string + PoliceRank(code : string, rank : string) + StaffPosition(code : string, title : string)

    (30 分)

    本題含圖表或公式,請對照原卷 PDF。

其他等別的「程式設計」

題目來源:考選部考畢試題查詢平臺(政府資訊公開資料);參考架構為本站自撰,僅供準備方向參考,非官方標準答案。最後更新:。