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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2015.4.15
系所別:
年級:
學號:
姓名:
考試時間 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).
    // Function Prototype
    #include <iostream>
    using std::endl;
    using std::cout;

    void print_stars(int n);        // Function Prototype

    int main()
    {
        for (int j=1; j<=5; j++)
            print_stars(j);
        return 0;
    }

    int print_stars(int n)
    {
        for (int i=0; i<n; i++)
            cout << '*';
        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).
    // Recursive Function Call
    #include <iostream>
    using std::cout;
    using std::endl;

    void oct(int n)
    {
        if (n >= 8)
            oct( n / 8);    // recursive call
        cout << n % 8;
    }

    int main()
    {
        oct(23); cout << 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).
    // Static Variable
    #include <iostream>
    using std::cout;
    using std::endl;

    void oct(int n)
    {
        static int nCount = 0;
        ++nCount;
        if (n >= 8)
            oct( n / 8);
        else
            cout << "Depth: " << nCount << endl;
        cout << n % 8;
    }

    int main()
    {
        oct(23); cout << endl;
        return 0;
    }