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

科目名稱:資訊系統 與網路導論 開課系所:資訊工程 學系 任課教師
吳坤熹
系所別:
年級:
學號:
姓名:
考試日期
2009.12.9

(考試時間: 14:10-14:30)

  1. (10%) What value will the following code display?
    // Pass-by-value
    #include <iostream>
    using std::cout;
    using std::endl;

    void test(char p[])
    { cout << ++p << endl;
    }

    int main()
    {
    char *name = "BASIC";
    test(++name);
    cout << name++ << endl;
    test(++name);
    cout << name++ << endl;
    }




  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).
    // void function
    #include <iostream>
    using std::cout;
    using std::endl;

    void print_stars(int n);

    int main()
    {
    int j;
    for (j=1; j<6; j--)
    print_stars(j);
    return 0;
    }

    void print_stars(int n)
    {
    int i;
    for (i=0; i<n; i++)
    cout << "*";
    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 check(int n)
    { static int s = 5;
    int t = 5;
    if (n>0)
    {
    s--;
    t--;
    check(n>>1);
    }
    else
    {
    cout << s << endl;
    cout << t << endl;
    }
    }

    int main()
    {
    check(12);
    }

  4. (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>
    using std::cout;
    using std::endl;

    void print(int i, int j)
    {
    cout << "i = " << i << "\t j = " << j << endl;
    }

    void swap1(int a, int b)
    { int temp;
    temp = a; a = b; b = temp;
    }
    void swap2(int &a, int &b)
    { int temp;
    temp = a; a = b; b = temp;
    }
    void swap3(int &a, int b)
    { int temp;
    temp = a; a = b; b = temp;
    }

    int main()
    {
    int i=3, j=2;
    swap1(i,j); print(i,j);
    swap2(i,j); print(i,j);
    swap3(i,j); print(i,j);
    }