資訊處理 112 年程式設計概要考古題(共 5 題) 資料來源:考選部歷屆試題|法律人 LawPlayer 整理 https://lawplayer.com/exam/information-processing/112-%E7%A8%8B%E5%BC%8F%E8%A8%AD%E8%A8%88%E6%A6%82%E8%A6%81 第 1 題 試回答下列有關程式設計領域的問題:(每小題5 分,共20 分) ㈠撰寫程式時皆要取變數名稱(variable name)與其資料型態(data type), 試問這兩項的用意為何? ㈡程式敘述有時會以陣列(array)或串列(list)來表示,試問其功用為何? ㈢物件導向程式設計中的多型(polymorphism)的功用為何? ㈣何謂多載函式(overloading function)? 第 2 題 下列是C 程式語言所撰寫的兩數對調,它使用傳址呼叫來完成,試修正下 列程式錯誤的地方。程式在main()函式中先輸入兩個整數,並印出對調前 的兩數,再呼叫swap()對調函式,最後印出對調後的兩數。輸出結果如下: (24 分) Enter two numbers:100 200 a = 100, b = 200 a = 200, b = 100 /* 兩數對調程式 */ #include void swap(int , int); int main() { int a, b; printf("enter two numbers:"); scanf("%d %d", a, b); printf("a = %d, b = %d\n", a, b); swap(a, b); printf("a = %d, b = %d\n", a, b); return 0; } void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } 第 3 題 下列是以Python 程式語言撰寫的片段程式,試回答每一小題的輸出結 果。每一小題是獨立運作的。注意,若迴圈無法停止,則以無窮迴圈作 答。(每小題3 分,共30 分) ㈠total = 0 for i in range(1, 100): total += i print('total =', total) ㈡total = 0 i = 1 while i<=100: total += i i += 1 print('total =', total) ㈢total = 0 i = 1 while i<=100: i += 1 total += i print('total =', total) ㈣total = 0 for i in range(2, 100, 2): total += i print('total =', total) ㈤total = 0 i = 1 while i<=100: total += 1 i += 1 print('total =', total) ㈥total = 0 i = 1 while i<=100: total += i i += 2 print('total =', total) ㈦total = 0 for i in range(100, 1, -1): total += i print('total =', total) ㈧total = 0 i = 100 while i>0: total += i i -= 1 print('total =', total) 下列的程式,若依序輸入的數值是1、2、3、4、5、6 total = 0 i = 1 while i <= 5: num = eval(input('Enter a number: ')) if num % 5 == 0: break else: total += num i += 1 print('total =', total) 下列的程式,若依序輸入的數值是1、2、3、4、5、6 total = 0 i = 1 while i <= 5: num = eval(input('Enter a number: ')) if num % 5 == 0: continue else: total += num i += 1 print('total =', total) 第 4 題 利用Python、C、C++、Java、C#五種程式語言的任一種程式語言,撰寫 一程式用以模擬大樂透的電腦選號(6 個介於1 到49 之間不同的號碼)。 輸出時要由小至大排列。(26 分) 第 8 題 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include void g(int *x, int *y) { *x = *y - 1; *y = *y - 2; printf("*x = %d, *y = %d\n", *x, *y); } void f(int *z, int *w) { int n = 13; g(w, &n); printf("*z = %d, *w = %d, n = %d\n", *z, *w, n); } int main() { int i = 21; int j = 5; int n = 137; f(&i, &j); printf("print 1: i = %d, j = %d, n = %d\n", i, j, n); g(&n, &n); printf("print 2: i = %d, j = %d, n = %d\n", i, j, n); return 0; } ㈠請說明程式碼第8行至第12行中,每一行之運算子符號「*」及「&」操 作所代表的意義。(10分) ㈡請問該程式碼執行後輸出的結果為何?(15分) 二、根據以下C程式碼,回答下列問題: 1 2 3 4 5 6 7 8 9 10 11 12 #include int f(int a, int b){ if (b==1) return a; else { int value = a + f(a, b-1); return value; } } int main() { printf("%d\n", f(5, 3)); printf("%d\n", f(0, 0)); } ㈠請問那一行是此遞迴程式的中止條件?(5分) ㈡請說明第10行的輸出,以及其運作邏輯。(6分) ㈢請說明第11行的輸出,以及其運作邏輯。(7分) ㈣請說明函式f的主要功能,包含輸入參數與輸出結果的關係。(7分) 三、關於搜尋法程式: ㈠說明循序(Sequential)搜尋法,以及二元(Binary)搜尋法的優缺點。 (8分) ㈡以下二元搜尋程式碼有部分錯誤,若要修正為正確程式,請說明「最少」 需修改程式碼行數、原因與修改方法。(17分) 01 public class BinarySearch{ 02 public int faultyBinarySearch(int[] arr, int x){ 03 int l=0, r=arr.length-1; 04 int m=(l+r)/2; 05 while(l<=r){ 06 m=(l+r)/2; 07 if(arr[m]==x) return m; 08 if(arr[m]>x) l=m+1; 09 if(arr[m]