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

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

    int main()
    {
        unsigned char n = 255;
        cout << static_cast<unsigned short>(n++) << endl;
        cout << static_cast<unsigned short>(n) << 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).
    #include <iostream>

    int main()
    {
        int i;
        for (i=1; i<8; i++)
        {
            if (i>2)
            {
                if (i==4) continue;
                else break;
            }
            std::cout << i << std::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).
    #include <iostream>

    int main()
    {
        int n = 6;
        while (true)
        {
            std::cout << n << ' ';
            if (n==1) break;
            if (n % 2)
                n = 3 * n + 1;
            else
                n /= 2;
        }
        std::cout << std::endl;
        return 0;
    }