申論 1關於以下C程式碼:01#include<stdio.h>02#define SIZE 1003#define THREE 304unsigned int f1(unsigned int a, unsigned int b){ return (a&&!b); }05unsigned int f2(unsigned int a, unsigned int b){ return (a<<b);}06unsigned int f3(unsigned int a, unsigned int b){ return (a&~b);}07int f4(int a, int b) { return a*b+a-b;}08int f5(int a, int b) {09int data[SIZE];10for (int i=1, k=0; i<a; i++) {11if (i%3==0) data[k++]=f4(i, i+1);12}13return data[b];14}15int f6(int a, int b) {16int data[][THREE] = {{4,3,2},{3,4,2},{2,3,3}};17for (int i=0; i<THREE; i++) {18for (int j=0; j<THREE; j++) {19if (i>a || j>b)20data[i][j]= data[j][i]+b;21}22}23return data[a][b];24}25int main() {26printf("%u\n", f1(6, 2));27printf("%u\n", f2(6, 2));28printf("%u\n", f3(7, 2));29printf("%d\n", f4(3, 12));30printf("%d\n", f5(15, 3));31printf("%d\n", f5(3, 15));32printf("%d\n", f6(1, 1));33printf("%d\n", f6(3, 2));34return 0;35}請說明程式執行後,程式碼編號26~33的輸出,以及其運算邏輯。(25分)
本卷皆為申論題,點「看答案與解析」查看擬答。
弱點分析
未作答的題目不計分。看我的紀錄
申論 2關於以下C++程式碼:01#include <iostream>02#include <string>03#include <exception>04#include <stdexcept>05#include <assert.h>06using namespace std;07class NegativeException: public exception {08const char * what () const throw () { return "negative"; }09};10class DivideByZeroException: public logic_error{11public:12DivideByZeroException() : logic_error( "divide by zero" ) {}13};14int getResult(int x, int y) {15if (x<0 || y<0) throw NegativeException();16else if (y==0) throw DivideByZeroException();17return (x/y);18}19void f(int x, int y) {20try { cout << "Result:"<< getResult(x, y)<<endl; }21catch (std::exception &e) { cout << "1: " << e.what() << "\n"; }22}23void testResult() {24f(2, -1);25f(2, 0);26f(2, 3);27f(6, 3);28}29void assertResult() {30assert(getResult(8, 4)==1);31}32int main() {33testResult();34assertResult();35return 0;36}㈠請說明程式執行後的輸出。(15分)㈡請說明程式中assert與exception的使用時機與目的。(10分)
申論 3關於以下Java程式:01import java.io.*;02import java.util.ArrayList;03abstract class Fruit {04public Fruit(int sweetness) {this.sweetness = sweetness; }05public abstract String eat();06protected String taste() {07if (sweetness>0 && sweetness<5) return "no";08else if (sweetness<=10) return "little";09else if (sweetness<=15) return "some";10else if (sweetness<=20) return "more";11else return "super";12}13private int sweetness;14}15class Apple extends Fruit {16public Apple(String c, int s) { super(s);this.color = c; }17public String eat(){ return color +":"+taste()+" sweetness "; }18private String color;19}20class Watermelon extends Fruit {21public Watermelon(String v, int s) { super(s);this.volume = v; }22public String eat(){ return volume +":"+taste()+" sweetness "; }23private String volume;24}25public class Test {26public static void test01() {27ArrayList<Fruit> fs = new ArrayList<Fruit>();28fs.add(new Apple("red", 18));29fs.add(new Watermelon("big", 20));30fs.add(new Apple("green", 10));31fs.forEach((n) -> System.out.println(n.eat()));32}33public static void main(String[] args) throws InterruptedException {34test01();35}36}㈠請說明程式執行後其輸出與其運作程式碼行數順序。(12分)㈡請依下面表格,說明Fruit的設計功用,包含Fruit類別類型與功用以及方法(method)。(13分)Fruit類別類型與功用Fruit方法功用eat功用taste功用說明
申論 4以下C++程式有部分違反安全程式設計原則,可能具有許多潛在風險。01#include <iostream>02#include <string>03#define SIZE 1004using namespace std;05class Food {06public:07Food() = default;08Food(int c) { cal = c; }09int getCal() { return cal; }10private:11int cal;12};13void f1() {14Food *f[SIZE];15cout<<f[0]->getCal()<<endl;16}17void f2(int n) {18string *f = NULL;19for(int i = 0; i < n; i++)20f = new string("ok");21cout<<*f<<endl;;22}23void f3(int n) {24double x = 3, y1 = 5, y2 = 2;25for (int i=0; i<n; i++) {26x = x/10.0;27y1 = y1/10.0; y2 = y2/10.0;28}29if(x == (y1-y2)) cout<<"X == Y"<<endl;30}31void f4(char *s1, char *s2) {32int len =0;33char *s =s1 ;34while (*s2!='\0') {35*s1=*s2;36s1++; s2++;37}38cout<<s<<endl;39}40void f5(int n) {41int result = 0;42int *d = new int[n];43d[0] = d[1] = 1;44for(int i = 0; i < n-2; i++) d[i+2] = d[i+1]+d[i];45for(int i = 0; i < n; i++) result = result + d[i];46cout<<result<<endl;47}48int main() {49char s1[]="goodness", s2[]="food";50f1();51f2(2);52f3(1);53f4(s1, s2);54f5(6);55return 0;56}請說明此程式,執行函式f1()~f5()的輸出,以及函式f1()~f5()可能具有的潛在風險。(25分)