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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2015.6.3
系所別:
年級:
學號:
姓名:
考試時間 08:20-08:30
  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).
    // Creating String Objects (P.405)
    #include <iostream>
    #include <string>
    using std::cout;
    using std::endl;
    using std::string;

    int main()
    {
        int a = 65;
        string b(a, a);
        cout << b << 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).
    // The Size of a Vector (P.495)
    #include <iostream>
    #include <string>
    #include <vector>
    using std::cout;
    using std::endl;
    using std::string;
    using std::vector;
    int main()
    {
       vector<string> a;    // a string vector
       int b = 6;
       int c = 3;
       cout << a.size() << endl;
       a.push_back(b);
       a.push_back(c);
       cout << a.size() << endl;
       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).
    // Creating Vector Containers (P.492)
    #include <iostream>
    #include <vector>
    using std::cout;
    using std::endl;
    using std::vector;

    int main()
    {
        int i;
       
    // an integer array
        int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
       
    // an integer vector       
        vector<int> c( a+2, a+7 );  
       
    // Reverse iterator (P.494)              
        vector<int> d( c.rbegin(), c.rend() );

        for (i=0; i<c.size(); i++)
           cout << c.at(i);
        cout << endl;

        for (i=0; i<d.size(); i++)
           cout << d.at(i);
        cout << endl;
        return 0;
    }