112年 地方特考 三等 資訊處理 程式設計 試卷

pdf
96.42 KB
4 頁
windows10
侵權投訴
加載中. ..
PDF
112
三等考試
訊處理
式設計
考試時間
2
小時
座號
禁止使用電子計算器。
本科目除專門名詞或數理公式外,應使用本國文字作答。
代號
34660
頁次
4
1
一、關於下 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 <stdio.h>
#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];
for (int i=1, k=0; i<a; i++) {
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;
}
代號
34660
頁次
4
2
二、針對下列 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 <stdexcept>
#include <iostream>
#include <string>
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"<<endl;
}
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"<<endl;
}catch(exception &e) {
cout<<"exc2"<<endl;
}
}
void Except::f4() {
try {throw out_of_range("no");
}catch(out_of_range &e) {
cout<<e.what()<<endl;
cout<<"exc3"<<endl;
}
}
void Except::f5() {
try {throw out_of_range("yes");
}catch(exception &e) {
cout<<"exc41"<<endl;
}catch(out_of_range &e) {
cout<<e.what()<<endl;
cout<<"exc42"<<endl;
}
}
void Except::f6() {
try {throw out_of_range("ok");
}finally {
cout<<"exc6"<<endl;;
}
}
代號
346
6
0
頁次
4
3
三、針對下 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));
}
}
代號
34660
頁次
4
4
四、針對下 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((&apos;127.0.0.1&apos;, 7000))
s.listen(5)
print(&apos;wait for connection...&apos;)
while True:
conn, addr = s.accept()
print(&apos;connected by &apos; + str(addr))
indata = conn.recv(1024)
print(&apos;recv: &apos; + indata.decode())
if &apos;quit&apos; in indata.decode():
outdata = &apos;bye &apos;
else:outdata = &apos;hi &apos; + indata.decode()
conn.send(outdata.encode())
conn.close()
if &apos;quit&apos; in indata.decode():
break
print(&apos;listen...&apos;)
s.close()
#client.py
import socket
while True:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((&apos;127.0.0.1&apos;, 7000))
name = input(&apos;>name:&apos;)
print(&apos;send: &apos; + name)
s.send(name.encode())
indata = s.recv(1024)
s.close()
print(&apos;>&apos; + indata.decode())
if &apos;quit&apos; in name:
break
收藏 ⬇️ 下載