國立暨南國際大學 114 學年度第二學期小考試卷

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2026.3.20
系所別:
年級:
學號:
姓名:
考試時間 14:30-14:40
  1. (10%) After you run the following program on a Windows PC, what will be the sizes of "a.txt" and "b.dat", respectively?
    // Size of Files
    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    struct Box {
        unsigned short length;
        unsigned short width;
        unsigned short height;
    };

    int main() {
        Box b[] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
        FILE* f = fopen("a.txt", "w");
        for (size_t i=0; i<sizeof(b)/sizeof(b[0]); ++i)
            fprintf(f, "%d %d %d\n", b[i].length, b[i].width, b[i].height);
        fclose(f);

        f = fopen("b.dat", "wb");
        fwrite(b, sizeof(b[0]), sizeof(b)/sizeof(b[0]), f);
        fclose(f);
        return 0;
    }

  2. (10%) Determine whether the syntax of the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // struct tm
    #include <cstdio>
    #include <ctime>
    int main() {
        time_t t = time(0);
        struct tm* now = localtime(&t);
        printf("%d-%d-%d\n", now->tm_year, now->tm_mon, now->tm_mday);
        return 0;
    }


  3. (10%) Determine whether the syntax of the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Function Pointers
    #include <iostream>
    int add(int a, int b) { return a + b; }
    int mod(int a, int b) { return a % b; }

    int main() {
        int *f[2](int, int)  = { add, mod };
        for (size_t i=0; i<sizeof(f)/sizeof(f[0]); ++i)
            std::cout << f[i](5, 3) << std::endl;
        return 0;
    }