民國112年 高考三級 資訊處理 程式設計考古題

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

官方資料

題目與答案

第1題

關於以下C程式碼: #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分)

第2題

關於以下C++程式碼: #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分)

第3題

關於以下Java程式: 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功用 說明

第4題

以下C++程式有部分違反安全程式設計原則,可能具有許多潛在風險。 #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分)