
112年公務人員普通考試試題
※注意:禁止使用電子計算器。
不必抄題,作答時請將試題題號及答案依照順序寫在試卷上,於本試題上作答者,不予計分。
本科目除專門名詞或數理公式外,應使用本國文字作答。
代號:
頁次:
-
一、根據以下C程式碼,回答下列問題:
1
2
3
4
5
6
7
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 = %dn", *x, *y);
}
void f(int *z, int *w) {
int n = 13;
g(w, &n);
printf("*z = %d, *w = %d, n = %dn", *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 = %dn", i, j, n);
g(&n, &n);
printf("print 2: i = %d, j = %d, n = %dn", i, j, n);
return 0;
}
請說明程式碼第8行至第12行中,每一行之運算子符號「*」及「&」操
作所代表的意義。(10分)
請問該程式碼執行後輸出的結果為何?(15分)

代號:
頁次:
-
二、根據以下C程式碼,回答下列問題:
1
2
3
4
5
6
7
8
9
10
11
#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("%dn", f(5, 3));
printf("%dn", 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] r=m-1;
10 }
11 return -1;
12 }
13 }

代號:
頁次:
-
四、請依據以下Java程式碼回答下列問題:
01 import java.util.Scanner;
02 public class MyJavaCode {
03 public static void main(String[] args) {
04 Scanner in = new Scanner(System.in);
05 int a = in.nextInt();
06 Animal dog = new Dog();
07 dog.run(a);
08 dog.run();
09 }
10 }
11
12 class Dog extends Animal {
13 public void run(int units){
14 System.out.println("Dog's running speed is " + units);
15 }
16 public void run(){
17 System.out.println("Dog's default running speed is 20");
18 }
19 }
請說明Java中class、abstract class與interface之間的主要差異?(10分)
請說明上述程式碼那幾行會產生編譯錯誤,以及錯誤的原因。(5分)
不改變原1-19行程式碼順序的條件下,請修正問題之編譯問題,使之
可以正常執行。修正不包含左右大括號「{}」,新增或修改之指令行數
不可以超過3行。(10分)