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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2022.3.9
系所別:
年級:
學號:
姓名:
考試時間 08:10-08:20
  1. (10%) Suppose the following program is compiled and run on a little-endian computer (e.g., lilina).  What will be the output?
    /* Dynamic memory allocation, reinterpret_cast, and little-endian */
    #include <iostream>

    int main()
    {
        char* p1 = new char[4]{'1', '0', '3', '\0'};
        int* p2 = reinterpret_cast<int*>(p1);
        std::cout << p1 << std::endl;
        std::cout << *p2 << std::endl;
        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).
    /* Binary Tree */
    #include <iostream>

    struct Node {
        char ch;
        Node* left;
        Node* right;
    };

    void printTree(Node* p) {
        if (p) {
            printTree(p->left);
            std::cout << p->ch;
            printTree(p->right);
        }
    }

    int main()
    {
        Node G = { 'G', nullptr, nullptr };
        Node F = { 'F', nullptr, nullptr };
        Node E = { 'E', nullptr, nullptr };
        Node D = { 'D', nullptr, nullptr };
        Node C = { 'C', &F, &G };
        Node B = { 'B', &D, &E };
        Node A = { 'A', &B, &C };
        printTree(&A);
        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).
    /* Bitwise Operation */
    #include <iostream>

    int main()
    {
        char s[6] = "Hello";
        char* p = s;
        while (*p != '\0') {
            *p = *p & 0xdf;
            ++p;
        }
        std::cout << s << std::endl;
        return 0;
    }