國立暨南國際大學 101 學年度第一學期小考試卷

科目名稱:計算機概 論 開課系所:資訊工程 學系 考試日期 2012.11.27
系所別:
年級:
學號:
姓名:
考試時間 14:10-14: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).
    // String Copy
    #include <iostream>
    #include <cstring>

    using std::cout;
    using std::endl;

    int main()
    {
            char str1[] = "APPLE";
            char str2[] = "abc";
            strncpy(str1, str2, 3);
            cout << str1 << 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).
    // Copying n characters
    #include <iostream>
    #include <cstring>

    using std::cout;
    using std::endl;

    int main()
    {
            char str1[] = "APPLE";
            char str2[] = "abc";
            strncpy(str1, str2, 4);
            cout << str1 << 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).
    // Finding a character in a string
    #include <iostream>
    #include <cstring>

    using std::cout;
    using std::endl;

    int find(char *s, char c)
    {
        int len = 0;
        while (*++s != '\0')
            if (*s == c)
                len++;
        return len;
    }

    int main()
    {
        char str1[] = "This is an island.";
        char str2[] = "That is a dog.";
       
        cout << find(str1, 'i') << endl;
        cout << find(str2, 'T') << endl;
        return 0;
    }