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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2014.4.15
系所別:
年級:
學號:
姓名:
考試時間 18:10-18: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).
    // Default Value of Parameters (P.302)
    // Static Variables in a Function (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;
    }


  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).
    // Function Overloading (P.310)
    // Use of the const Modifier (P.270)

    // Strings and Character Arrays (P.174)
    #include <iostream>
    #include <cstring>
    using std::endl;
    using std::cout;

    int text_size(char c)
    { return sizeof(c); }

    int text_size(const char s[])
    { return strlen(s); }

    int main()
    {
        cout << text_size('T') << endl;
        cout << text_size('10') << 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).
    // Pointers to Functions (P.295)
    #include <iostream>

    int sum(int a, int b)
    { return a + b; }

    int product(int a, int b)
    { return a * b; }

    int main()
    {
        int (*pdo_it) (int, int) = product;
        pdo_it = sum;
        std::cout << pdo_it(3, 7) << std::endl;
        return 0;
    }