lawpalyer logo

程式設計考古題|歷屆國考試題彙整

橫跨多種國家考試的程式設計歷屆試題(選擇題 + 申論題)

年份:

資訊處理 24 題

關於下列C 程式碼,請說明程式執行後,程式碼編號27~33 的輸出,以 及其運算邏輯。(25 分) 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 #include <stdio.h> #define SIZE 30 typedef enum direction {North, South, East=3, West} dir_t; int f1(int a, int b) { int x = 3.0/a; double y = (a/2)*(b%3) + x; return y; } int f2(dir_t d) { d= (North+East)/2 > d? East: West; return d; } int f3(int a, int b) { if (b==a || b<=1) return a+b; else if (a<=1) return b-a; else return f3(a-b, a-1)+b+a; } int f4(int a, int b) { int data[SIZE]; for (int i=1, k=0; i<a; i++) { if (i%2==0) data[k++]=i; } return data[b]; } unsigned int f5(unsigned int a, unsigned int b) { return (~a&b); } int main() { printf("%d\n", f1(10, 4)); printf("%d\n", f2(South)); printf("%d\n", f3(6, 4)); printf("%u\n", f3(7, 4)); printf("%d\n", f4(20, 5)); printf("%d\n", f4(10, 4)); printf("%u\n", f5(4, 7)); return 0; }
關於以下C程式碼: 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 #include<stdio.h> #define SIZE 10 #define THREE 3 unsigned int f1(unsigned int a, unsigned int b){ return (a&&!b); } unsigned int f2(unsigned int a, unsigned int b){ return (a<<b); } unsigned int f3(unsigned int a, unsigned int b){ return (a&~b); } int f4(int a, int b) { return a*b+a-b; } int f5(int a, int b) { int data[SIZE]; for (int i=1, k=0; i<a; i++) { if (i%3==0) data[k++]=f4(i, i+1); } return data[b]; } int f6(int a, int b) { int data[][THREE] = {{4,3,2},{3,4,2},{2,3,3}}; for (int i=0; i<THREE; i++) { for (int j=0; j<THREE; j++) { if (i>a || j>b) data[i][j]= data[j][i]+b; } } return data[a][b]; } int main() { printf("%u\n", f1(6, 2)); printf("%u\n", f2(6, 2)); printf("%u\n", f3(7, 2)); printf("%d\n", f4(3, 12)); printf("%d\n", f5(15, 3)); printf("%d\n", f5(3, 15)); printf("%d\n", f6(1, 1)); printf("%d\n", f6(3, 2)); return 0; } 請說明程式執行後,程式碼編號26~33的輸出,以及其運算邏輯。(25分)
針對下列C++程式,請標示出Except 類別的f1, …, f6 函式中有問題的 函式,與說明其問題之原因;並請說明若將有問題的函式和程式碼刪除, 其程式執行後之輸出。(25 分) 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 #include <stdexcept> #include <iostream> #include <string> using namespace std; class Except{ public: void f1(int c); void f2(); void f3(); void f4(); void f5(); void f6(); }; int main() { Except e; e.f1(1); e.f2(); e.f3(); e.f4(); e.f5(); e.f6(); return 0; } void Except::f1(int c) { if (c<0) throw out_of_range("large"); cout<<"exc1"<<endl; } void Except::f2() { f1(-1); } 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 60 61 62 63 64 void Except::f3() { try { f1(-1); cout<<"ok"<<endl; }catch(exception &e) { cout<<"exc2"<<endl; } } void Except::f4() { try { throw out_of_range("no"); }catch(out_of_range &e) { cout<<e.what()<<endl; cout<<"exc3"<<endl; } } void Except::f5() { try { throw out_of_range("yes"); }catch(exception &e) { cout<<"exc41"<<endl; }catch(out_of_range &e) { cout<<e.what()<<endl; cout<<"exc42"<<endl; } } void Except::f6() { try { throw out_of_range("ok"); }finally { cout<<"exc6"<<endl;; } }
(1)
關於以下C++程式碼: 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 #include <iostream> #include <string> #include <exception> #include <stdexcept> #include <assert.h> using namespace std; class NegativeException: public exception { const char * what () const throw () { return "negative"; } }; class DivideByZeroException: public logic_error{ public: DivideByZeroException() : logic_error( "divide by zero" ) {} }; int getResult(int x, int y) { if (x<0 || y<0) throw NegativeException(); else if (y==0) throw DivideByZeroException(); return (x/y); } void f(int x, int y) { try { cout << "Result:"<< getResult(x, y)<<endl; } catch (std::exception &e) { cout << "1: " << e.what() << "\n"; } } void testResult() { f(2, -1); f(2, 0); f(2, 3); f(6, 3); } void assertResult() { assert(getResult(8, 4)==1); } int main() { testResult(); assertResult(); return 0; } 請說明程式執行後的輸出。(15分) 請說明程式中assert與exception的使用時機與目的。(10分)
針對下列Java 程式碼,請完成統一塑模語言(UML)類別圖(a)~(e);另 外請標示出錯誤程式碼行數並說明錯誤原因;以及說明若將錯誤行數程 式碼予以註解後,執行其程式的輸出。(25 分) 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 import java.io.*; interface Pet { public abstract int eat(int f); }; class Dog implements Pet { public Dog(int f) {food = f; } public int eat(int f) { food += f; return food; } private int food; }; public class Main{ public static void main(String[] args) { Pet d1 = new Pet(); Pet d2 = new Dog(); Pet d3 = new Dog(5); d1.eat(5); d2.eat(5); System.out.println("dog: "+d3.eat(5)); } }
(5)
(5)
(5)
(5)
關於以下Java程式: 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 import java.io.*; import java.util.ArrayList; abstract class Fruit { public Fruit(int sweetness) {this.sweetness = sweetness; } public abstract String eat(); protected String taste() { if (sweetness>0 && sweetness<5) return "no"; else if (sweetness<=10) return "little"; else if (sweetness<=15) return "some"; else if (sweetness<=20) return "more"; else return "super"; } private int sweetness; } class Apple extends Fruit { public Apple(String c, int s) { super(s); this.color = c; } public String eat() { return color +":"+taste()+" sweetness "; } private String color; } class Watermelon extends Fruit { public Watermelon(String v, int s) { super(s); this.volume = v; } public String eat() { return volume +":"+taste()+" sweetness "; } private String volume; } public class Test { public static void test01() { ArrayList<Fruit> fs = new ArrayList<Fruit>(); fs.add(new Apple("red", 18)); fs.add(new Watermelon("big", 20)); fs.add(new Apple("green", 10)); fs.forEach((n) -> System.out.println(n.eat())); } public static void main(String[] args) throws InterruptedException { test01(); } } 請說明程式執行後其輸出與其運作程式碼行數順序。(12分) 請依下面表格,說明Fruit的設計功用,包含Fruit類別類型與功用以及方 法(method)。(13分) Fruit類別類型與功用 Fruit方法功用 eat功用 taste功用 說明
針對下列Python 程式碼,依序在兩個Terminal 執行server.py 和client.py 後,在client.py 輸入Tom 和quit;請說明client.py 的Terminal 之輸出內 容,並說明Line 03, 04, 05 程式碼的運作邏輯。(25 分) 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 import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('127.0.0.1', 7000)) s.listen(5) print('wait for connection...') while True: conn, addr = s.accept() print('connected by ' + str(addr)) indata = conn.recv(1024) print('recv: ' + indata.decode()) if 'quit' in indata.decode(): outdata = 'bye ' else: outdata = 'hi ' + indata.decode() conn.send(outdata.encode()) conn.close() if 'quit' in indata.decode(): break print('listen...') s.close() #client.py import socket while True: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('127.0.0.1', 7000)) name = input('>name:') print('send: ' + name) s.send(name.encode()) indata = s.recv(1024) s.close() print('>' + indata.decode()) if 'quit' in name: break
(5)
(1024)
(1024)
以下C++程式有部分違反安全程式設計原則,可能具有許多潛在風險。 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 #include <iostream> #include <string> #define SIZE 10 using namespace std; class Food { public: Food() = default; Food(int c) { cal = c; } int getCal() { return cal; } private: int cal; }; void f1() { Food *f[SIZE]; cout<<f[0]->getCal()<<endl; } void f2(int n) { string *f = NULL; for(int i = 0; i < n; i++) f = new string("ok"); cout<<*f<<endl;; } void f3(int n) { double x = 3, y1 = 5, y2 = 2; for (int i=0; i<n; i++) { x = x/10.0; y1 = y1/10.0; y2 = y2/10.0; } if(x == (y1-y2)) cout<<"X == Y"<<endl; } void f4(char *s1, char *s2) { int len =0; char *s =s1 ; while (*s2!='\0') { *s1=*s2; s1++; s2++; } cout<<s<<endl; } void f5(int n) { int result = 0; int *d = new int[n]; d[0] = d[1] = 1; for(int i = 0; i < n-2; i++) d[i+2] = d[i+1]+d[i]; for(int i = 0; i < n; i++) result = result + d[i]; cout<<result<<endl; } int main() { char s1[]="goodness", s2[]="food"; f1(); f2(2); f3(1); f4(s1, s2); f5(6); return 0; } 請說明此程式,執行函式f1()~f5()的輸出,以及函式f1()~f5()可能具有的潛 在風險。(25分)
(2)
(1)
(6) 25 分
有關C 程式設計,回答下列問題: 下列程式執行結果為何?(13 分) 下列程式執行結果為何?(10 分)
請問以下三小題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); } }
有關Python 程式設計,回答下列問題: 以下程式執行時,輸入正數為15 和21 時,其輸出結果分別為何? (14 分) 以下是將x = [-5, 16, 30, -11, 26, -22] 整數串列,利用氣泡排序法,由 小到大逐次排列,並顯示每一次排列的python 主程式, 此程式執行結果如下: 寫出氣泡排序法的func_2_2(x)函數,來完成上述程式。(11 分)
資料庫是企業組織或政府部門中用以保存大量資料的機制。下圖為資料 表的結構,請回答以下各小題。(每小題10 分,共20 分) 請以SQL 列出某一個人的年度薪資總額,必須顯示id, first_name, last_name 及總額。 現在的資料查詢服務常見於網頁系統,而SQLInjection 是最常見的資 料庫攻擊方式。請問下列查詢語句是否會有被攻擊的疑慮?若有的話 該如何改善?程式碼可以使用PHP 或ASP.NET。 SELECT first_name, last_name FROM employee WHERE id=’A001’ employee id : varchar(10) first_name : varchar(30) last_name : varchar(20) salary_slip id : varchar(10) year : int month : int amount : int
(10)
(30)
(20)
(10)
有關C++程式設計,回答以下問題: 建立一個Rect 類別,其Rect.h 定義如下: 其屬性有length 和width,每個屬性內定為1.0。提供計算矩形周長 (perimeter)和面積(area)的成員函數。另外,提供寬度屬性的設定 (setWidth)和獲取(getWidth)函數,也提供長度屬性的設定(setLength) 和獲取(getLength)函數。設定函數要檢查長度和寬度均為大於0.0 且 小於20.0 的浮點數。以下為測試Rect 類別的主程式: 執行結果如下: 寫出Rect.h 中的Rect( ),setWidth( ),setLength( ),getWidth( ),getLength( ), perimeter( ),area( )等C++函數,來完成上述主程式和執行結果的功能。 (25 分)
請使用C, C++, Java 或Python 程式語言撰寫採用五位數整數來進行資料 加密的完整程式。(30 分) 撰寫加密函數encrypt(),此函數可將所傳入的五位數整數進行加密, 並將加密後的密碼傳回。加密的規則如下:  先將該五位數整數的每位數值分別以「加5 後除以10 之餘數」 取代。  再將取代後之數字的第一位數與第五位數互換,第二位數與第四 位數互換,第三位數不變後取得一個新整數,此即為加密後的密 碼。(註:第一位數為萬分位、第二位數為千分位,以此類推。) 撰寫解密函數decipher(),此函數可將所傳入的五位數密碼進行解密, 並將解密後的整數傳回。解密的規則是將上述加密後之密碼還原為原 傳入的整數。 撰寫主程式來驗證上述之加密與解密函數的正確性。程式一開始先讓 使用者輸入任一之五位數整數[不在合理範圍內(10000~99999)須請 使用者重新輸入],在呼叫加密函數後將所傳回之密碼從螢幕上顯示 出;再將該密碼傳入解密函數,再將所傳回之整數從螢幕上顯示出。 程式須可讓使用者持續輸入五位數之整數,直到使用者輸入0(整數) 時結束程式的執行。 以下為程式執行的範例:(備註:斜體加外框線之整數為使用者所輸入, 其餘皆為程式執行的輸出。) 請輸入一個五位數的整數(輸入0 結束程式):1234 所輸入的數值並不是五位數之整數 請重新輸入:12345 加密後的密碼為:09876 解密後的密碼為:12345 請輸入一個五位數的整數(輸入0 結束程式):67890 加密後的密碼為:54321 解密後的密碼為:67890 請輸入一個五位數的整數(輸入0 結束程式):0 感謝使用此程式,歡迎您對此程式的優化提出建議。
有關Java 程式設計,回答下列問題: 寫一找尋小於10,000 的完美數(Perfect number)程式,所謂完美數是 指:如果一個正整數等於它所有正因數的和,但不包括它自己,則此 正整數被稱為完美數。例如,6 是第一個完美數,因為6 = 3 + 2 + 1。接 下來是28 = 14 + 7 + 4 + 2 + 1。(11 分) 以下程式功能為何?當輸入32,56,80 時,其輸出結果分別為多少? (16 分)
抽象(abstract)類別、介面(interface)、覆寫(override)方法、多載(overload) 方法是物件導向程式設計實現多型(polymorphism)的方式。下圖是人 事系統中人員基本資料維護的類別圖,請說明類別圖中的覆寫方法及多 載方法名稱(須說明理由),並使用C++, Java 或Python 程式語言撰寫 Person 類別與Police 類別的程式碼。Police 類別之GetPersonalInformation 函數必須傳回身分證(id_no)、姓名(name)、職稱(rank),UpdateRoleTitle 函數必須完整實作更新police_rank。(30 分) Program Main(string[] args):void Person <<abstract>> + GetPersonalInformation() : string + UpdateRoleTitle() : void Police - police_rank : PoliceRank + Police(id : string, name : string) + GetPersonalInformation() : string + UpdateRoleTitle(title : ?) : void Staff - staff_position : StaffPosition + Staff(id : string, name : string) + GetPersonalInformation() : string + UpdateRoleTitle(title : ?) : void PoliceRank StaffPosition + code : string + rank : string + PoliceRank(code : string, rank : string) + code : string + title : string + StaffPosition(code : string, title : string)
請問下列程式碼之輸出結果為何?(25 分) #include <stdio.h> #include <stdlib.h> int function(int n) { if (n < 10) return n; int sum = 0; while (n > 0) { sum += n % 10; n /= 10; } return function(sum); } int main() { int n = 12345; int output; output = function(n); printf("output%d\n", output); return 0; }
假設一堆疊(Stack)的推入(Push)順序為:123、234、345、456、567, 並且途中可以隨意彈出(Pop)取值,則下列彈出(Pop)取值之順序有 無可能出現? 345、567、456、234、123 若有可能,請依序將推入(Push)與彈出(Pop)的步驟列出。若無可能, 請解釋原因為何?(25 分)
請問下列程式碼之輸出結果以及該程式碼的目的為何?(25 分) #include "stdio.h" int f(int a, int b) { if(a%b == 0) return b; return f(b,a%b); } int main(void) { printf("f(21,9) = %d\n",f(21,9)); printf("f(6,44) = %d\n",f(6,44)); return 0; }
請問下列程式碼之輸出結果為何?(25 分) #include <stdio.h> #include <stdlib.h> void function(int a, int b) { printf("a=%d,b=%d\n", a, b); for (i = 0; i < 8; i++) { if (!(a > 10 && b < 10) && (a <= 10 || b >= 10)) { a = a + 1; b = b - 1; printf("a=%d,b=%d\n", a, b); } } } int main() { int x = 5; int y = 10; function(x, y); return 0; }
請使用Java、C#、C++或Python 等物件導向程式語言,建立矩陣相關 運算的系統,系統一共有三個類別,分別為抽象類別Matrix,實作類別 MatrixMultiply 及MatrixAdd,其中:(30分) *抽象類別Matrix 中所有的變數跟方法皆為protected,主要包含了: .matrix1, matrix2, resultM 三個實數值二維matrices 資料 .一個抽象的checkDimension( )方法,用來檢查matrix1跟matrix2是 否適合做所指定的運算 .一個抽象的matrixOperation()方法,用來對matrix1跟matrix2進行運算 .一個setMatrix()方法,可以將所傳入的兩個matrices 複製到matrix1, matrix2 .改寫物件的toString()方法,可以顯示matrix1, matrix2及resultM 的 內容 *類別MatrixMultiply,繼承類別Matrix: .實作Matrix 中的兩個抽象方法,分別可以實現父類別內matrix1及 matrix2進行乘法時的維度檢查及乘法運算,進行運算前會主動呼叫 checkDimension(),確定是否可以進行相關運算,如無法運算, resultM 會被設定為null 並回傳,否則進行運算,結果存於父類別的 resultM 並回傳 .一個空建構子及一個可以接受兩個二維matrices 的建構子,建構子 中呼叫父類別的setMatrix()方法,設定matrix1及matrix2(空建構 子會將matrix1及matrix2設為null) *類別MatrixAdd,同MatrixMultiply,僅實作抽象方法時,是以matrices 加法為對象,進行維度的檢查及運算
請使用Java、C、C++、C#或Python,分別使用iterative 跟recursive 方 法,撰寫二元搜尋法,搜尋已排序的整數值數列。(20分) 註:假設數列資料是以具有array 性質的list 來存放 *模組程式應能接受欲搜尋的資料及已排序數列的相關資料 *模組程式應回傳所欲搜尋的資料是否在數列資料中
請使用Java、C、C++、C#或Python 撰寫相關程式模組,使用stack(先 進後出的線性資料結構)來完成preorder 的深度優先(Depth First Search) 樹狀圖追蹤(traversal)。(25分) 註:假設樹狀圖的節點資料可以為任意型別 註:假設樹狀圖的節點結構內含三個全域變數:資料、父節點、所有子 節點串接的linked list *模組程式應能接受樹狀圖的樹根 *模組程式應以字串數列方式,回傳樹狀圖追蹤的結果(以空白、逗號 或換行符號區隔資料字串) *樹狀圖中的節點需另以獨立的class 定義節點資料、相關的建構子 (Constructor)與存取子(Accessor/Mutator)
請使用Java、C、C++、C#或Python 撰寫相關模組程式,可以找出某一 整數的所有generator(s)。(25分) digit-sum:正整數N 的digit-sum 定義為「該正整數本身跟構成該整數個 別數字的總和」,如245的digit-sum 為256,因為245+2+4+5等於256 generator:正整數M 是正整數N 的digit-sum 時,則N 為M 的generator 如245是256的generator 如1979是2005的generator,因為1979+1+9+7+9等於2005 如198及207皆是216的generator,而198是216最小的generator *模組程式應能接受使用者由標準輸入裝置所輸入的資料 *模組程式中應控制使用者輸入資料的正確性與合法性,如輸入資料非 正整數,程式應顯示輸入資料不正確的訊息,然後結束程式 *輸入資料無誤,執行程式後,應顯示該輸入資料的所有generator(s)