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

科目名稱:計算機概 論 開課系所:資訊工程 學系 考試日期 2012.11.30
系所別:
年級:
學號:
姓名:
考試時間 08:10-08:25

  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).
    // Pointers as Arguments to a Function
    #include <iostream>

    int strlen(char* s)
    {
        char* p = s;
        while (*s != '\0')
            ++s;
        return s - p;
    }

    int main()
    {
        char* str = "NCNU";
        int n = strlen(str);
        printf("The length of \"%s\" is %d.\n",
            str, n);
        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).
    // Pass-by-reference
    #include <iostream>

    int strlen(char* &s)
    {
        char* p = s;
        while (*s != '\0')
            ++s;
        return s - p;
    }

    int main()
    {
        char* str = "NCNU";
        int n = strlen(str);
        printf("The length of \"%s\" is %d.\n",
            str, n);
        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).
    // Static variable
    #include <iostream>

    int CountDown()
    {
        static int n = 10;
        // Try this code with static and try it without it
        if (n>0)
            return n--;
        else
            return 0;
    }

    int main()
    {
        CountDown();
        CountDown();
        std::cout << CountDown() << std::endl;
        return 0;
    }