
112 年特種考試地方政府公務人員考試試題
※注意:禁止使用電子計算器。
不必抄題,作答時請將試題題號及答案依照順序寫在試卷上,於本試題上作答者,不予計分。
本科目除專門名詞或數理公式外,應使用本國文字作答。
代號:
頁次:
-
一、關於下列 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
#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];
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("%dn", f1(10, 4));
printf("%dn", f2(South));
printf("%dn", f3(6, 4));
printf("%un", f3(7, 4));
printf("%dn", f4(20, 5));
printf("%dn", f4(10, 4));
printf("%un", f5(4, 7));
return 0;
}

代號:
頁次:
-
二、針對下列 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
#include
#include
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"<
}
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"<
}catch(exception &e) {
cout<<"exc2"<
}
}
void Except::f4() {
try {throw out_of_range("no");
}catch(out_of_range &e) {
cout<
cout<<"exc3"<
}
}
void Except::f5() {
try {throw out_of_range("yes");
}catch(exception &e) {
cout<<"exc41"<
}catch(out_of_range &e) {
cout<
cout<<"exc42"<
}
}
void Except::f6() {
try {throw out_of_range("ok");
}finally {

代號:
頁次:
-
三、針對下列 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));
}
}

代號:
頁次:
-
四、針對下列 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
# server.py
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