112年 高普考 高考三級 資訊處理 程式設計 試卷

pdf
102.98 KB
4 頁
windows10
侵權投訴
加載中. ..
PDF
112年公務人員高等考試三級考試試題
資訊處理
程式設計
考試時間
2
座號
禁止使用電子計算器。
本科目除專門名詞或數理公式外,應使用本國文字作答。
代號
37640
頁次
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
30
31
32
33
34
35
#include<stdio.h>
#define SIZE 10
#define THREE 3
unsigned int f1(unsigned int a, unsigned int b){ return (a&&!b); }
unsigned int f2(unsigned int a, unsigned int b){ return (a<<b); }
unsigned int f3(unsigned int a, unsigned int b){ return (a&~b); }
int f4(int a, int b) { return a*b+a-b; }
int f5(int a, int b) {
int data[SIZE];
for (int i=1, k=0; i<a; i++) {
if (i%3==0) data[k++]=f4(i, i+1);
}
return data[b];
}
int f6(int a, int b) {
int data[][THREE] = {{4,3,2},{3,4,2},{2,3,3}};
for (int i=0; i<THREE; i++) {
for (int j=0; j<THREE; j++) {
if (i>a || j>b)
data[i][j]= data[j][i]+b;
}
}
return data[a][b];
}
int main() {
printf("%un", f1(6, 2));
printf("%un", f2(6, 2));
printf("%un", f3(7, 2));
printf("%dn", f4(3, 12));
printf("%dn", f5(15, 3));
printf("%dn", f5(3, 15));
printf("%dn", f6(1, 1));
printf("%dn", f6(3, 2));
return 0;
}
請說明程式執行後,程式碼編26~33的輸出,以及其運算邏輯。25分)
代號
37640
頁次
4
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
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
#include <assert.h>
using namespace std;
class NegativeException: public exception {
const char * what () const throw () { return "negative"; }
};
class DivideByZeroException: public logic_error{
public:
DivideByZeroException() : logic_error( "divide by zero" ) {}
};
int getResult(int x, int y) {
if (x<0 || y<0) throw NegativeException();
else if (y==0) throw DivideByZeroException();
return (x/y);
}
void f(int x, int y) {
try { cout << "Result:"<< getResult(x, y)<<endl; }
catch (std::exception &e) { cout << "1: " << e.what() << "n"; }
}
void testResult() {
f(2, -1);
f(2, 0);
f(2, 3);
f(6, 3);
}
void assertResult() {
assert(getResult(8, 4)==1);
}
int main() {
testResult();
assertResult();
return 0;
}
請說明程式執行後的輸出。15分)
請說明程式中assertexception的使用時機與目的。10分)
代號:
37640
頁次:
4
3
關於以下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
29
30
31
32
33
34
35
36
import java.io.*;
import java.util.ArrayList;
abstract class Fruit {
public Fruit(int sweetness) {this.sweetness = sweetness; }
public abstract String eat();
protected String taste() {
if (sweetness>0 && sweetness<5) return "no";
else if (sweetness<=10) return "little";
else if (sweetness<=15) return "some";
else if (sweetness<=20) return "more";
else return "super";
}
private int sweetness;
}
class Apple extends Fruit {
public Apple(String c, int s) { super(s); this.color = c; }
public String eat() { return color +":"+taste()+" sweetness "; }
private String color;
}
class Watermelon extends Fruit {
public Watermelon(String v, int s) { super(s); this.volume = v; }
public String eat() { return volume +":"+taste()+" sweetness "; }
private String volume;
}
public class Test {
public static void test01() {
ArrayList<Fruit> fs = new ArrayList<Fruit>();
fs.add(new Apple("red", 18));
fs.add(new Watermelon("big", 20));
fs.add(new Apple("green", 10));
fs.forEach((n) -> System.out.println(n.eat()));
}
public static void main(String[] args) throws InterruptedException {
test01();
}
}
請說明程式執行後其輸出與其運作程式碼行數順序。12分)
請依下面表格說明Fruit的設計功用包含Fruit類別類型與功用以及方
法(method13分)
Fruit類別類型與功用 Fruit方法功用 eat功用 taste功用
說明
代號
37640
頁次
4
4
以下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
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
#include <iostream>
#include <string>
#define SIZE 10
using namespace std;
class Food {
public:
Food() = default;
Food(int c) { cal = c; }
int getCal() { return cal; }
private:
int cal;
};
void f1() {
Food *f[SIZE];
cout<<f[0]->getCal()<<endl;
}
void f2(int n) {
string *f = NULL;
for(int i = 0; i < n; i++)
f = new string("ok");
cout<<*f<<endl;;
}
void f3(int n) {
double x = 3, y1 = 5, y2 = 2;
for (int i=0; i<n; i++) {
x = x/10.0;
y1 = y1/10.0; y2 = y2/10.0;
}
if(x == (y1-y2)) cout<<"X == Y"<<endl;
}
void f4(char *s1, char *s2) {
int len =0;
char *s =s1 ;
while (*s2!=&apos;&apos;) {
*s1=*s2;
s1++; s2++;
}
cout<<s<<endl;
}
void f5(int n) {
int result = 0;
int *d = new int[n];
d[0] = d[1] = 1;
for(int i = 0; i < n-2; i++) d[i+2] = d[i+1]+d[i];
for(int i = 0; i < n; i++) result = result + d[i];
cout<<result<<endl;
}
int main() {
char s1[]="goodness", s2[]="food";
f1();
f2(2);
f3(1);
f4(s1, s2);
f5(6);
return 0;
}
請說明此程式執行函式f1()~f5()輸出以及函式f1()~f5()能具有的潛
在風險。25分)
收藏 ⬇️ 下載