112年 一般警察特考 三等 警察資訊管理人員 物件導向程式設計 試卷

pdf
103.25 KB
4 頁
windows10
侵權投訴
加載中. ..
PDF
112
年公務人員特種考試警察人員、一般警察人員、國家安全局國家安全情報
人員考試及
112
年特種考試交通事業鐵路人員、退除役軍人轉任公務人員考試試題
考 試 別
:
一般警察人員考試
等 別
:
三等考試
類科組別
:
警察資訊管理人員
科 目
:
物件導向程式設計
考試時間
:
2小時 座號:
※注意:
禁止使用電子計算器。
不必抄題,作答時請將試題題號及答案依照順序寫在試卷上,於本試題上作答者,不予計分。
本科目除專門名詞或數理公式外,應使用本國文字作答。
代號:
30450
頁次:
4
-
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
#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)
代號:
30450
頁次:
4
-
2
針對以下 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
(b)
(c)
(d)
(f)
代號:
30450
頁次:
4
-
3
以下 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]=''; values[i]=0; }
}
int &operator[] (char c) {
for (int i=0; ie; i++) if (keys[i]==c) return values[i];
keys[mSize]=c;
return values[mSize++];
}
~IMap() { delete keys; delete values; }
private:
char *keys;
int *values;
int mSize;
};
void test01() {
IMap x(10);
x['a'] = 5;
cout<
cout<
}
int countChar(string s, char c) {
IMap x(20);
for (int i=0; i
x[s[i]] = (a) ;
return (b) ;
}
void test02() { cout< 'b')< }
int main() {
test01();
test02();
return 0;
}
請說明執行 test01()的輸出;以及說明 IMap 類別 int&operator[] (char c)
函式的設計邏輯。(15 分)
函式 countChar(s, c)是利用 IMap 類別的 Map 功能,統計在字串 s中,
字元 c出現次數;請完成此函式(a), (b)使之能正常執行其功能。(10 分)
代號:
30450
頁次:
4
-
4
以下 Java 程式模擬銀行帳戶存款與查詢餘額的多執行緒功能。Account
是帳戶類別,可以存款與查詢餘額;ATM 類別可以操作帳戶的存款功能;
Test 類別輸出查詢之最後帳戶餘額。
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
import java.util.ArrayList;
class Account {
public void deposit(int m) { balance = balance + m; }
public int getBalance() { return balance; }
private int balance=0;
}
class ATM extends Thread {
public ATM(Account a, int m) {account = a; money = m;}
public void run() {
try {Thread.sleep(100);
} catch (InterruptedException e) { e.printStackTrace(); }
synchronized(Account.class) { account.deposit(money); }
//account.deposit(money);
}
private Account account;
private int money;
}
public class Test {
public static void main(String[] args) throws InterruptedException {
ArrayList ts = new ArrayList<>();
Account account = new Account();
for(int i = 1 ; i <= 10 ; i++) {
Thread atm = new ATM(account, i);
atm.start();
ts.add(atm);
}
for(Thread t : ts) { t.join(); }
System.out.println("" + account.getBalance());
}
}
請說明此程式的執行緒之特性以及此程式輸出。(10 分)
將Line28 註解掉後,請說明程式執行結果與其運作原因。再將 Line13
註解掉,並打開 Line14,28 註解後,請說明此時程式執行結果與其運作
原因。(15 分)
收藏 ⬇️ 下載