
年公務人員特種考試警察人員、一般警察人員、國家安全局國家安全情報
人員考試及
112
年特種考試交通事業鐵路人員、退除役軍人轉任公務人員考試試題
考 試 別
一般警察人員考試
等 別
三等考試
類科組別
警察資訊管理人員
科 目
物件導向程式設計
考試時間
2小時 座號:
※注意:
禁止使用電子計算器。
不必抄題,作答時請將試題題號及答案依照順序寫在試卷上,於本試題上作答者,不予計分。
本科目除專門名詞或數理公式外,應使用本國文字作答。
代號:
頁次:
-
針對以下 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
#include
#include
using namespace std;
class Pet {
public:
virtual int eat(int f) = 0;
private:
string name;
};
class Dog: public Pet {
public:
Dog() {food =0; }
int eat(int f) {
food += f;
cout<
return food;
}
private:
int food;
};
int main() {
Pet *d1 = new Pet();
Pet *d2 = new Dog();
Pet *d3 = new Dog();
d2.eat(5);
cout<>eat(5)<<endl;
cout<>eat(5)<<endl;
return 0;
}
請完成統一塑模語言(UML)類別圖(a)~(e)。(10 分)
請標示出錯誤行數程式碼,並說明錯誤原因。若將錯誤碼行數註解,
程式輸出為何?(15 分)
{ (a) }
Pet
- name: string
(b)
Dog
(d)
(e)
(c)

代號:
頁次:
-
針對以下 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
interface Moving { public int go(int s); }
class Fly implements Moving { public int go(int s) { return s*5; } }
class Walk implements Moving { public int go(int s) { return s*2; } }
class Pet {
public Pet(Moving m, String mObj) {
moving = m;
movingObject = mObj;
distance = 0;
System.out.println(""+moving.go(5));
}
public void go(int d) { distance += moving.go(d); }
public String toString() { return (movingObject + " " + distance);}
private Moving moving;
private int distance;
private String movingObject;
};
class Bird extends Pet { public Bird(Moving m) { super(m, "wing"); } }
class Cat extends Pet { public Cat(Moving m) { super(m, "leg"); } }
public class Test {
public static void run(Pet p) { p.go(5); System.out.println(p); }
public static void main(String[] args) {
Moving m1 = new Fly();
Moving m2 = new Walk();
Pet p1 = new Bird(m1);
Pet p2 = new Cat(m2);
run(p1);
run(p2);
}
}
請完成統一塑模語言(UML)類別圖(a)~(f)。(10 分)
此程式輸出為何?並請說明 Line 20 程式碼的意義與運作流程,包含
參數 p以及 p.go()和System.out.println(p)的說明。(15 分)
Pet
CatBird
(a)
Moving
WalkFly

代號:
頁次:
-
以下 C++程式實現部分 Map 的功能,keys 存字元,values 存該字元對應
之整數值。
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
#include
#include
using namespace std;
class IMap{
public:
IMap(int s) {
mSize = 0;
keys = new char[s];
values = new int[s];
for (int i=0; i i++) { keys[i]='