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

pdf
81.41 KB
4 頁
moex
侵權投訴
加載中. ..
PDF
11
4
年公務人員特種考試警察人員、一般警察人員、
國家安全局國家安全情報人員、移民行政人員考試及
114年特種考試退除役軍人轉任公務人員考試試題
考 試 別
:
一般警察人員考試
等 別
:
三等考試
類科組別
:
警察資訊管理人員
科 目
:
物件導向程式設計
考試時間
:
2小時 座號:
※注意:禁止使用電子計算器。
不必抄題,作答時請將試題題號及答案依照順序寫在試卷上,於本試題上作答者,不予計分。
本科目除專門名詞或數理公式外,應使用本國文字作答。
代號:
30420
頁次:
4
-
1
一、針對下列 C++程式碼,請說明其輸出與其程式 p.print()、q.split()(Line 28,
29)執行之運作流程邏輯;並請說明在 testPattern()中測試 Pattern 類別
split()(Line 27, 29),對於「指令與條件判斷涵蓋度」不足之處。(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
#include
#include
using namespace std;
class Pattern {
public:
Pattern(string data) { this->data = data; }
void split(string symbol) {
int start = 0, stop = 0;
stop = data.find(symbol);
while (stop != string::npos) {
if (stop - start != 0) words.push_back(data.substr(start, stop - start));
start = stop + symbol.size();
stop = data.find(symbol, start);
}
if (start != length) words.push_back(data.substr(start));
}
void print() { for (auto& s : words) cout << s << "#"; cout< }
private:
string data;
vector > words;
int length;
};
void testPattern() {
Pattern p("this is a test");
Pattern q("that was so happle");
p.split(" ");
p.print();
q.split("ha");
q.print();
}
int main() {
testPattern();
return 0;
}
代號:
30420
頁次:
4
-
2
二、下列 Java 程式碼計算食物(Food)料理(cook)後,動物(Animal)擷
取之卡路里量。請完成統一塑模語言 UML 圖;並說明其執行後的輸出,
以及多型(Polymorphism)、封裝(Encapsulation)發生所在之程式碼行數。
(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
41
42
43
44
45
46
47
48
49
abstract class Food {
public Food(int cal, String type) {
this.calorie = cal;
this.type = type;
}
abstract public void cook(int c);
public String show(int c) {
String result = type+":";
cook(c);
result += calorie;
return result;
}
protected int calorie; // 卡路里
private String type;
}
class Bread extends Food {
public Bread(int c, int a) {
super(c, "Bread");
additives = a;
}
public void cook(int cal) { calorie += additives*cal; }
private int additives;
}
class Beef extends Food {
public Beef(int c) {
super(c, "Beef");
condiments = c*2;
}
public void cook(int cal) { calorie += condiments%cal; }
private int condiments;
}
class Anamial {
public Anamial(Food f) {food = f;}
public String getCal() { return food.show(2); }
private Food food;
}
public class FoodTest {
public static void main(String[] args) {
Food f1 = new Bread(3, 2);
Food f2 = new Bread(2, 3);
Food f3 = new Beef(3);
Anamial a1 = new Anamial(f1);
Anamial a2 = new Anamial(f2);
Anamial a3 = new Anamial(f3);
System.out.println(a1.getCal());
System.out.println(a2.getCal());
System.out.println(a3.getCal());
}
}
_____
Food
_____
_____
Bread
additives
Beef
condiments
Anamial
_____
代號:
30420
頁次:
4
-
3
三、針對下列 C++程式碼,請修正程式碼行數 58「Job jobs ...」的錯誤及修
正錯誤後正確執行之輸出;並請說明 Employee 類別 candidate 資料的運作
原理,以及程式碼行數 61「e1.arrange()」程式執行之運作流程。(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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include
#include
#include
using namespace std;
class Job {
public:
Job(string _name, int _sa, int _sk) {
name = _name;
salary = _sa;
skill = _sk;
employee_name = "None";
}
string getName() { return name; }
int getSalary() { return salary; }
int getSkill() { return skill; }
void hire(string _name) { employee_name = _name; }
void print() { cout << name <<"::"< }
private:
string name;
int salary;
int skill;
string employee_name;
};
class Employee {
public:
Employee(string _name, int _sa, int _sk) {
name = _name;
salary = _sa;
skill = _sk;
}
void match(Job *m) {
if ((m->getSalary() >= salary) && (skill >= m->getSkill())) {
candidate[m->getName()] = m;
}
}
void arrange() {
int score = 0;
Job *jb, *jb_wanted;
for (auto c = candidate.begin(); c != candidate.end(); ++c) {
jb = c->second; // get value of MAP
if (jb->getSalary() >= score) {
score = jb->getSalary();
jb_wanted = jb;
}
}
jb_wanted->hire(name);
}
string getName() { return name; }
private:
string name;
int salary;
int skill;
map Job *> candidate;
};
int main() {
Employee e1("Tom", 37, 85);
Employee e2("John", 35, 75);
Job jobs[] ={ new Job("RD", 45, 75), new Job("Sales", 35, 70),
new Job("Manager", 55, 80)};
for (int i=0; i<3; i++) e1.match(jobs[i]);
代號:
30420
頁次:
4
-
4
61
62
63
64
65
e1.arrange();
for (int i=0; i<3; i++) e2.match(jobs[i]);
e2.arrange();
for (int i=0; i<3; i++) jobs[i]->print();
return 0;
}
四、針對下列 C++程式碼,請說明其輸出與其 inpuScore()、computeAverage()
執行之運作流程邏輯;並請說明 computeAverage()設計上的問題。(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
41
42
43
44
45
46
47
48
49
50
51
#include
#include
#include
using namespace std;
class Student{
public:
Student(string n) { name = n; math_score = eng_score =average =0; }
void checkSmall(int s) {
if (s<0) throw out_of_range("small");
cout<<"ok,";
};
void inpuScore(int m_s, int e_s) {
try {checkSmall(m_s); checkSmall(e_s);
math_score = m_s;
eng_score = e_s;
}
catch(exception &e) { cout< }
}
void computeAverage(int n) {
try {if (n<=0) throw out_of_range("zero");
average = (math_score+eng_score)/n;
}
catch(exception &e) { cout<<"exc"< }
catch(out_of_range &e) { cout< }
}
void print() { cout << name <<":"< }
private:
int math_score; // 數學成績
int eng_score; // 英文成績
int average; // 平均成績
string name;
};
void test01() {
Student stu("John");
stu.inpuScore(0, 0);
stu.inpuScore(100, -1);
stu.inpuScore(100, 90);
stu.print();
}
void test02() {
Student stu("Tom");
stu.inpuScore(100, 90);
stu.computeAverage(0);
stu.print();
}
int main() {
test01();
test02();
return 0;
}
收藏 ⬇️ 下載