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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2014.5.13
系所別:
年級:
學號:
姓名:
考試時間 18:10-18:20
  1. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Size of an Object
    // Assuming the program is running on a 64-bit computer

    #include <iostream>
    using std::cout;
    using std::endl;

    class CText
    {
    public:
        char* ptext;

        CText(const char* s)
        {
            ptext = new char[strlen(s)+1];
            strcpy(ptext, s);
        }
    };

    int main()
    {
        CText a("NCNU"), b("Berkeley");
        cout << sizeof(a) << strlen(a.ptext) << endl;
        cout << sizeof(b) << strlen(b.ptext) << endl;
        return 0;
    }


  2. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Class Destructors (P.435)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CText
    {
    public:
        char* ptext;

        CText(const char* s)
        {
            ptext = new char[strlen(s)+1];
            strcpy(ptext, s);
        }

        ~CText()
        {
            cout << "Destructor called to release the memory of "
                    << ptext << endl;
            delete ptext;
        }
    };

    int main()
    {
        CText c("Coconut"), m("Mango");
        return 0;
    }


  3. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Implementing a Copy Constructor (P.442)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CText
    {
    public:
        char* ptext;

        CText(char* s)
        {
            ptext = new char[strlen(s)+1];
            strcpy(ptext, s);
        }

        ~CText()
        {
            if (ptext)
                cout << "Destructor called for " << ptext << endl;
            else
                cout << "Destructor called, but no memory needs "
                     << "to be releases." << endl;
        }
    };

    int main()
    {
        CText b("Banana");
        CText a(b);
        *a.ptext = 'b';
        return 0;
    }


  4. (10%) 江蕙的「落雨聲」歌詞中,哪一段表達了「樹欲靜而風不止,子欲養而親不在」的感慨?