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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2014.6.3
系所別:
年級:
學號:
姓名:
考試時間 18:10-20:00
1

2

3
 
4

5
 
6
 
7
 
8
 
9
 
10

  1. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Pointer Arithmetic (P.194)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        const char* pstr1 = "Good morning.";
        const char* pstr2 = pstr1 + 5;
        cout << pstr2++;
        cout << pstr2 << endl;
        return 0;
    }


  2. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Static variables in functions (P.283)
    #include <iostream>
    using std::endl;
    using std::cout;

    int pop(int n = 1)
    {
        static unsigned short array [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        static unsigned short length = sizeof array / sizeof array[0];
        while (n-- > 1)
            length--;
        return array[--length];
    }

    int main()
    {
        cout << pop(4) << endl;
        cout << pop(2) << endl;
        cout << pop( ) << endl;
        return 0;
    }


  3. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Recursive Function Calls (P.285)
    #include <iostream>
    using std::cout;

    void octal(int n)
    {
        if (n >= 8 )
            octal(n / 8);
        else
            cout << n % 8;
    }

    int main()
    {
        octal(156);
        return 0;
    }


  4. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Constructors (P.374)
    #include <iostream>
    using std::cout;

    class CRational
    {
    public:
        int numerator;
        int denominator;

        void Print()
        {
            cout << numerator;
            if (denominator != 1)
            {
                cout << '/' << denominator;
            }
        }

        Constructor(int q = 0, int p = 1)
        {
            numerator   = q;
            denominator = p;
        }
    };

    int main()
    {
       CRational a(1,3);
       CRational b(2);

       a.Print();
       b.Print();
       return 0;
    }


  5. (10%) Assume that on a 64-bit computer, each pointer in C++ will occupy 8 bytes.  Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Size of a Class
    #include <iostream>
    #include <cstring>
    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 size()
        { return sizeof(*this); }

        int length()
        { return strlen(ptext); }
    };

    int main()
    {
        CText a("NCNU"), b("MIT");
        cout << a.size() << '\t' << a.length() << endl;
        cout << b.size() << '\t' << b.length() << endl;
        return 0;
    }



  6. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Destructors (P.436 - P.444)
    #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 ("
                    << strlen(ptext)+1 << " bytes) of "
                    << ptext << endl;
            delete [] ptext;
        }
    };

    int main()
    {
        CText a("Apple"), b("Banana");
        return 0;
    }


  7. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    // Copy Constructor (P.442)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CText
    {
    public:
        char* ptext;

        CText(char* s = NULL)
        {
            if (s)
            {
                ptext = new char[strlen(s)+1];
                strcpy(ptext, s);
            }
            else
                ptext = static_cast<char*>(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 c("Coconut");
        CText a(c);
        a.ptext += 4;
        return 0;
    }

  8. (10%) Determine whether the following code has syntax erros or
        not.  If it is correct, predict its output.  If it is
        incorrect, point out the
        mistake(s).

    // Overloading the Addition Operator (P.459)
    #include <iostream>
    using std::cout;
    using std::endl;

    class CData
    {
    public:
    int* pdata;

    CData(int v = 0)
    { pdata = new int(v); }

    CData Add(CData d)
    { return CData(*pdata = *d.pdata); }

    CData operator+(CData d)
    { return Add(d); }

    void Print()
    { cout << *pdata << endl; }
    };

    int main()
    {
    CData a(5), b(3);
    CData c;
    c = a + b;
    a.Print();
    c.Print();
    return 0;
    }

  9. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Searching Strings (P.523)
    #include <iostream>
    #include <string>
    using std::cout;
    using std::endl;
    using std::string;
    int o(string a, string b)
    {
    int c = 0, p = 0;
    while ( a.find(b, p) <= a.length() )
    {
    p = a.find(b, p) + 1 ;
    c++;
    }
    return c;
    }

    int main()
    {
    string a("NCCU");
    string b("C");
    cout << o(a, b) << endl;
    cout << o(a, b+"U") << endl;
    cout << o(a, "NCNU") << endl;
    return 0;
    }

  10. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // The Size of a Vector (P.655)
    #include <iostream>
    #include <string>
    #include <vector>
    using std::cout;
    using std::endl;
    using std::string;
    using std::vector;

    int main()
    {
    vector<int> a(10);
    int b = 5;
    int c = 27;

    cout << a.size() << endl;
    a.push_back(b);
    a.push_back(c);
    cout << a.size() << endl;

    return 0;
    }