lawpalyer logo

資訊處理 112 年程式設計考古題

民國 112 年(2023)資訊處理「程式設計」考試題目,共 8 題 | 資料來源:考選部

0 題選擇題 + 8 題申論題

關於下列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("%d\n", f1(10, 4)); printf("%d\n", f2(South)); printf("%d\n", f3(6, 4)); printf("%u\n", f3(7, 4)); printf("%d\n", f4(20, 5)); printf("%d\n", f4(10, 4)); printf("%u\n", f5(4, 7)); return 0; }
關於以下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("%u\n", f1(6, 2)); printf("%u\n", f2(6, 2)); printf("%u\n", f3(7, 2)); printf("%d\n", f4(3, 12)); printf("%d\n", f5(15, 3)); printf("%d\n", f5(3, 15)); printf("%d\n", f6(1, 1)); printf("%d\n", f6(3, 2)); return 0; } 請說明程式執行後,程式碼編號26~33的輸出,以及其運算邏輯。(25分)
針對下列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;; } }
(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 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分) 請說明程式中assert與exception的使用時機與目的。(10分)
針對下列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)); } }
(5)
(5)
(5)
(5)
關於以下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類別類型與功用以及方 法(method)。(13分) Fruit類別類型與功用 Fruit方法功用 eat功用 taste功用 說明
針對下列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 import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('127.0.0.1', 7000)) s.listen(5) print('wait for connection...') while True: conn, addr = s.accept() print('connected by ' + str(addr)) indata = conn.recv(1024) print('recv: ' + indata.decode()) if 'quit' in indata.decode(): outdata = 'bye ' else: outdata = 'hi ' + indata.decode() conn.send(outdata.encode()) conn.close() if 'quit' in indata.decode(): break print('listen...') s.close() #client.py import socket while True: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('127.0.0.1', 7000)) name = input('>name:') print('send: ' + name) s.send(name.encode()) indata = s.recv(1024) s.close() print('>' + indata.decode()) if 'quit' in name: break
(5)
(1024)
(1024)
以下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!='\0') { *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分)
(2)
(1)
(6) 25 分