民國112年 地方三等 資訊處理 程式設計考古題

民國112年 地方三等 資訊處理 程式設計收錄考選部官方試題,共 4 題可線上瀏覽,考選部未公告本科目答案。

官方資料

題目與答案

第1題

關於下列C 程式碼,請說明程式執行後,程式碼編號27~33 的輸出,以 及其運算邏輯。(25 分) #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; }

第2題

針對下列C++程式,請標示出Except 類別的f1, …, f6 函式中有問題的 函式,與說明其問題之原因;並請說明若將有問題的函式和程式碼刪除, 其程式執行後之輸出。(25 分) #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); } 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;; } }

第3題

針對下列Java 程式碼,請完成統一塑模語言(UML)類別圖(a)~(e);另 外請標示出錯誤程式碼行數並說明錯誤原因;以及說明若將錯誤行數程 式碼予以註解後,執行其程式的輸出。(25 分) 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)); } }

第4題

針對下列Python 程式碼,依序在兩個Terminal 執行server.py 和client.py 後,在client.py 輸入Tom 和quit;請說明client.py 的Terminal 之輸出內 容,並說明Line 03, 04, 05 程式碼的運作邏輯。(25 分) # server.py 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