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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2015.4.8
系所別:
年級:
學號:
姓名:
考試時間 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).
    // HW03 - Space Deleting
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    int main()
    {
        int len;
        char s[] = "This is a book.";
        const int MAX_LEN = sizeof(s);
        for(len = 0; s[len] != '\0'; len++)
        {
            while(s[len]==' ')
            {
                cout<<s[len];
                len++;
                while(s[len]==' ')
                    len++;
            }
            cout << s[len];
        }
        cout<<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).
    // Two-dimensional Array
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        char name[5][20] = { "Alice", "Bob", "Charlie", "Dennis", "Emma" };
        cout << sizeof(name[4]) << endl;
        cout << sizeof(name[4][4]) << 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).
    // Pointer vs. Array
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        const char* name[5] = { "Alice", "Bob", "Charlie", "Dennis", "Emma" };
        cout << *(name + 2) << endl;
        return 0;
    }